Microsoft Docs

Quickstart: Create an Azure virtual machine with the Azure CLI

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

Note: This article is for creating a VM with the Azure CLI. For creating a Windows VM, see Quickstart: Create a Windows virtual machine with the Azure CLI.

Prerequisites

Sign in to Azure

Sign in to your Azure account using the Azure CLI:

az login

The CLI will open your default browser to authenticate your account.

Create a resource group

A resource group is a logical container into which Azure resources are deployed and managed. Use az group create to create a resource group. Replace myResourceGroup with a name for your resource group and eastus with the desired location.

az group create --name myResourceGroup --location eastus

The output will be in JSON format, detailing the resource group:

{
  "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup",
  "location": "eastus",
  "managedBy": null,
  "name": "myResourceGroup",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}

Create a virtual machine

Use the az vm create command to create a VM. This command creates a Linux VM named myVM in the myResourceGroup resource group.

The following command creates a VM with the latest Ubuntu LTS image. You can specify other images using the --image parameter.

az vm create \
  --resource-group myResourceGroup \
  --name myVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys

The command will output JSON information about the VM, including its public IP address.

Note: If you don't want to use SSH keys, you can use passwords with the --admin-password parameter. However, SSH keys are generally more secure.

Connect to the virtual machine

Once the VM is created, you can connect to it using SSH. Replace <publicIpAddress> with the public IP address of your VM, which you can find in the output of the az vm create command.

ssh azureuser@<publicIpAddress>

When prompted, type yes to continue connecting if this is the first time. You will then be prompted for the SSH key passphrase if you set one. If you used a password, you'll be prompted for that.

Tip: If you generated SSH keys without a passphrase, you won't be prompted for one.

Clean up resources

When you no longer need the VM and its associated resources, you can delete the resource group. This will remove the VM and all other resources that were deployed into that resource group.

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

The --yes parameter confirms that you want to delete the resource group, and --no-wait allows the command to return immediately without waiting for the deletion to complete.

Next Steps