Manage Azure Virtual Machines with Azure CLI

This guide covers common operations for managing Azure Virtual Machines (VMs) using the Azure Command-Line Interface (CLI). The Azure CLI provides a powerful and flexible way to interact with your Azure resources.

Prerequisites

Before you begin, ensure you have:

Common Management Tasks

Start and Stop VMs

You can start or stop a VM to manage costs or perform maintenance.

Start a VM

az vm start --resource-group MyResourceGroup --name MyVM

Stop a VM

az vm stop --resource-group MyResourceGroup --name MyVM

Deallocate a VM (stops billing for compute costs)

az vm deallocate --resource-group MyResourceGroup --name MyVM

Restart a VM

To restart a running VM:

az vm restart --resource-group MyResourceGroup --name MyVM

View VM Status

Check the current power state of your VM.

az vm show --resource-group MyResourceGroup --name MyVM --query instanceView.statuses[1].displayStatus

The output will typically be "VM running" or "VM stopped".

Resize a VM

Change the size of your VM to adjust its CPU, memory, and other resources. You must stop and deallocate the VM before resizing.

List available VM sizes in a region

az vm list-sizes --location eastus --output table

Resize the VM

az vm resize --resource-group MyResourceGroup --name MyVM --size Standard_D4s_v3

Attach and Detach Disks

Manage data disks for your VM.

Attach an existing managed disk

az vm disk attach --resource-group MyResourceGroup --vm-name MyVM --name MyManagedDisk --lun 1

Detach a disk

az vm disk detach --resource-group MyResourceGroup --vm-name MyVM --name MyManagedDisk

Take Snapshots

Create point-in-time snapshots of your VM disks for backup and disaster recovery.

Create a snapshot of an OS disk

az snapshot create --resource-group MyResourceGroup --name MyVMSnapshot --source /subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/MyVM/osDisk

For comprehensive backup and restore solutions, consider Azure Backup.

Delete a VM

Remove a VM and its associated resources (network interfaces, public IP addresses, etc.).

Caution: This action is irreversible.

az vm delete --resource-group MyResourceGroup --name MyVM

To delete the VM and its associated resource group:

az group delete --name MyResourceGroup

Get VM IP Addresses

Retrieve the public and private IP addresses assigned to your VM.

az vm show --resource-group MyResourceGroup --name MyVM --show-details --query '{PrivateIP:privateIps, PublicIP:publicIps}'

Update VM Extensions

Manage VM extensions, such as the Custom Script Extension or desired state configuration.

List installed extensions

az vm extension list --resource-group MyResourceGroup --vm-name MyVM --output table

Set (install/update) an extension

az vm extension set --resource-group MyResourceGroup --vm-name MyVM --name CustomScript --publisher Microsoft.Azure.Extensions --settings '{"fileUris": ["https://example.com/script.sh"], "commandToExecute": "sh script.sh"}'