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, likeUbuntuLTSorWin2019Datacenter.[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-keysExample
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-keysOutput
{
"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-keysQuick Reference
| Parameter | Description |
|---|---|
| --resource-group | Name of the resource group for the VM |
| --name | Name of the virtual machine |
| --image | OS image to use (e.g., UbuntuLTS, Win2019Datacenter) |
| --admin-username | Username for the VM admin account |
| --generate-ssh-keys | Automatically create SSH keys for Linux VMs |
| --size | VM size (e.g., Standard_B1s) |
| --location | Azure 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.