Add a disk to a Linux VM

Applies to: ✔️ Linux VMs ✔️ Flexible scale sets

This article shows you how to attach a persistent disk to your virtual machine (VM) to preserve your data, even if your VM is reprovisioned due to maintenance or resizing.

Note

Before you can use Azure CLI in Microsoft Azure operated by 21Vianet, please run az cloud set -n AzureChinaCloud first to change the cloud environment. If you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

Attach a new disk to a VM

If you want to add a new, empty data disk to your VM, use the az vm disk attach command with the --new parameter. If your VM is in an Availability Zone, the disk is automatically created in the same zone as the VM. For more information, see Overview of Availability Zones. The following example creates a disk named myDataDisk that is 50 Gb in size:

az vm disk attach \
   -g myResourceGroup \
   --vm-name myVM \
   --name myDataDisk \
   --new \
   --size-gb 50

Lower latency

In select regions, disk attach latency is reduced. In those regions, there's an improvement of up to 15%. This improvement is useful if you have planned or unplanned failovers between VMs, you're scaling your workload, or you're running a high-scale stateful workload such as Azure Kubernetes Service. However, this improvement is limited to the explicit disk attach command, az vm disk attach. You won't see the performance improvement if you call a command that might implicitly perform an attach, like az vm update. You don't need to take any action other than calling the explicit attach command to see this improvement.

Attach an existing disk

To attach an existing disk, find the disk ID and pass the ID to the az vm disk attach command. The following example queries for a disk named myDataDisk in myResourceGroup, then attaches it to the VM named myVM:

diskId=$(az disk show -g myResourceGroup -n myDataDisk --query 'id' -o tsv)

az vm disk attach -g myResourceGroup --vm-name myVM --name $diskId

Identifying disks

Azure Linux VMs use different disk interfaces depending on the VM size and generation:

  • VM sizes v6 and newer: Use NVMe interface for improved performance
  • VM sizes v5 and older: Use SCSI interface for disk management

Connect to the virtual machine

To identify disks associated with your Linux VM, connect to the VM by using SSH. For details, see How to use SSH with Linux on Azure. The following example connects to a VM with the public IP address of 10.123.123.25 with the username azureuser:

ssh azureuser@10.123.123.25

Note

Before identifying specific disks, determine whether your VM uses SCSI, NVMe, or a combination of both interfaces.

The azure-vm-utils package provides utilities to optimize the Linux experience on Azure VMs, making disk identification more reliable across different VM configurations.

Use the following commands to list disks on the VM:

# List all disks
sudo azure-disk-list

# List NVMe disks with detailed information
sudo azure-nvme-id

The output from azure-nvme-id is similar to:

/dev/nvme0n1: type=os
/dev/nvme0n2: type=data, lun=0
/dev/nvme1n1: type=local, index=1, name=nvme-50G-1

Next Steps