Azure

Azure VM CLI

Azure VM CLI commands for virtual machine management.

32 commands
Windows MacOS Linux
#compute #cloud

VM Management

az vm create -g myRG -n myVM --image Ubuntu2204 --size Standard_B2s # Create a VM with Ubuntu
List VMs in resource group as table
az vm list -g myRG -o table
Show VM details
az vm show -g myRG -n myVM
Start a stopped VM
az vm start -g myRG -n myVM
Stop a running VM
az vm stop -g myRG -n myVM
Restart a VM
az vm restart -g myRG -n myVM
Deallocate VM to stop billing
az vm deallocate -g myRG -n myVM
Delete a VM without confirmation
az vm delete -g myRG -n myVM --yes

Disk Management

az disk create -g myRG -n myDisk --size-gb 128 --sku Premium_LRS # Create managed disk
List all disks in resource group
az disk list -g myRG -o table
az vm disk attach -g myRG --vm-name myVM --name myDisk # Attach disk to VM
az vm disk detach -g myRG --vm-name myVM --name myDisk # Detach disk from VM
az disk update -g myRG -n myDisk --size-gb 256 # Resize a managed disk
az snapshot create -g myRG -n mySnap --source myDisk # Create disk snapshot

Networking

az vm open-port -g myRG -n myVM --port 80 --priority 100 # Open port on VM
az vm list-ip-addresses -g myRG -n myVM -o table # Show VM IP addresses
az network nic list --resource-group myRG -o table # List NICs in resource group
az network public-ip create -g myRG -n myIP --sku Standard # Create public IP
az network nsg rule create -g myRG --nsg-name myNSG -n allowSSH --priority 100 --destination-port-ranges 22 # Create NSG rule

Images

List popular VM images
az vm image list --output table
az vm image list --all --publisher Canonical -o table # List all Canonical images
az vm image list-skus -l eastus -p Canonical -f 0001-com-ubuntu-server-jammy -o table # List image SKUs
az image create -g myRG -n myImage --source myVM # Create image from VM
az vm capture -g myRG -n myVM --vhd-name-prefix myPrefix # Capture VM as image

Extensions

az vm extension set -g myRG --vm-name myVM --name customScript --publisher Microsoft.Azure.Extensions # Install extension
az vm extension list -g myRG --vm-name myVM -o table # List installed extensions
az vm extension delete -g myRG --vm-name myVM --name customScript # Remove extension
az vm run-command invoke -g myRG -n myVM --command-id RunShellScript --scripts "hostname" # Run command on VM
List available extensions
az vm extension image list -o table

Quick Commands

Create an Ubuntu VM in a resource group
az vm create -g myRG -n myVM --image Ubuntu2204 --size Standard_B2s
Deallocate VM to stop billing
az vm deallocate -g myRG -n myVM
Run a shell command on a VM
az vm run-command invoke -g myRG -n myVM --command-id RunShellScript --scripts "hostname"