Microsoft Azure · Compute

Azure Virtual Machines

A virtual machine gives you a full computer in the cloud, with complete control over the operating system.

Beginner 5 min Lesson 7 of 28
0/28 in Microsoft Azure
On this page · 5 min · Beginner

Learning objectives#

  • Create a Linux VM with the CLI
  • Connect securely with SSH
  • Open only the ports you need
  • Know when a VM is the right choice

Explanation#

A VM is IaaS: you choose the size (CPU and RAM), image (Ubuntu, Windows Server…) and disks, and you are responsible for patching and securing the OS.

Sizes come in families: B-series (burstable, cheap for light workloads), D-series (general purpose), E-series (memory optimised) and more.

Network security groups (NSGs) control which ports are reachable. Open only what you need — SSH from your IP, HTTP/HTTPS for web traffic.

Use VMs for software that needs full OS control. For ordinary web apps, App Service or Container Apps are less work.

Code example: A small Ubuntu VM running Nginx#

ShellA small Ubuntu VM running Nginx Illustrative
az vm create --resource-group rg-learn --name vm-web \
  --image Ubuntu2204 --size Standard_B1s \
  --admin-username azureuser --generate-ssh-keys \
  --custom-data cloud-init.txt         # installs nginx on first boot


az vm open-port --resource-group rg-learn --name vm-web --port 80
az vm auto-shutdown --resource-group rg-learn --name vm-web --time 1900


ssh azureuser@<public-ip>


# cloud-init.txt
#cloud-config
packages: [nginx]
Expected output
{
  "powerState": "VM running",
  "publicIpAddress": "20.xxx.xxx.xxx",
  "resourceGroup": "rg-learn"
}

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. az vm create provisions the VM, a disk, a network interface, a public IP and an NSG.
  2. --generate-ssh-keys creates a key pair so no password is needed.
  3. cloud-init installs Nginx automatically on first boot.
  4. open-port allows HTTP; auto-shutdown stops the VM every evening to save money.

Real-world use case#

Running legacy software that needs a specific OS configuration, a self-hosted build agent, or a jump box for administration.

Common mistakes#

Opening SSH (22) or RDP (3389) to the whole internet

Bots scan these ports constantly. Restrict to your IP or use Azure Bastion.

Leaving VMs running

A stopped (deallocated) VM does not bill for compute; a running idle one does.

Best practices#

  • Use SSH keys, not passwords
  • Restrict management ports or use Bastion
  • Enable auto-shutdown for dev VMs
  • Prefer PaaS when you do not need OS control

Remember this#

VM = IaaS = you patch the OS.

Open the fewest ports possible.

Deallocate when not in use — stop paying for compute.

Practice exercise#

Your task: Stop (deallocate) and later start vm-web from the CLI.

Show solution
Shell Illustrative
az vm deallocate -g rg-learn -n vm-web
az vm start -g rg-learn -n vm-web

Mini quiz#

Azure Virtual Machines quizMultiple choice Question 1 of 1

Which statement about Azure VMs is true?

Score so far: 0/0

Want more? Practise all Microsoft Azure MCQs · Microsoft Azure interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.