Azure Cheatsheet

Virtual Machines

Use this Azure reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Create a VM

# Quickstart — Linux (generates SSH keys automatically)
az vm create \
  --resource-group myRG \
  --name myVM \
  --image Ubuntu2204 \
  --size Standard_B2s \
  --admin-username azureuser \
  --generate-ssh-keys

# With an existing public key
az vm create \
  --resource-group myRG \
  --name myVM \
  --image Ubuntu2204 \
  --size Standard_B2s \
  --admin-username azureuser \
  --ssh-key-values ~/.ssh/id_rsa.pub

# Windows VM with password
az vm create \
  --resource-group myRG \
  --name myWinVM \
  --image Win2022Datacenter \
  --size Standard_D2s_v5 \
  --admin-username azadmin \
  --admin-password "P@ssw0rd1234!"

# Place in specific AZ for HA
az vm create ... --zone 1

# Attach to existing VNet/subnet
az vm create ... \
  --vnet-name myVNet \
  --subnet mySubnet \
  --public-ip-address ""   # no public IP

Common Images

az vm image list --all -o table           # full list (slow)
az vm image list --offer Ubuntu -o table  # filter by offer

# Popular aliases (use with --image)
# Ubuntu2204, Ubuntu2004, Debian11, CentOS85Gen2,
# Win2022Datacenter, Win2019Datacenter, RHEL9

Common VM Sizes

SizevCPUsRAMUse case
Standard_B1s11 GBDev/test burstable
Standard_B2s24 GBLight workloads
Standard_D2s_v528 GBGeneral purpose
Standard_D4s_v5416 GBGeneral purpose
Standard_E4s_v5432 GBMemory-intensive
Standard_F4s_v248 GBCompute-intensive
Standard_NC6s_v36112 GBGPU (V100)
# List sizes in a region
az vm list-sizes --location eastus -o table

Start / Stop / Restart

CommandWhat it does
az vm start -g myRG -n myVMStart a stopped VM
az vm stop -g myRG -n myVMStop (OS shutdown, still billed for disk)
az vm deallocate -g myRG -n myVMStop + deallocate (stops compute billing)
az vm restart -g myRG -n myVMReboot
az vm redeploy -g myRG -n myVMMove to new host (fixes host issues)
# Deallocate multiple VMs at once
az vm deallocate --ids $(az vm list -g myRG --query "[].id" -o tsv)

Connect

# SSH (Linux)
ssh azureuser@<public-ip>

# Get public IP
az vm show -g myRG -n myVM -d --query publicIps -o tsv

# RDP (Windows) — download RDP file
az vm show -g myRG -n myVM -d --query [fqdns,publicIps] -o tsv

# Serial console access (browser-based emergency)
az serial-console connect -g myRG -n myVM

# Run command remotely (no SSH needed)
az vm run-command invoke \
  -g myRG -n myVM \
  --command-id RunShellScript \
  --scripts "df -h && free -m"

Disks

# List disks
az disk list -g myRG -o table

# Create and attach a data disk
az vm disk attach \
  -g myRG --vm-name myVM \
  --name myDataDisk \
  --new --size-gb 128 \
  --sku Premium_LRS

# Detach a data disk
az vm disk detach -g myRG --vm-name myVM --name myDataDisk

# Resize OS disk (VM must be deallocated)
az vm deallocate -g myRG -n myVM
az disk update -g myRG -n myVM_OsDisk --size-gb 256
az vm start -g myRG -n myVM

# Create snapshot of a disk
az snapshot create \
  -g myRG --name mySnap \
  --source $(az vm show -g myRG -n myVM --query "storageProfile.osDisk.managedDisk.id" -o tsv)

Images & Snapshots

# Capture VM as generalized image (deallocate + generalize first)
az vm deallocate -g myRG -n myVM
az vm generalize -g myRG -n myVM
az image create -g myRG --name myImage --source myVM

# Create VM from captured image
az vm create -g myRG -n newVM --image myImage --admin-username azureuser --generate-ssh-keys

Extensions & Custom Script

# Run a script on VM at creation time
az vm create ... \
  --custom-data cloud-init.yaml

# cloud-init.yaml example
#cloud-config
packages:
  - nginx
  - git
runcmd:
  - systemctl enable nginx
  - systemctl start nginx
# Install VM extension (Custom Script)
az vm extension set \
  -g myRG --vm-name myVM \
  --name CustomScript \
  --publisher Microsoft.Azure.Extensions \
  --settings '{"fileUris":["https://example.com/setup.sh"],"commandToExecute":"bash setup.sh"}'

# List installed extensions
az vm extension list -g myRG --vm-name myVM -o table

Availability & Scaling

# Create availability set
az vm availability-set create -g myRG -n myAvSet \
  --platform-fault-domain-count 2 \
  --platform-update-domain-count 5

# Create VM in the availability set
az vm create ... --availability-set myAvSet

# Create Virtual Machine Scale Set
az vmss create \
  -g myRG -n myVMSS \
  --image Ubuntu2204 \
  --instance-count 3 \
  --vm-sku Standard_B2s \
  --admin-username azureuser \
  --generate-ssh-keys \
  --upgrade-policy-mode automatic

# Scale VMSS manually
az vmss scale -g myRG -n myVMSS --new-capacity 5

# Enable autoscale
az monitor autoscale create \
  -g myRG --resource myVMSS \
  --resource-type Microsoft.Compute/virtualMachineScaleSets \
  --name autoscale-profile \
  --min-count 2 --max-count 10 --count 3

Monitoring & Status

# Show VM power state
az vm get-instance-view -g myRG -n myVM \
  --query "instanceView.statuses[1].displayStatus" -o tsv

# List all VMs across subscription with status
az vm list -d -o table

# Show boot diagnostics
az vm boot-diagnostics get-boot-log -g myRG -n myVM