0
0
AzureHow-ToBeginner · 4 min read

How to Create a Virtual Machine in Azure Quickly

To create a virtual machine in Azure, use the az vm create command with parameters like --resource-group, --name, and --image. This command sets up the VM with the specified OS and configuration in your chosen resource group.
📐

Syntax

The basic command to create a VM in Azure using Azure CLI is:

az vm create --resource-group <resource-group-name> --name <vm-name> --image <image-name> [options]

Explanation of parts:

  • az vm create: Command to create a virtual machine.
  • --resource-group: The name of the resource group where the VM will be created.
  • --name: The name you want to give your VM.
  • --image: The OS image to use, like UbuntuLTS or Win2019Datacenter.
  • [options]: Additional settings like size, admin username, and SSH keys.
bash
az vm create --resource-group MyResourceGroup --name MyVM --image UbuntuLTS --admin-username azureuser --generate-ssh-keys
💻

Example

This example creates an Ubuntu Linux VM named MyVM in the resource group MyResourceGroup. It sets the admin username and generates SSH keys automatically for secure login.

bash
az group create --name MyResourceGroup --location eastus
az vm create --resource-group MyResourceGroup --name MyVM --image UbuntuLTS --admin-username azureuser --generate-ssh-keys
Output
{ "fqdns": "", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/MyVM", "location": "eastus", "name": "MyVM", "powerState": "VM running", "privateIpAddress": "10.0.0.4", "publicIpAddress": "52.170.12.34", "resourceGroup": "MyResourceGroup", "zones": "" }
⚠️

Common Pitfalls

Common mistakes when creating VMs in Azure include:

  • Not creating or specifying a resource group before creating the VM.
  • Using an incorrect or unsupported image name.
  • Forgetting to set admin credentials or SSH keys, which blocks access.
  • Choosing a VM size not available in the selected region.

Always verify your resource group exists and use az vm list-sizes --location <region> to check available sizes.

bash
## Wrong: Missing resource group
az vm create --name MyVM --image UbuntuLTS --admin-username azureuser

## Right: Create resource group first
az group create --name MyResourceGroup --location eastus
az vm create --resource-group MyResourceGroup --name MyVM --image UbuntuLTS --admin-username azureuser --generate-ssh-keys
📊

Quick Reference

ParameterDescription
--resource-groupName of the resource group for the VM
--nameName of the virtual machine
--imageOS image to use (e.g., UbuntuLTS, Win2019Datacenter)
--admin-usernameUsername for the VM admin account
--generate-ssh-keysAutomatically create SSH keys for Linux VMs
--sizeVM size (e.g., Standard_B1s)
--locationAzure region (e.g., eastus)

Key Takeaways

Always create or specify a resource group before creating a VM.
Use the az vm create command with required parameters like resource group, name, and image.
Generate SSH keys or set admin credentials to access your VM securely.
Check available VM sizes in your region to avoid deployment errors.
Use Azure CLI commands step-by-step for smooth VM creation.