Creating Your First Azure VM: Step-by-Step with the CLI
This page walks through creating an Azure Virtual Machine from scratch using the Azure CLI. You will create a resource group, generate SSH keys, provision a Linux VM, and connect to it — covering the real decisions you face along the way rather than just running commands blindly.
Before you start
You need two things: the Azure CLI installed and authenticated. If you have not done this yet, see installing the Azure CLI. Then run:
# Authenticate with your Azure account
az login
# Confirm which subscription is active
az account show --output tableIf you have multiple subscriptions, set the correct one before creating any resources:
az account set --subscription "My Subscription Name"If you do not want to install the CLI locally, use Azure Cloud Shell — it opens a browser-based terminal with the CLI already installed and authenticated.
Step 1: Create a resource group
Every Azure resource must belong to a resource group. A resource group is a container that holds related resources — it makes management, billing, and cleanup straightforward. When you delete a resource group, all resources inside it are deleted together.
az group create \
--name learn-vm-rg \
--location eastusChoose a region close to your users or your other Azure resources. Run az account list-locations —output table to see all available regions and their display names.
Step 2: Create the VM
A single az vm create command creates the VM and all required supporting resources: a virtual network, subnet, public IP address (optional), network interface, and network security group.
az vm create \
--resource-group learn-vm-rg \
--name my-first-vm \
--image Ubuntu2204 \
--size Standard_B2s \
--admin-username azureuser \
--generate-ssh-keys \
--public-ip-sku StandardWhat each flag does:
—image Ubuntu2204— boots from Ubuntu 22.04 LTS. Runaz vm image list —output tablefor other options.—size Standard_B2s— a burstable VM with 2 vCPUs and 4 GB RAM. Good for light workloads and learning. See VM sizes explained for the full family breakdown.—admin-username azureuser— the Linux user account created on the VM. Avoidrootoradmin.—generate-ssh-keys— creates an RSA key pair if one does not exist in~/.ssh/and registers the public key on the VM.—public-ip-sku Standard— attaches a Standard SKU public IP. Standard SKU is required for Availability Zone support and is recommended over Basic.
The command takes 1–3 minutes. When it completes, it returns JSON including the VM’s public IP address:
{
"publicIpAddress": "20.x.x.x",
"resourceGroup": "learn-vm-rg",
"id": "/subscriptions/.../virtualMachines/my-first-vm"
}By default, az vm create opens port 22 (SSH) in the NSG. This is convenient for learning but not recommended for production. For production VMs, remove direct SSH access and use Azure Bastion instead.
Step 3: Connect to your VM
SSH to the VM using the public IP address from the previous step:
ssh azureuser@20.x.x.xIf you used —generate-ssh-keys, the CLI saved the private key to ~/.ssh/id_rsa and SSH will use it automatically. Once connected, you are inside a standard Ubuntu shell. Run a quick check to confirm you are on Azure infrastructure:
# Check the OS version
cat /etc/os-release
# Check the VM's hostname (matches the VM name in Azure)
hostname
# View attached disks
lsblkYou will see an OS disk (sda) and typically a temporary disk (sdb). The temporary disk is local to the physical host and is lost when the VM is deallocated — do not store data you want to keep there.
Step 4: Basic VM management commands
A few CLI commands you will use regularly:
# Stop and deallocate the VM (stops billing for compute)
az vm deallocate --resource-group learn-vm-rg --name my-first-vm
# Start a deallocated VM
az vm start --resource-group learn-vm-rg --name my-first-vm
# Restart a running VM
az vm restart --resource-group learn-vm-rg --name my-first-vm
# Show VM status
az vm show \
--resource-group learn-vm-rg \
--name my-first-vm \
--show-details \
--output table
# List all VMs in a resource group
az vm list --resource-group learn-vm-rg --output tableThe distinction between stop and deallocate matters for billing. az vm stop shuts down the OS but keeps the hardware allocated — you continue paying for compute. az vm deallocate releases the hardware reservation. Always deallocate when you want to stop billing.
Opening a port on the VM
The NSG created by az vm create only allows SSH by default. If you install a web server on the VM and want to reach it from your browser, open port 80:
az vm open-port \
--resource-group learn-vm-rg \
--name my-first-vm \
--port 80 \
--priority 900This adds a rule to the VM’s NSG allowing inbound TCP on port 80 from any source. For production workloads, restrict the source to specific IP ranges or place the VM behind an Azure Load Balancer rather than exposing it directly.
Cleaning up: delete everything
Deleting the VM alone leaves orphaned resources — the managed disk, public IP, network interface, and NSG still exist and some may still incur charges. The cleanest approach is to delete the whole resource group:
az group delete --name learn-vm-rg --yes --no-waitThe —yes flag skips the confirmation prompt. The —no-wait flag returns the terminal immediately while deletion happens in the background. All resources in the group are deleted within a few minutes.
Always check for orphaned resources after deleting VMs individually. Run az resource list —resource-group my-rg —output table to see what still exists in a resource group. Disks and public IPs are the most common resources left behind when VMs are deleted through the portal without selecting “delete associated resources”.
What az vm create built behind the scenes
A single az vm create command created seven Azure resources:
- Virtual Machine — the compute resource itself
- OS Managed Disk — a Premium SSD with the Ubuntu image
- Network Interface (NIC) — connects the VM to the virtual network
- Virtual Network — a private network space (10.0.0.0/16 by default)
- Subnet — a subdivision of the VNet (10.0.0.0/24 by default)
- Public IP Address — the externally accessible IP
- Network Security Group — the firewall with the SSH allow rule
In production you typically pre-create the virtual network and subnet rather than letting the VM create them, so you can place multiple resources in the same network with consistent addressing. The —vnet-name and —subnet flags on az vm create let you specify an existing network instead of creating a new one.
Things that catch beginners out
- Not deallocating — just closing the terminal. Your VM keeps running and billing until you explicitly deallocate it. Closing an SSH session does not stop the VM.
- Deleting the VM but not the disk. When you delete a VM through the portal, it asks whether to delete associated disks. If you click through without checking, the managed disk remains and you continue paying for storage.
- Using a weak admin username. Usernames like
admin,administrator, orrootare targets for brute-force attacks. Use a non-obvious username and authenticate with SSH keys, not passwords. - Not noting the region of existing resources. If you create a VM in East US but your virtual network is in West Europe, the VM cannot join that network. All resources in the same network must be in the same region.
Summary
- Create a resource group first — it is the container for all the resources your VM needs.
az vm createprovisions the VM plus a VNet, subnet, NIC, NSG, and public IP in one command.- Use
—generate-ssh-keysif you do not already have an SSH key, and connect withssh azureuser@<ip>. - Deallocate (not just stop) a VM to stop compute billing. Storage charges continue for the attached disk.
- Delete the resource group to remove all associated resources cleanly in one operation.
Frequently asked questions
How long does it take to create an Azure VM?
Most VMs provision within 1–3 minutes using the CLI or portal. The VM is running and connectable shortly after the create command returns. Complex configurations with multiple disks or extensions take slightly longer.
Do I need an SSH key before creating a Linux VM?
No. The az vm create command can generate an SSH key pair for you using --generate-ssh-keys. It saves the private key to ~/.ssh/id_rsa on your local machine. If you already have a key, pass --ssh-key-values @~/.ssh/id_rsa.pub to use it instead.
How do I delete a VM and all its associated resources?
The easiest method is to delete the entire resource group: az group delete --name my-rg --yes. This removes the VM, its disks, network interface, public IP (if any), and NSG in one operation. Deleting the VM alone leaves the disks and network resources behind as orphaned resources you will still be billed for.