Last reviewed: August 2026
This guide shows how to create a Linux virtual machine in Microsoft Azure using both the Azure portal and Azure CLI, with current security practices such as SSH keys, Standard public IPs, NSGs, and clean-up guidance.
Prerequisites
- An Azure subscription
- Permission to create resource groups, virtual machines, networking resources, and public IPs
- Azure CLI or Azure Cloud Shell if you want to use commands
Create a Linux VM in the Azure portal
- Open Azure portal → Virtual machines → Create → Azure virtual machine.
- Select the subscription and resource group.
- Provide a VM name and region.
- Choose an image such as Ubuntu Server 22.04 LTS Gen2 or another supported image for your workload.
- Select an appropriate VM size.
- For Linux authentication, select SSH public key instead of a reusable administrator password.
- Use a username such as azureuser.
- Generate a new key pair or use an existing SSH key stored in Azure.
- Under inbound ports, allow only the ports you actually need, such as SSH 22 and HTTP 80 for a test web server.
- Review disk, networking, management, monitoring, and advanced settings.
- Select Review + create, validate the configuration, and create the VM.
Connect to the VM with SSH
chmod 400 ~/Downloads/myKey.pem ssh -i ~/Downloads/myKey.pem azureuser@Create a VM with Azure CLIRG=melochip-vm-rg LOCATION=eastus VM=melochip-web01 USER=azureuser az group create --name $RG --location $LOCATION az vm create --resource-group $RG --name $VM --image Ubuntu2204 --admin-username $USER --generate-ssh-keys --public-ip-sku StandardYou can also use a full image URN for stricter control over the publisher, offer, SKU, and version.
Create a VM inside an existing VNet and subnet
az vm create --resource-group HUBRG01 --name WEB1 --image Ubuntu2204 --location eastus --vnet-name HUBVNET01 --subnet HUBSUBNET01 --admin-username azureuser --generate-ssh-keys --public-ip-sku StandardIf the VM must remain private, omit public exposure and use Azure Bastion, VPN, ExpressRoute, or another approved private-access method.
Check VM status and IP addresses
az vm show --resource-group $RG --name $VM --show-details --output table az vm list-ip-addresses --resource-group $RG --name $VM --output tableNSG and security guidanceDo not hard-code administrator passwords in scripts or articles.Prefer SSH-key authentication for Linux VMs.Limit SSH or RDP source IP ranges whenever possible.Do not open management ports to the entire internet unless absolutely necessary.Use NSGs, guest firewalls, and least privilege together.For production systems, consider Azure Bastion or private connectivity instead of a public management IP.Enable monitoring, patching, backup, and Defender recommendations as required by your environment.Availability optionsAzure offers multiple resiliency options depending on region and workload design, including availability zones, availability sets, and virtual machine scale sets. For new architectures, choose the option that matches your availability requirement rather than automatically placing every VM into an availability set.
Install NGINX for a quick test
sudo apt update sudo apt install -y nginx sudo systemctl enable --now nginxIf HTTP port 80 is allowed in the NSG, browse to the VM's public IP to confirm the web server is reachable.
Common problems
- SSH connection fails: verify the public IP, NSG rule, username, private-key path, and guest firewall.
- VM creation fails: check regional quota, SKU availability, image availability, policy restrictions, and permissions.
- No public IP: confirm whether your design intentionally created a private VM.
- Wrong image: use az vm image list or a full image URN to verify the exact image.
- High cost: resize, stop/deallocate when unused, enable auto-shutdown, or delete the resource group after testing.
Clean up the lab
az group delete --name $RG --yes --no-waitDeleting the resource group removes the VM and the related resources created inside that resource group, so use this only when the lab is no longer needed.
Official references
- Create a Linux VM in the Azure portal
- Create a Linux VM with Azure CLI
- Create and manage SSH keys with Azure CLI
Related Melochip guide: Azure Load Balancer.
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
7 min read · Sep 19, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Sep 17, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Sep 17, 2026Related articles
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Sep 17, 2026
Learn Kubernetes Ingress with controllers, IngressClass, host and path routing, TLS, DNS, annotations, redirec...
8 min read · Sep 17, 2026
Learn Kubernetes ConfigMaps and Secrets with YAML, environment variables, file mounts, update behavior, immuta...
8 min read · Sep 17, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Sep 17, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.