Azure CLI Basics for Virtual Machines

This guide covers the fundamental Azure CLI commands you'll need to manage Azure Virtual Machines (VMs).

Prerequisites

Before you begin, ensure you have the following:

Logging In and Out

To interact with Azure resources, you first need to log in:

az login

This command opens a browser window for you to authenticate. After successful authentication, you can list your subscriptions:

az account list --output table

To set a specific subscription as active:

az account set --subscription "Your Subscription Name or ID"

To log out:

az logout

Resource Groups

Resource groups are logical containers for your Azure resources. It's best practice to create a resource group for each project or application.

Create a Resource Group

az group create --name MyResourceGroup --location eastus

Replace MyResourceGroup with your desired name and eastus with your preferred Azure region.

List Resource Groups

az group list --output table

Delete a Resource Group

Caution: This will delete all resources within the group.

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

Virtual Machines (VMs)

Let's explore some basic VM operations.

List VMs

To list all VMs in your current subscription:

az vm list --output table

To list VMs within a specific resource group:

az vm list --resource-group MyResourceGroup --output table

Get VM Details

Retrieve detailed information about a specific VM:

az vm show --resource-group MyResourceGroup --name MyVM --show-details

Replace MyVM with the name of your virtual machine.

Start, Stop, and Restart VMs

Start VM:
az vm start --resource-group MyResourceGroup --name MyVM
Stop VM (Deallocate):
az vm stop --resource-group MyResourceGroup --name MyVM
Restart VM:
az vm restart --resource-group MyResourceGroup --name MyVM
Restart VM (Fast):
az vm restart --resource-group MyResourceGroup --name MyVM --no-wait
Note: Stopping a VM deallocates its resources, which stops billing for compute costs. A 'deallocated' VM is powered off. A 'stopped' VM is powered off but still consumes some resources and incurs charges.

Delete a VM

Caution: This deletes the VM and its associated OS disk. Data disks may need to be deleted separately.

az vm delete --resource-group MyResourceGroup --name MyVM --yes

Working with Disks

VMs require disks for the operating system and potentially for data. You can manage these disks as separate resources.

List Disks

az disk list --resource-group MyResourceGroup --output table

Show Disk Details

az disk show --resource-group MyResourceGroup --name MyVM_OsDisk_1_xxxxxxxx --output table

You can find the exact disk name from the output of az vm show or az disk list.

Delete a Disk

Caution: Ensure this disk is not attached to any active VM.

az disk delete --resource-group MyResourceGroup --name MyVM_OsDisk_1_xxxxxxxx --yes

Connecting to a Linux VM

Use SSH to connect to your Linux VMs.

ssh username@public_ip_address

You can retrieve the public IP address using:

az vm show --resource-group MyResourceGroup --name MyVM --show-details --query publicIps --output tsv

Connecting to a Windows VM

Use RDP (Remote Desktop Protocol) to connect. You'll need the public IP address and your VM's administrator credentials.

mstsc /v:public_ip_address
Tip: For more advanced operations like creating custom images, managing extensions, or working with availability sets and scale sets, refer to the specific sections of the Azure documentation.
Important: Always refer to the official Azure documentation for the most up-to-date command syntax and available options, as the Azure CLI is regularly updated.