Azure Virtual Machines CLI Documentation
This documentation provides comprehensive guidance on managing Azure Virtual Machines using the Azure Command-Line Interface (CLI).
Introduction to Azure CLI for VMs
The Azure CLI is a powerful tool for managing Azure resources from your command line. It enables you to automate tasks, script deployments, and interact with Azure services efficiently. For Virtual Machines (VMs), the Azure CLI offers a wide range of commands to create, configure, and manage your compute resources.
Installation
Before you can use the Azure CLI, you need to install it on your local machine or use the Azure Cloud Shell.
Windows Installation
Download the MSI installer from the official Azure CLI documentation or use package managers like Chocolatey:
choco install azure-cli
macOS Installation
Use Homebrew for installation:
brew update && brew install azure-cli
Linux Installation
Installation varies by distribution. For Debian/Ubuntu:
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Refer to the official documentation for other distributions.
Azure Cloud Shell
The Azure Cloud Shell is pre-installed with the Azure CLI. Access it through the Azure portal or by visiting shell.azure.com.
Logging In
To manage your Azure resources, you first need to log in to your Azure account:
Open your terminal or command prompt and run:
az login
This command will open a browser window for you to authenticate. After successful login, you can set your default subscription if you have multiple:
az account set --subscription "Your Subscription Name or ID"
Managing Virtual Machines
The Azure CLI provides commands for the entire lifecycle of a virtual machine.
Creating a Virtual Machine
Create a new VM with the az vm create command. You'll need to specify a resource group, VM name, and an image.
Basic VM creation:
az vm create \
--resource-group MyResourceGroup \
--name MyVM \
--image UbuntuLTS \
--admin-username azureuser \
--admin-password 'YourSecurePassword123!'
For SSH key authentication (recommended):
az vm create \
--resource-group MyResourceGroup \
--name MyVM \
--image UbuntuLTS \
--admin-username azureuser \
--ssh-key-values ~/.ssh/id_rsa.pub
To create a VM with a specific size and region:
az vm create \
--resource-group MyResourceGroup \
--name MyVM \
--image CentOS \
--size Standard_DS2_v2 \
--location eastus \
--admin-username azureuser \
--generate-ssh-keys
Listing Virtual Machines
View all VMs in your current subscription or within a specific resource group:
List all VMs in the subscription:
az vm list --output table
List VMs in a specific resource group:
az vm list --resource-group MyResourceGroup --output table
Showing VM Details
Get detailed information about a specific VM:
az vm show --resource-group MyResourceGroup --name MyVM --show-details
To get the public IP address:
az vm show -g MyResourceGroup -n MyVM -d --query publicIps -o tsv
Starting a Virtual Machine
Start a stopped VM:
az vm start --resource-group MyResourceGroup --name MyVM
Stopping a Virtual Machine
Stop a running VM. Note that this deallocates the VM, stopping billing for compute resources.
az vm stop --resource-group MyResourceGroup --name MyVM
Restarting a Virtual Machine
Restart a VM:
az vm restart --resource-group MyResourceGroup --name MyVM
Deleting a Virtual Machine
Delete a VM. This action is irreversible. You may be prompted to confirm.
az vm delete --resource-group MyResourceGroup --name MyVM
To delete a VM and all associated resources (like disks and NICs):
az vm delete --resource-group MyResourceGroup --name MyVM --delete-osdisk --delete-data-disks
Managing VM Disks
You can manage disks attached to your VMs, such as attaching or detaching data disks.
List disks associated with a VM:
az vm disk list -g MyResourceGroup -n MyVM --query "[].{Name:name, SizeGB:diskSizeGB, Type:storageAccountType}" -o table
Attach a managed disk to a VM:
az vm disk attach --resource-group MyResourceGroup --vm-name MyVM --name MyDataDisk.vhd --new --size-gb 100 --sku Standard_LRS
Changing VM Size
Resize a VM to a different SKU. The VM must be stopped and deallocated first.
az vm deallocate --resource-group MyResourceGroup --name MyVM
az vm resize --resource-group MyResourceGroup --name MyVM --size Standard_DS3_v2
az vm start --resource-group MyResourceGroup --name MyVM
Networking
Manage virtual networks (VNets), subnets, and network interface cards (NICs).
Creating a Virtual Network
Create a VNet and subnet:
az network vnet create \
--resource-group MyResourceGroup \
--name MyVNet \
--address-prefix 10.0.0.0/16 \
--subnet-name MySubnet \
--subnet-prefix 10.0.0.0/24
Creating a Network Interface
Create a NIC and associate it with a subnet:
az network nic create \
--resource-group MyResourceGroup \
--name MyNic \
--vnet-name MyVNet \
--subnet MySubnet \
--public-ip-address MyPublicIp
Note: The public IP address must be created separately if needed: az network public-ip create -g MyResourceGroup -n MyPublicIp
Virtual Machine Scale Sets (VMSS)
Manage VM Scale Sets for automatic scaling and load balancing.
Create a VM Scale Set:
az vmss create \
--resource-group MyResourceGroup \
--name MyVmss \
--image UbuntuLTS \
--upgrade-policy-mode automatic \
--admin-username azureuser \
--generate-ssh-keys
List VMSS:
az vmss list --resource-group MyResourceGroup -o table
Automation and Scripting
Leverage the Azure CLI to script complex deployment scenarios and automate routine tasks. Use scripting languages like Bash, PowerShell, or Python to orchestrate VM deployments and configurations.
Tip: Explore the --query argument to filter and shape the output of commands, making it easier to extract specific information for scripting.