Create Azure Virtual Machines using the Azure CLI
This document guides you through the process of creating Azure Virtual Machines (VMs) using the Azure Command-Line Interface (CLI). The Azure CLI provides a powerful and flexible way to manage your Azure resources from your terminal.
- An Azure account with an active subscription. If you don't have one, you can create a free account.
- Azure CLI installed on your local machine or accessible via Azure Cloud Shell.
Step 1: Install and Log In to Azure CLI
If you haven't already, install the Azure CLI. Once installed, log in to your Azure account:
Azure CLI Login Command
az login
This command will open a browser window for you to authenticate with your Azure credentials.
Step 2: Create a Resource Group
All Azure resources must reside within a resource group. A resource group is a logical container. You can create a new one using the following command:
Create a Resource Group
az group create --name MyResourceGroup --location eastus
Replace MyResourceGroup with your desired resource group name and eastus with the Azure region you want to use.
Step 3: Create a Virtual Machine
Now you can create a virtual machine. This command creates a basic Linux VM. You can customize it with various options.
Create a Linux Virtual Machine
az vm create \
--resource-group MyResourceGroup \
--name MyLinuxVM \
--image UbuntuLTS \
--admin-username azureuser \
--generate-ssh-keys
Key parameters:
--resource-group: The name of the resource group created in the previous step.--name: The name for your new VM.--image: The OS image to use. Common options includeUbuntuLTS,CentOS,Debian,Win2019Datacenter.--admin-username: The username for the administrator account on the VM.--generate-ssh-keys: Automatically generates SSH public and private key files if they don't exist in your~/.sshdirectory.
Step 4: Create a Windows Virtual Machine
To create a Windows VM, you'll need to specify a Windows image and provide a password for the administrator account.
Create a Windows Virtual Machine
az vm create \
--resource-group MyResourceGroup \
--name MyWinVM \
--image Win2019Datacenter \
--admin-username azureuser \
--admin-password "YourComplexPassword123!"
Important: Replace "YourComplexPassword123!" with a strong, unique password. For security reasons, it's often recommended to use password management tools or prompt for the password instead of hardcoding it.
Step 5: View Your Virtual Machine
After creation, you can get details about your VM, including its public IP address, which you'll need to connect to it.
Get VM Details
az vm show --resource-group MyResourceGroup --name MyLinuxVM -d --query publicIps -o tsv
This command will output the public IP address of your VM.
Connecting to Your VM
Connecting to a Linux VM (SSH)
Use an SSH client to connect to your Linux VM using the public IP address and the username you specified:
SSH Connection
ssh azureuser@YOUR_VM_PUBLIC_IP_ADDRESS
If you used --generate-ssh-keys, you might not be prompted for a password.
Connecting to a Windows VM (RDP)
You can connect to your Windows VM using Remote Desktop Protocol (RDP). You'll need to ensure that RDP port 3389 is open on the VM's network security group (NSG). The az vm create command typically creates a basic NSG with RDP enabled for Windows VMs by default.
Use your RDP client and the VM's public IP address, along with the admin username and password you provided during creation.
Step 6: Clean Up Resources
Once you are finished with your VM, it's important to clean up the resources to avoid incurring further charges. You can delete the entire resource group:
Delete Resource Group
az group delete --name MyResourceGroup --yes --no-wait
The --yes flag confirms the deletion, and --no-wait allows the command to return immediately while the deletion proceeds in the background.