How to Attach a Disk to an Azure VM: Step-by-Step Guide
To attach a disk to an Azure VM, use the
az vm disk attach command with the VM name, resource group, and disk name. This command connects an existing managed disk to your VM so it can be used as additional storage.Syntax
The basic command to attach a disk to an Azure VM is:
az vm disk attach: The command to attach a disk.--resource-group: The name of the resource group containing the VM.--vm-name: The name of the virtual machine.--name: The name of the managed disk to attach.--new(optional): To create and attach a new disk instead of an existing one.
bash
az vm disk attach --resource-group MyResourceGroup --vm-name MyVM --name MyDisk
Example
This example shows how to attach an existing managed disk named MyDataDisk to a VM named MyVM in the resource group MyResourceGroup. After running this command, the disk will be available to the VM as additional storage.
bash
az vm disk attach --resource-group MyResourceGroup --vm-name MyVM --name MyDataDisk
Output
Disk 'MyDataDisk' has been attached to VM 'MyVM'.
Common Pitfalls
Common mistakes when attaching disks include:
- Trying to attach a disk that does not exist or is in a different resource group.
- Not specifying the correct VM name or resource group.
- Forgetting to initialize and format the disk inside the VM after attaching.
- Attempting to attach a disk that is already attached to another VM.
Always verify the disk and VM names and ensure the disk is not in use elsewhere.
bash
Wrong: az vm disk attach --resource-group WrongGroup --vm-name MyVM --name MyDisk Right: az vm disk attach --resource-group MyResourceGroup --vm-name MyVM --name MyDisk
Quick Reference
| Parameter | Description |
|---|---|
| --resource-group | Name of the resource group containing the VM and disk |
| --vm-name | Name of the virtual machine to attach the disk to |
| --name | Name of the managed disk to attach |
| --new | Create and attach a new disk instead of using an existing one (optional) |
Key Takeaways
Use the az vm disk attach command with correct VM and disk names to attach a disk.
Ensure the disk exists and is not attached to another VM before attaching.
After attaching, initialize and format the disk inside the VM to use it.
Always specify the correct resource group for both VM and disk.
Use --new option to create and attach a new disk in one command.