Last updated: May 2026
Azure Virtual Machines Beginner AZ-104 ⏱ 15 min read

Create a Linux VM in Azure

Linux VMs are the most common VM type in Azure — cheaper than Windows (no OS licence fee), more flexible, and the standard choice for web servers, containers, and cloud-native workloads. This page walks you through creating a Linux VM from scratch, with a focus on SSH key authentication — the secure, professional way to connect.

What you'll learn Linux VM creation via Portal and CLI · SSH key authentication explained · Choosing the right Linux distribution · Networking configuration · Connecting via SSH · Running your first commands · Linux VM vs Windows VM differences · Cleanup

Linux VM vs Windows VM

FactorLinux VMWindows VM
OS licence costFree — no licence feeIncluded in VM price (~40% premium)
Connection methodSSH (port 22)RDP (port 3389)
AuthenticationSSH keys (recommended) or passwordPassword
Typical useWeb servers, containers, cloud-nativeLegacy apps, .NET, SQL Server, AD
VM name limit64 characters15 characters
Cost (B2s example)~₹2,200/month~₹4,000/month

Choosing a Linux Distribution

Azure supports many Linux distributions. Here are the most popular:

DistributionBest ForNotes
Ubuntu Server 22.04 LTSGeneral use, developersMost popular, huge community, great documentation
Ubuntu Server 20.04 LTSStable workloadsOlder LTS, still widely used
Red Hat Enterprise Linux (RHEL)Enterprise, regulated industriesPaid licence, enterprise support
CentOS StreamRHEL-compatible workloadsFree, upstream of RHEL
DebianStable, minimal footprintRock-solid stability
SUSE Linux EnterpriseSAP workloadsCertified for SAP HANA on Azure
💡
Recommendation Start with Ubuntu Server 22.04 LTS. It has the best Azure integration, the largest community, the most documentation, and free OS support. Unless you have a specific reason to use another distro, Ubuntu is the safe default.

SSH Keys — The Right Way to Authenticate

SSH keys are a more secure alternative to passwords. Instead of typing a password every time you connect, you use a pair of cryptographic keys:

  • Private key — Stays on your local machine. Never share this.
  • Public key — Goes on the VM. Azure installs this automatically during VM creation.

When you connect, SSH uses your private key to prove your identity — no password needed, and much harder to brute-force than a password.

Generate an SSH Key Pair

Bash / PowerShell Generate SSH key pair
# Works on Windows (PowerShell), macOS, and Linux
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"

# When prompted for file location, press Enter for default:
# Linux/Mac: ~/.ssh/id_rsa
# Windows: C:\Users\YourName\.ssh\id_rsa

# When prompted for passphrase:
# Enter a passphrase for extra security (recommended)
# Or press Enter for no passphrase (convenient but less secure)

This creates two files:

  • id_rsa — Your private key (keep this safe!)
  • id_rsa.pub — Your public key (this goes to Azure)
⚠️
Never Share Your Private Key Your private key (id_rsa) is like a master password. Never share it, never upload it, never put it in a repository. If it's compromised, anyone can access any server where your public key is installed.

Create VM via Azure Portal

The process is almost identical to creating a Windows VM. Key differences:

Image

Search for Ubuntu Server 22.04 LTS in the image selector.

Authentication Type

Select SSH public key (recommended over password):

  • Username — e.g., azureuser
  • SSH public key source — Choose "Use existing public key"
  • SSH public key — Paste the contents of your id_rsa.pub file
Bash View your public key to copy it
# Linux/Mac
cat ~/.ssh/id_rsa.pub

# Windows PowerShell
Get-Content ~/.ssh/id_rsa.pub

Inbound Port Rules

Select SSH (22) to allow SSH connections.

Create via Azure CLI (Fastest Method)

The CLI method is significantly faster than the portal. Azure CLI can auto-generate SSH keys if you don't have one:

Azure CLI Create a Linux VM with SSH keys
# Create resource group
az group create \
  --name rg-linux-vm-demo \
  --location centralindia

# Create Ubuntu VM with auto-generated SSH keys
# Keys saved to ~/.ssh/id_rsa automatically
az vm create \
  --resource-group rg-linux-vm-demo \
  --name vm-ubuntu-demo \
  --image Ubuntu2204 \
  --size Standard_B2s \
  --admin-username azureuser \
  --generate-ssh-keys \
  --location centralindia \
  --public-ip-sku Standard

# Open SSH port 22
az vm open-port \
  --resource-group rg-linux-vm-demo \
  --name vm-ubuntu-demo \
  --port 22
💡
--generate-ssh-keys This flag automatically creates an SSH key pair if one doesn't exist at ~/.ssh/id_rsa. The public key is installed on the VM. The private key stays on your machine. This is the quickest way to get started.
Azure CLI Get the VM's public IP address
az vm show \
  --resource-group rg-linux-vm-demo \
  --name vm-ubuntu-demo \
  --show-details \
  --query publicIps \
  --output tsv

Connecting via SSH

Once you have the VM's public IP address:

Bash / PowerShell Connect to the Linux VM via SSH
ssh azureuser@<YOUR-VM-PUBLIC-IP>

# Example:
ssh azureuser@4.188.23.45

# If your key is not at the default location:
ssh -i ~/.ssh/id_rsa azureuser@4.188.23.45

The first time you connect, SSH will ask you to verify the host fingerprint:

Output First connection prompt
The authenticity of host '4.188.23.45' can't be established.
ED25519 key fingerprint is SHA256:abc123...
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Type yes and press Enter. You're now connected to your Linux VM running in Azure!

First Commands After Login

Once connected, here are the first commands to run on a new Ubuntu VM:

Bash Essential first commands on a new Ubuntu VM
# Update package lists and upgrade all packages
sudo apt update && sudo apt upgrade -y

# Check OS version
lsb_release -a

# Check CPU and memory
nproc         # Number of CPUs
free -h       # Memory usage
df -h         # Disk usage

# Install useful tools
sudo apt install -y curl wget git unzip htop

# Check if the VM can reach the internet
curl https://api.ipify.org

Cleaning Up

Azure CLI Delete the resource group and all resources
az group delete \
  --name rg-linux-vm-demo \
  --yes \
  --no-wait
📝 Practice Questions
Click an option to check your answer. AZ-104 style questions.
Q1. What port must be open to connect to a Linux VM via SSH?
A Port 22
B Port 3389
C Port 443
D Port 8080
Q2. What is the benefit of using SSH key authentication over password authentication for a Linux VM?
A SSH keys make the VM run faster
B SSH keys reduce the cost of the VM
C SSH keys are more secure and cannot be brute-forced like passwords
D SSH keys require no internet connection to generate
Q3. Why are Linux VMs generally cheaper than Windows VMs of the same size on Azure?
A Linux VMs use older, slower hardware
B Linux has no OS licence fee — Windows includes a paid Microsoft licence cost
C Linux VMs use less storage
D Microsoft offers a discount because Linux is less popular
Q4. When using the Azure CLI to create a Linux VM with --generate-ssh-keys, where is the private key saved?
A In Azure Key Vault
B On your local machine at ~/.ssh/id_rsa
C On the VM at /home/azureuser/.ssh/id_rsa
D In the Azure Resource Group
Q5. Which Linux distribution is recommended as the default choice for most Azure workloads?
A Ubuntu Server 22.04 LTS
B Red Hat Enterprise Linux (RHEL)
C SUSE Linux Enterprise
D Debian
Comments
Disclaimer: RedKite Cloud is an independent educational resource and is not affiliated with, endorsed by, or officially connected to Microsoft Corporation. All product names, logos, and trademarks are property of their respective owners. Content is written independently for educational purposes only.