How to capture a classic Linux virtual machine as an image
Azure has two different deployment models for creating and working with resources: Resource Manager and classic. This article covers using the classic deployment model. Microsoft recommends that most new deployments use the Resource Manager model.
This article shows you how to capture a classic Azure virtual machine running Linux as an image to create other virtual machines. This image includes the OS disk and data disks attached to the virtual machine. It doesn't include networking configuration, so you'll need to configure that when you create the other virtual machines from the image.
Azure stores the image under Images. This is also where any images you've uploaded are stored. For more information about images, see About Virtual Machine Images in Azure.
These steps assume that you've already created an Azure virtual machine using the classic deployment model and configured the operating system, including attaching any data disks. If you haven't done this yet, read How to Create a Linux Virtual Machine.
Connect to the virtual machine using an SSH client of your choice. For details, see How to Log on to a Virtual Machine Running Linux.
In the SSH window, type the following command. Note that the output from
waagentmay vary slightly depending on the version of this utility:
sudo waagent -deprovision
This command will attempt to clean the system and make it suitable for re-provisioning. This operation performs the following tasks:
- Removes SSH host keys (if Provisioning.RegenerateSshHostKeyPair is 'y' in the configuration file)
- Clears nameserver configuration in /etc/resolv.conf
- Removes the
rootuser's password from /etc/shadow (if Provisioning.DeleteRootPassword is 'y' in the configuration file)
- Removes cached DHCP client leases
- Resets host name to localhost.localdomain
- Deletes the last provisioned user account (obtained from /var/lib/waagent) and associated data.
Deprovisioning deletes files and data in an effort to "generalize" the image. Only run this command on a virtual machine that you intend to capture as a new image template. It does not guarantee that the image is cleared of all sensitive information or is suitable for redistribution to third parties.
Type y to continue. You can add the
-forceparameter to avoid this confirmation step.
Type Exit to close the SSH client.
From your client computer, open Azure CLI and login to your Azure subscription. For details, read Connect to an Azure subscription from the Azure CLI.
Make sure you are in Service Management mode:
azure config mode asm
Shut down the virtual machine which is already deprovisioned in the steps above with:
azure vm shutdown <your-virtual-machine-name>
When the virtual machine is stopped, capture the image with the command:
azure vm capture -t <your-virtual-machine-name> <new-image-name>
Type the image name you want in place of new-image-name. This command creates a generalized OS image. The
-tsubcommand deletes the original virtual machine.
The new image is now available in the list of images that can be used to configure any new virtual machines. You can view it with the command:
azure vm image list
On the Azure classic portal, it will appear in the IMAGES list.
The image is ready to be used to create virtual machines. You can use the Azure CLI command
azure vm create and supply the image name you just created. See Using the Azure CLI for Mac, Linux, and Windows with Azure Service Management for details about the command. Alternatively, use the Azure classic portal to create a custom virtual machine by using the From Gallery method and selecting the image you just created. See How to Create a Custom Virtual Machine for more details.
See Also: Azure Linux Agent User Guide