How to Manage Azure Virtual Machines with the 'az vm' Command (with examples)

How to Manage Azure Virtual Machines with the 'az vm' Command (with examples)

The az vm command is part of the Azure Command-Line Interface (CLI), a powerful tool for managing Azure resources directly from the terminal or command prompt. The az vm command is specifically used to manage virtual machines in Azure, allowing you to create, modify, and control VMs efficiently. In this article, we will explore several essential use cases of the az vm command with detailed examples and explanations.

Display a Table of Available Virtual Machines

Code:

az vm list --output table

Motivation: This command is useful for quickly checking the current state of all virtual machines in your Azure subscription, especially when you are managing a large inventory of VMs. A table format provides a concise, human-readable overview that can help in operational decision-making or auditing tasks.

Explanation:

  • az vm list: This portion of the command lists all the virtual machines available in your Azure subscription.
  • --output table: This argument specifies that the output should be formatted as a table. The table format makes it easier to read and analyze data compared to other formats like JSON or YAML.

Example Output:

Name       ResourceGroup    Location    VM Size    ProvisioningState    PowerState
---------  ---------------  ----------  ---------  -------------------  -----------
vm1        myResourceGroup  eastus      Standard_B2s  Succeeded          VM running
vm2        testGroup        westus      Standard_DS1 StillBeingCreated

Create a Virtual Machine using the Default Ubuntu Image and Generate SSH Keys

Code:

az vm create --resource-group rg --name vm_name --image UbuntuLTS --admin-user azureuser --generate-ssh-keys

Motivation: Creating a new virtual machine is a fundamental task for users who wish to deploy applications or perform testing in a cloud environment. This example also demonstrates how to automatically generate SSH keys, ensuring secure access without manual key management.

Explanation:

  • az vm create: Initiates the creation of a new virtual machine in Azure.
  • --resource-group rg: Specifies the resource group (rg) in which the VM should be created. Resource groups help in organizing Azure resources.
  • --name vm_name: Defines the name of the new virtual machine as vm_name.
  • --image UbuntuLTS: Indicates the Ubuntu Linux Long-Term Support image as the base for the VM, which is a common choice for server deployments due to its stability and support.
  • --admin-user azureuser: Sets azureuser as the administrator username for the VM.
  • --generate-ssh-keys: Automatically generates SSH keys for secure and encrypted connections without requiring manual key handling.

Example Output:

{
  "fqdns": "",
  "id": "/subscriptions/.../resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm_name",
  "location": "eastus",
  "macAddress": "00-0D-3A-B0-39-08",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "13.92.224.125",
  "resourceGroup": "rg"
}

Stop a Virtual Machine

Code:

az vm stop --resource-group rg --name vm_name

Motivation: Stopping a virtual machine is crucial when you wish to save on billing costs associated with running VMs or need to temporarily halt operations for maintenance or troubleshooting purposes.

Explanation:

  • az vm stop: Specifies the stop operation for a virtual machine.
  • --resource-group rg: Specifies the resource group (rg) containing the VM to be stopped.
  • --name vm_name: Specifies the particular VM to stop by name (vm_name).

Example Output:

Stopping VM 'vm_name' in resource group 'rg' and deallocating related resources...

Deallocate a Virtual Machine

Code:

az vm deallocate --resource-group rg --name vm_name

Motivation: Deallocating a VM is important when you wish to release the VM’s resources, such as the IP address, without deleting the VM, thus further reducing costs since deallocated VMs are not billed.

Explanation:

  • az vm deallocate: Deallocates the VM, freeing its associated compute resources.
  • --resource-group rg: Indicates the resource group that includes the VM to be deallocated.
  • --name vm_name: Specifies the name of the VM that needs to be deallocated.

Example Output:

Deallocating VM 'vm_name' in resource group 'rg'.

Start a Virtual Machine

Code:

az vm start --resource-group rg --name vm_name

Motivation: Starting a VM is necessary to bring it back online after it has been stopped or deallocated. This is a common operation when it’s time to resume the use of services or applications hosted on that VM.

Explanation:

  • az vm start: Initiates the start operation for the specified virtual machine.
  • --resource-group rg: Specifies the resource group where the VM is found.
  • --name vm_name: Indicates the name of the VM to be started.

Example Output:

Starting VM 'vm_name' in resource group 'rg'.

Restart a Virtual Machine

Code:

az vm restart --resource-group rg --name vm_name

Motivation: Restarting a VM is often needed after applying updates or settings that require a reboot to take effect. It is also a useful action to resolve certain issues without changing the VM’s configuration.

Explanation:

  • az vm restart: Executes a restart operation on the virtual machine.
  • --resource-group rg: Identifies the resource group containing the VM.
  • --name vm_name: Specifies the name of the VM to be restarted.

Example Output:

Restarting VM 'vm_name' in resource group 'rg'.

List VM Images Available in the Azure Marketplace

Code:

az vm image list

Motivation: Listing available VM images helps users explore and identify suitable operating systems and configurations for their new VMs, speeding up the deployment process.

Explanation:

  • az vm image list: Retrieves a list of all VM images offered in the Azure Marketplace, giving users a comprehensive view of available options for their deployments.

Example Output:

[
  {
    "offer": "UbuntuServer",
    "publisher": "Canonical",
    "sku": "18.04-LTS",
    "version": "latest"
  },
  {
    "offer": "CentOS",
    "publisher": "OpenLogic",
    "sku": "7.9",
    "version": "latest"
  }
  ...
]

Conclusion:

The az vm command is a versatile and essential tool for managing virtual machines within Azure. Whether you are listing VMs, stopping them to save costs, or exploring available VM images, az vm provides an efficient and effective way to control your Azure environment from the command line. By understanding and utilizing these use cases, users can streamline their cloud operations and leverage the full potential of Azure services.

Related Posts

Git Clone: The Essential Guide (with examples)

Git Clone: The Essential Guide (with examples)

Git is a powerful version control system that enables developers to collaborate on projects effectively.

Read More
Mastering the 'poweroff' Command in Linux (with examples)

Mastering the 'poweroff' Command in Linux (with examples)

The poweroff command is a straightforward utility in Linux systems, used primarily to manage system shutdowns, reboots, and halting operations efficiently.

Read More
How to use the command 'pacman --database' (with examples)

How to use the command 'pacman --database' (with examples)

The pacman --database command is a powerful tool for managing the Arch Linux package database.

Read More