Azure CLI Commands - Manage Azure Resources

Learn essential Azure CLI commands for managing Azure resources, including VM creation, storage management, and image deployment. Optimize your cloud operations.

Azure CLI Commands

This page provides a collection of essential Azure Command Line Interface (CLI) commands to help you manage your Azure resources efficiently. The Azure CLI is a powerful tool for interacting with Azure services from your command line.

Azure CLI Installation and Login

Before you can use the Azure CLI, you need to install it and log in to your Azure account. Here are the basic commands for installation and different login methods.

# Install Azure CLI (example for Debian/Ubuntu)
sudo apt-get update
sudo apt-get install -y ca-certificates curl apt-transport-https lsb-release gnupg
curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null
AZ_REPO=$(lsb_release -cs)
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list
sudo apt-get update
sudo apt-get install -y azure-cli

# Login to Azure account (opens a browser for authentication)
azure login

# Login with organizational ID email address
azure login -u <your organizational ID email address>

# Login using a service principal
azure login -u "<principal>" -p "<key>" --service-principal --tenant "<tenant>"

Virtual Machine Management

Manage your Azure Virtual Machines, including listing available images, creating new VMs, and managing their disks.

VM Image and Creation

# Find images with "Linux" in their name
azure vm image list | grep "Linux"

# Start a new VM (in ASM mode, older syntax)
azure vm create ${vm_name} ${image_name} -u ${azureuser} \
  -p "${password}" -z "Small" -e -l "West US"

VM Disk Management

# List VM disks
azure vm disk list

# Remove all disks of VMs labeled with "LABEL"
for disk in $(azure vm disk list | grep LABEL | awk '{print $2}')
do
  azure vm disk delete --blob-delete "$disk"
done

Storage Account Operations

Create and configure Azure Storage Accounts, which are essential for storing blobs, files, queues, and tables.

# Create a storage account
azure storage account create ${stor_acc} \
  --label ${stor_acc} --location 'West US'

# Set default account environment variables for easier access
export AZURE_STORAGE_ACCOUNT=$account_name
export AZURE_STORAGE_ACCESS_KEY=$account_key

Image Management and Sharing

Create custom VM images from storage files and share them using shared access signatures (SAS).

Create VM Image

# Create a VM image based on a storage file
azure vm image create ${image_name} --os linux  -l 'West Europe' \
  --blob-url https://${stor_acc}.blob.core.windows.net:443/vms/${image}.vhd -v

Share Files with SAS

# Share a file (e.g., a VM image) with the world using a SAS token
azure storage blob sas create -a ${stor_acc} \
  --container ${container} --blob ${filename} --permissions r

Downloading Files from Blob Storage

Download files from Azure Blob Storage using tools like curl or wget. This requires constructing the correct authorization header.

# Download a file from a blob storage using curl/wget
# Values for header variables (like x_ms_date_h, x_ms_version_h, authorization_header)
# can be calculated using specific Azure Storage SDK logic or helper scripts.
# Refer to Azure Storage documentation for precise header construction.
# Example structure:
# curl -v \
#   -H "$x_ms_date_h" \
#   -H "$x_ms_version_h" \
#   -H "$authorization_header" \
# "$URL"

Further Resources

For more in-depth information and advanced usage, please refer to the official Azure CLI documentation: