Azure Virtual Machines
A virtual machine gives you a full computer in the cloud, with complete control over the operating system.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 29, 2026
Related tutorials
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#
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]
{
"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#
- az vm create provisions the VM, a disk, a network interface, a public IP and an NSG.
- --generate-ssh-keys creates a key pair so no password is needed.
- cloud-init installs Nginx automatically on first boot.
- 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#
Bots scan these ports constantly. Restrict to your IP or use Azure Bastion.
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
az vm deallocate -g rg-learn -n vm-web
az vm start -g rg-learn -n vm-web
Mini quiz#
Which statement about Azure VMs is true?
Want more? Practise all Microsoft Azure MCQs · Microsoft Azure interview questions
My notes#
Saved in this browser.