Creating Your First Azure Virtual Machine
Create a small Linux VM in the Azure portal and with the Azure CLI, connect safely over SSH, keep it within the free account and clean up afterwards.
Creating a VM in Azure takes about five minutes and one long form. Most of the form can be left at its defaults, but a handful of fields decide whether the VM is covered by your free account, whether SSH is open to the whole internet, and whether deleting the VM actually deletes everything it created.
This article builds one small Linux VM twice: first in the Azure portal, explaining each tab of the create form, then with the Azure CLI so you can repeat it. It also covers the ways to connect, what each power state costs, and how to clean up so nothing keeps billing you.
What gets created
An Azure VM is never a single resource. When you create one, Azure creates or reuses several others, each billed and deleted on its own terms:
- A resource group: the container everything goes into. Deleting it deletes everything inside.
- The VM: the compute part, with a size such as
Standard_B1sthat fixes its vCPUs and memory. - An OS disk: a managed disk built from the image you choose.
- A network interface (NIC): connects the VM to a subnet and holds its private IP address.
- A virtual network (VNet) and subnet: created for you if the region has none you want to use.
- A network security group (NSG): a stateful firewall attached to the NIC.
- A public IP address: a separate resource associated with the NIC, so the VM is reachable from the internet.
flowchart TB
subgraph RG["Resource group: rg-first-vm"]
subgraph VNet["Virtual network"]
subgraph Sub["Subnet: default"]
NIC["Network interface"]
end
end
VM["VM: Standard_B1s"]
Disk["OS disk: managed disk"]
NSG["Network security group"]
PIP["Public IP: Standard SKU"]
end
IMG["Image: Ubuntu Server 24.04 LTS"] -->|"copied to"| Disk
Disk --- VM
VM --- NIC
NSG -->|"filters traffic to"| NIC
PIP --- NIC
You["You"] -->|"SSH, port 22"| PIP
Compared with the other two big clouds, the NSG sits closest to an AWS security group: it attaches to a NIC (or a whole subnet) rather than living on the network and matching VMs by tag, as Google Cloud firewall rules do. Navigating the Azure Portal explains resource groups and how the portal organizes them.
Before you start
A subscription. Every resource belongs to a subscription. A new Azure free account gives you a credit for the first 30 days and 12 months of selected free services, including a monthly allowance of small VM hours. Azure Free Tier and Billing Overview explains how the credit, the spending limit and the free services interact, and how to set a budget alert before you build anything.
Permission to create VMs. If you created the subscription you are its Owner. In a shared subscription, the Virtual Machine Contributor role on a resource group lets you manage VMs, but creating the network pieces also needs network permissions, so Contributor on the resource group is the simpler choice for a lab. Azure Identity and Access Management (Microsoft Entra ID) covers how roles and scopes work.
A region. Pick one close to you that offers the size you want. Free accounts often have small vCPU quotas per region and per VM family, so a size that is greyed out in one region may be available in another. Azure Global Infrastructure: Regions and Availability Zones covers how regions and zones affect a VM.
The Azure CLI, for the second half. Azure Cloud Shell, opened from the terminal icon in the portal header, has it installed and signed in. An ephemeral session needs no storage account. To work locally instead, install the Azure CLI and run az login.
Staying inside the free services
The free VM allowance only applies when several settings line up. The create form doesn’t warn you when they don’t, and its price estimate shows the normal price either way.
| Setting | Free services need | Common mistake |
|---|---|---|
| Size | B1s, B2pts v2 (Arm) or B2ats v2 (AMD) | Accepting the suggested size, often a D-series or B2s, billed in full |
| OS disk | Premium SSD at the P6 size (64 GiB) | A Standard SSD disk, or a Windows image’s default 127 GiB disk, which falls in a larger, paid tier |
| Hours | One allowance shared by all eligible VMs | Running two VMs around the clock and assuming both are free |
| Public IP and traffic | Not part of the VM allowance | Forgetting that the public IP and outbound data are metered separately |
The exact terms of the offer change from time to time, so check the free services list in your subscription’s portal view before you rely on it. The subscription overview shows how much of each allowance you have used.
All three free sizes are burstable B-series machines with 1 GiB of memory. They run at a low CPU baseline, bank credits while idle and spend them to burst. That is fine for a small web server or a lab, and painful for builds or databases. B2pts v2 runs on Arm processors, so it needs an Arm64 image; choose B1s or B2ats v2 if you want the ordinary x64 images.
You create a B1s VM from a Windows Server image and accept every disk default. Is the whole VM covered by the free services?
Correct! The size can be free while the disk is billed; the free disk allowance is for 64 GiB P6 Premium SSDs.
Creating the VM in the portal
In the portal, search for Virtual machines, then choose Create and Virtual machine. The form has tabs across the top: Basics, Disks, Networking, Management, Monitoring, Advanced and Tags. You can choose Review + create from any tab, so the tabs you skip keep their defaults. Here is what each one does and what to pick for a first Linux server.
Basics
Project details:
- Subscription: the one you want to bill.
- Resource group: choose Create new and call it
rg-first-vm. Putting a lab in its own group is what makes cleanup a single step later.
Instance details:
- Virtual machine name:
first-vm. Azure also uses it as the computer name inside the OS. - Region: the region you picked.
- Availability options: No infrastructure redundancy required. Availability zones and scale sets matter for production, not for one lab VM.
- Security type: leave Trusted launch virtual machines. It turns on Secure Boot and a virtual TPM, which protect the boot chain and cost nothing extra.
- Image: Ubuntu Server 24.04 LTS - x64 Gen2. Older guides, including an earlier version of this article, used Ubuntu 20.04, which left standard support in 2025. For Windows, choose Windows Server 2025 Datacenter: Azure Edition rather than Windows Server 2019, which is past mainstream support.
- VM architecture: x64.
- Size: choose See all sizes, search for
B1sand select Standard_B1s.
Administrator account:
- Authentication type: SSH public key. Password logins on an internet-facing VM attract constant guessing; a key can’t be guessed.
- Username:
azureuser, or any name that isn’t reserved, such asrootoradmin. - SSH public key source: Generate new key pair, with a key pair name such as
first-vm_key. If you already have a key, choose Use existing public key and paste the contents of your.pubfile.
Inbound port rules: Allow selected ports, with SSH (22). The form warns that this opens the port to the internet, and it does: the rule it creates accepts SSH from any source. You’ll narrow it after creation, in the connection section below. For a Windows VM the equivalent is RDP (3389), with the same warning.
Disks
- OS disk size: set it to 64 GiB (P6) so the disk matches the free allowance. Linux images default to a smaller size, which is billed as a different disk tier.
- OS disk type: Premium SSD. Standard SSD costs less outside the free account, but the free allowance is for Premium SSD.
- Delete with VM: leave it ticked. When you delete the VM, the OS disk goes with it.
You can add data disks here, but a first VM doesn’t need one. A managed disk can be made larger later, never smaller. Understanding Azure Virtual Machines compares the disk types and when each is worth paying for.
Networking
- Virtual network and subnet: the form proposes a new VNet named after the resource group, with a
defaultsubnet. Keep it. - Public IP: a new Standard SKU address. The older Basic SKU was retired in 2025, so guides that mention it are out of date. A Standard public IP is closed to inbound traffic unless an NSG rule allows it.
- NIC network security group: Basic, which creates an NSG for the NIC from the inbound port choice on the Basics tab. Advanced lets you pick or build an NSG yourself.
- Delete public IP and NIC when VM is deleted: tick it, so deleting the VM doesn’t leave a billed public IP behind.
Outbound internet access needs a word too. Azure used to give every VM a hidden “default outbound access” address. For virtual networks created with the API versions released after 31 March 2026, which the portal and CLI now use, that no longer happens by default: subnets are private, and a VM needs a public IP, a NAT gateway or a load balancer to reach the internet. This VM has a public IP, so apt update works. A VM you create without one, for example to reach only through Bastion, will need a NAT gateway before it can download packages. The default outbound access page has the details.
Management
- Microsoft Defender for Cloud: a lab doesn’t need a paid Defender plan.
- Identity: a system-assigned managed identity gives software on the VM an Entra ID identity to call Azure services without stored secrets. A lab that calls nothing doesn’t need one.
- Login with Microsoft Entra ID: lets you sign in to the VM with your Entra account and control access with Azure roles instead of keys. Worth trying once you have the basics working.
- Auto-shutdown: turn it on and pick a time in the evening. The VM is then deallocated every day at that time, so a forgotten lab costs you hours, not weeks.
Monitoring, Advanced and Tags
- Monitoring: leave Boot diagnostics on, with a managed storage account. It gives you a screenshot and log of the boot, and it is what the serial console uses.
- Advanced: the Custom data box takes a cloud-init file or script, run on the first boot only. That differs from Google Cloud startup scripts, which run on every boot.
- Tags: add something like
purpose = lab. Tags help you find and group costs later.
This cloud-init file, pasted into Custom data, installs nginx:
#cloud-config
package_update: true
packages:
- nginx
Serving it also needs port 80 open. Add HTTP (80) to the inbound ports on the Basics tab, or add a rule afterwards.
Review and create
Review + create validates the form and shows the price per hour for the size you chose, without the free account discount. Choose Create. If you generated a key pair, the portal now offers Download private key and create resource. Download it: Azure does not keep the private key and you can’t fetch it again.
Deployment takes a minute or two. Choose Go to resource when it finishes. The overview page shows the public IP address and the status Running.
Connecting to the VM
The VM page has a Connect menu that lists the options and checks whether your network rules allow them.
| Method | Needs a public IP | Needs port 22 or 3389 open | Notes |
|---|---|---|---|
| Native SSH or RDP client | Yes | Yes, ideally only from your IP | The usual way; nothing extra to deploy |
| Azure Bastion | No | No, only from Bastion to the VM | Browser-based; a free Developer SKU exists in some regions, other SKUs are billed |
| Serial console | No | No | Text console through boot diagnostics, for when the network or SSH is broken |
az ssh vm with Entra ID login | Yes, unless used with Bastion | Yes | Access granted with Azure roles instead of keys |
SSH from your machine
Lock the private key down, then connect with the VM’s public IP:
chmod 400 ~/Downloads/first-vm_key.pem
ssh -i ~/Downloads/first-vm_key.pem azureuser@<public-ip>
On Windows, the built-in OpenSSH client in PowerShell takes the same command; set the key file so only your account can read it, or SSH refuses to use it.
Narrow the SSH rule
The rule the form created for port 22 allows any source. Open the VM’s Networking page, select the rule and change Source to My IP address. The portal fills in your current public address. Every SSH scanner on the internet stops reaching the VM, and you can still connect.
If your address changes often, Azure Bastion is the cleaner fix: delete the inbound SSH rule entirely and connect through the browser instead. Bastion reaches the VM on its private IP, so the VM doesn’t even need a public IP for administration.
sequenceDiagram
participant You as Your browser
participant Portal as Azure portal
participant Bastion as Azure Bastion
participant VM as VM (private IP)
You->>Portal: Connect, then Bastion
Portal->>Portal: Check your Azure role on the VM
Portal->>Bastion: Open session
Bastion->>VM: SSH to the private IP
VM-->>You: Terminal in the browser
You chose "Allow selected ports" with SSH (22) and changed nothing afterwards. Who can reach port 22 on the VM's public IP?
Correct! A Standard public IP is closed by default, but the NSG rule the form created opens port 22 to every source.
The same VM with the Azure CLI
The portal is the best way to see the options. The CLI is how you make the setup repeatable. These commands build the same VM in Cloud Shell or any signed-in terminal. Save the cloud-init file from earlier as cloud-init.yaml first.
az group create --name rg-first-vm --location canadacentral
az vm create \
--resource-group rg-first-vm \
--name first-vm \
--image Ubuntu2404 \
--size Standard_B1s \
--admin-username azureuser \
--generate-ssh-keys \
--os-disk-size-gb 64 \
--storage-sku Premium_LRS \
--custom-data cloud-init.yaml \
--tags purpose=lab
Ubuntu2404is an image alias the CLI resolves toCanonical:ubuntu-24_04-lts:server:latest. Older guides useUbuntuLTS, which no longer exists. For images without an alias, pass the full URN, for exampleMicrosoftWindowsServer:WindowsServer:2025-datacenter-azure-edition:latest, which also needs--admin-passwordinstead of SSH keys.--generate-ssh-keysuses~/.ssh/id_rsa.pubif it exists and creates a key pair if it doesn’t. In Cloud Shell, the key lives in the Cloud Shell home directory, which an ephemeral session throws away.- On a Linux image,
az vm createalso creates an NSG namedfirst-vmNSGwith a rule allowing SSH from any source, a Standard public IP, a VNet and a subnet. The command prints the public IP when it finishes.
Here is what happens behind that one command:
sequenceDiagram
participant CLI as az vm create
participant ARM as Azure Resource Manager
participant Net as Network resources
participant Comp as Compute
CLI->>ARM: Submit deployment
ARM->>Net: Create VNet, subnet, NSG, public IP, NIC
Net-->>ARM: Done
ARM->>Comp: Create disk from image and start VM
Comp-->>ARM: VM running
ARM-->>CLI: Public IP, private IP, power state
Now narrow SSH to your address, open port 80 for nginx, and set the daily shutdown:
MY_IP=$(curl -s https://ifconfig.me)
az network nsg rule update \
--resource-group rg-first-vm \
--nsg-name first-vmNSG \
--name default-allow-ssh \
--source-address-prefixes "$MY_IP/32"
az vm open-port --resource-group rg-first-vm --name first-vm \
--port 80 --priority 1010
az vm auto-shutdown --resource-group rg-first-vm --name first-vm --time 0200
az vm open-port creates a rule allowing the port from any source, which is what a public web server wants and what an admin port doesn’t. The auto-shutdown time is in UTC and written as HHMM, so 0200 is an evening shutdown for most of North America. Check the rules with:
az network nsg rule list --resource-group rg-first-vm --nsg-name first-vmNSG --output table
Once cloud-init has finished, http:// and the public IP shows the nginx welcome page. If it doesn’t, connect over SSH and read /var/log/cloud-init-output.log.
For anything you build more than a few times, describe it in Bicep or Terraform so the VM and its network are created and deleted as one unit.
Power states and what they cost
Azure has two kinds of “stopped”, and only one of them stops the compute charge.
stateDiagram-v2
[*] --> Running: Create or start
Running --> Stopped: Shut down inside the OS, or az vm stop
Running --> Deallocated: Stop in portal, az vm deallocate, auto-shutdown
Stopped --> Deallocated: az vm deallocate
Stopped --> Running: Start
Deallocated --> Running: Start
Deallocated --> [*]: Delete
| State | Compute billed | Disk billed | Public IP |
|---|---|---|---|
| Running | Yes | Yes | Kept and billed |
| Stopped (still allocated) | Yes | Yes | Kept and billed |
| Stopped (deallocated) | No | Yes | Standard public IPs are static, so the address is kept and still billed |
| Deleted | No | Only disks you chose to keep | Deleted if “Delete with VM” was ticked |
The portal’s Stop button deallocates, and so does auto-shutdown. Running sudo shutdown now inside the VM, or az vm stop, leaves it allocated and still billed for compute. From the CLI, az vm deallocate is the one you want for a lab. Understanding Azure Virtual Machines goes further into power states, availability options and the ways to pay.
You finish for the day and run sudo shutdown now over SSH. The portal shows the VM as Stopped. What are you paying for overnight?
Correct! Shutting down from inside the OS leaves the VM allocated; only Stop in the portal or az vm deallocate ends the compute charge.
Cleaning up
Because everything went into one resource group, one command removes the VM, its disk, NIC, NSG, public IP and VNet:
az group delete --name rg-first-vm --yes
In the portal, open the resource group and choose Delete resource group; you type its name to confirm. Then check for anything that escaped:
az resource list --output table
You may see a resource group called NetworkWatcherRG. Azure creates it automatically, with a Network Watcher instance, the first time you create a virtual network in a region. It doesn’t charge for simply existing, and you can leave it.
If you deleted only the VM instead of the group, look for leftovers on the Disks and Public IP addresses pages. Any disk or public IP that wasn’t set to delete with the VM keeps billing until you remove it.
How this compares with AWS and Google Cloud
| Azure | AWS EC2 | Google Compute Engine | |
|---|---|---|---|
| Firewall | NSG on the NIC or subnet | Security group on the instance | VPC firewall rules matched by tag or service account |
| Free VM sizes | B1s, B2pts v2, B2ats v2 for 12 months | Credit-based Free Tier for new accounts | One e2-micro in three US regions, always free |
| Stop that ends compute billing | Deallocate | Stop | Stop |
| First-boot script | Custom data with cloud-init, runs once | User data, runs once by default | Startup script, runs every boot |
| Browser access | Azure Bastion | EC2 Instance Connect, Session Manager | SSH-in-browser, IAP |
If you’ve built a VM elsewhere, Creating Your First AWS EC2 Instance and Launching Your First GCP Compute Engine Instance walk through the same exercise on the other two clouds.
Further reading
- Quickstart: create a Linux VM in the Azure portal
- Quickstart: create a Linux VM with the Azure CLI
- Quickstart: create a Windows VM in the Azure portal
- Network security groups
- Azure Bastion
- Linux virtual machines pricing
Key takeaways
- An Azure VM brings a disk, NIC, NSG, public IP and VNet with it; put a lab in its own resource group so one delete removes them all.
- The free allowance needs an eligible B-series size and a 64 GiB P6 Premium SSD OS disk, and its hours are shared across all your VMs.
- Use SSH keys, and narrow the SSH rule to your own IP or replace it with Azure Bastion; the default rule allows any source.
- Pick current images such as Ubuntu Server 24.04 LTS or Windows Server 2025, and the
Ubuntu2404alias in the CLI. - Deallocate, don’t just shut down: only Stop in the portal,
az vm deallocateor auto-shutdown ends the compute charge. - Tick the “delete with VM” options for the disk, NIC and public IP, and check for leftovers after deleting.
Part 6 of 7 · Diving into Microsoft Azure
- 01Introduction to Microsoft Azure
- 02Navigating the Azure Portal
- 03Azure Global Infrastructure: Regions and Availability Zones
- 04Mastering Microsoft Entra Identity and Access Management
- 05Azure Free Tier and Billing Overview
- 06Creating Your First Azure Virtual Machine
- 07Azure Support and Documentation Resources