Quickstart: Create and Manage a Linux Virtual Machine in Azure

This guide walks you through the essential steps to create, connect to, and manage a Linux virtual machine (VM) in Azure using the Azure CLI.

Prerequisites

Step 1: Log in to Azure

Open your terminal or command prompt and log in to Azure.

az login

This command opens a browser window for you to authenticate your Azure account.

Step 2: Create a Resource Group

Create a resource group to hold your VM and related resources.

az group create --name MyResourceGroup --location eastus

Replace MyResourceGroup with your desired resource group name and eastus with your preferred Azure region.

Step 3: Create a Virtual Machine

Create the Linux VM.

az vm create \ --resource-group MyResourceGroup \ --name MyLinuxVM \ --image Ubuntu2204 \ --admin-username azureuser \ --generate-ssh-keys

This command creates a VM named MyLinuxVM with the Ubuntu 22.04 LTS image. It also creates SSH keys for authentication and sets the admin username to azureuser.

You can choose from various VM images. To see a list of available images, run:

az vm image list --output table

Step 4: Connect to the Virtual Machine

Connect to your VM using SSH.

ssh azureuser@20.118.23.123

Replace azureuser with your admin username and 20.118.23.123 with the public IP address of your VM. The IP address will be displayed after the az vm create command, or you can retrieve it using:

az vm show -d --resource-group MyResourceGroup --name MyLinuxVM --query publicIps --output tsv

Step 5: Manage the Virtual Machine

Once connected, you can manage your Linux VM like any other Linux server.

View VM details:

az vm show --resource-group MyResourceGroup --name MyLinuxVM --show-details --output table

Stop the VM:

az vm stop --resource-group MyResourceGroup --name MyLinuxVM

Start the VM:

az vm start --resource-group MyResourceGroup --name MyLinuxVM

Deallocate the VM (stops billing for compute):

az vm deallocate --resource-group MyResourceGroup --name MyLinuxVM

Step 6: Clean up Resources

Delete the resource group and all its resources when you're done.

az group delete --name MyResourceGroup --yes --no-wait

This will remove the VM, its network interface, disk, and public IP address.

Next Steps