Quickstart: Create a Linux Virtual Machine with Azure CLI

This guide walks you through creating a Linux virtual machine (VM) in Azure using the Azure Command-Line Interface (CLI).

Note: This quickstart requires you to have the Azure CLI installed and logged in. If you haven't already, install the Azure CLI and log in using az login.

1. Create a Resource Group

Azure resources are deployed into a resource group, which is a logical container for them. Use the az group create command to create a resource group. Replace <myResourceGroup> with the name you want to use for your resource group and <eastus> with the desired Azure region.

az group create --name <myResourceGroup> --location <eastus>

2. Create a Virtual Machine

Use the az vm create command to create a virtual machine. This command creates the VM, associated network resources, and a default SSH public key if one doesn't exist.

Replace <myVM> with your desired VM name, <myResourceGroup> with the resource group name you created in the previous step, and <eastus> with the Azure region.

az vm create \
  --resource-group <myResourceGroup> \
  --name <myVM> \
  --image Ubuntu2204 \
  --admin-username azureuser \
  --location <eastus>

The --image parameter specifies the operating system image to use. You can find a list of available images using az vm image list --output table.

SSH Keys: By default, the Azure CLI will generate SSH public and private key files in ~/.ssh if they don't exist. You can specify your own SSH public key file with --ssh-key-values.

3. Connect to the Virtual Machine

Once the VM is created, you can connect to it using SSH. The az vm show command can retrieve the public IP address of your VM.

az vm show -d -g <myResourceGroup> -n <myVM> --query publicIps -o tsv

Then, use the public IP address to connect to your VM:

ssh azureuser@<PublicIpAddress>

If prompted, type yes to continue the connection.

4. Clean Up Resources

When you are finished with the VM, you can delete the resource group and all of its contained resources using the az group delete command.

az group delete --name <myResourceGroup>

To confirm the deletion, type y when prompted.

Next Steps

This quickstart demonstrated the basic steps to create and connect to an Azure VM. For more advanced configurations, explore the following: