az

Comprehensive Azure CLI (az) command reference for managing Azure resources. Includes commands for resource groups, VMs, storage, networking, and more.

Azure CLI Commands

Azure CLI 2.0 - Essential Commands for Resource Management

The Azure Command-Line Interface (CLI) is a powerful tool for managing Azure resources directly from your terminal. This guide provides a quick reference for essential az commands, covering resource group creation, virtual machine management, storage operations, and networking configurations. Master these commands to streamline your Azure deployments and operations.

Managing Azure Resource Groups

Resource groups are logical containers for your Azure resources. Efficiently create and manage them using the following commands.

# Install Azure CLI 2.0 with one curl command.
curl -L https://aka.ms/InstallAzureCli | bash

# create a resource group named "MyRG" in the 'westus2' region
az group create -n MyRG -l westus2

Virtual Machine (VM) Operations

Deploy, configure, and manage your virtual machines with these core Azure CLI commands.

# create a Linux VM using the UbuntuLTS image,
# with two attached storage disks of 10 GB and 20 GB
az vm create -n MyLinuxVM -g MyRG \
    --ssh-key-value $HOME/.ssh/id_rsa.pub --image UbuntuLTS \
    --data-disk-sizes-gb 10 20

# list VMs
az vm list --output table

# list only VMs having distinct state
az vm list -d --query "[?powerState=='VM running']" --output table

# delete VM (with the name MyLinuxVM in the group MyRG)
az vm delete -g MyRG -n MyLinuxVM --yes

# Delete all VMs in a resource group
az vm delete --ids $(az vm list -g MyRG --query "[].id" -o tsv)

# Create an Image based on a running VM
az vm deallocate -g MyRG -n MyLinuxVM
az vm generalize -g MyRG -n MyLinuxVM
az image create --resource-group MyRG --name MyTestImage --source MyLinuxVM

# Running VM based on a VHD
az storage blob upload --account-name "${account_name}"  \
   --account-key "${account_key}" --container-name "${container}" --type page \
   --file "${file}" --name "${vhd}"
az disk create \
   --resource-group "${resource_group}" \
   --name myManagedDisk \
   --source "https://${account_name}.blob.core.windows.net/${container}/${vhd}"

# open port
az vm open-port --resource-group MyRG --name MyLinuxVM --port 443 --priority 899

Storage Account and Blob Management

Interact with Azure Storage accounts, containers, and blobs for data management and transfer.

# Show storage accounts
az storage account list --output table

# Show containers for an account
az storage container list --account-name mystorageaccount --output table

# Show blobs in a container
az storage blob list --account-name mystorageaccount \
    --container-name mycontainer  --output table

# list account keys
az storage account keys list  --account-name STORAGE_NAME --resource-group RESOURCE_GROUP

# Copy blob
az storage blob copy start \
    --source-uri 'https://xxx.blob.core.windows.net/jzwuuuzzapn0/abcd?...' \
    --account-key XXXXXXXXXX== \
    --account-name destaccount \
    --destination-container vms \
    --destination-blob DESTINATION-blob.vhd

Networking and Disk Operations

Configure virtual networks, network interfaces, public IPs, and manage disk snapshots and attachments.

# Show own images
az image list --output table

# Configure default storage location
az configure --defaults location=eastus2

# Show disks
az disk list --output table

# List virtual networks
az network vnet list --output table

# List virtual networks adapters
az network nic list --output table

# List public IP addresses used by the VMs
az vm list-ip-addresses --output table

# create snapshot
az snapshot create --resource-group IC-EXASOL-001 --source vm1-disk1 -n vm1-snap1

# create SAS url for a snapshot
az snapshot grant-access --resource-group IC-EXASOL-001 --name vm1-snap1\
    --duration-in-seconds 36000 --query '[accessSas]' -o tsv

# attach disk
az vm disk attach --vm-name vm1 -g RESOURCE_GROUP --disk DISK1_ID

# detach disk
az vm disk detach --vm-name vm1 -g RESOURCE_GROUP --name DISK1_ID

Further Resources