如何建立虛擬機器或 VHD 的受控映像How to create a managed image of a virtual machine or VHD

若要建立虛擬機器 (VM) 的多個複本以在 Azure 中用於開發和測試,請擷取 VM 或 OS VHD 的受控映像。To create multiple copies of a virtual machine (VM) for use in Azure for development and test, capture a managed image of the VM or of the OS VHD. 若要大規模建立、儲存和共用映像,請參閱共用映像資源庫To create, store and share images at scale, see Shared Image Galleries.

一個受控映像最多可支援 20 個同時部署。One managed image supports up to 20 simultaneous deployments. 嘗試從相同的受控映像同時建立 20 個以上的虛擬機器時,因為單一 VHD 的儲存體效能限制之故,可能會導致佈建逾時。Attempting to create more than 20 VMs concurrently, from the same managed image, may result in provisioning timeouts due to the storage performance limitations of a single VHD. 若要同時建立 20 個以上的 VM,請使用共用映像資源庫中設定為每 20 個並行虛擬機器部署 1 個複本的映像。To create more than 20 VMs concurrently, use a Shared Image Galleries image configured with 1 replica for every 20 concurrent VM deployments.

若要建立受控映像,您必須移除個人帳戶資訊。To create a managed image, you'll need to remove personal account information. 在下列步驟中,您將取消佈建現有的 VM、將它解除配置,然後建立映像。In the following steps, you deprovision an existing VM, deallocate it and create an image. 您可以使用此映像,在您訂用帳戶的任何資源群組中建立 VM。You can use this image to create VMs across any resource group within your subscription.

若要建立一份現有 Linux VM 的副本以進行備份或偵錯,或是從內部部署 VM 上傳特定的 Linux VHD,請參閱從自訂的磁碟映像上傳及建立 Linux VMTo create a copy of your existing Linux VM for backup or debugging, or to upload a specialized Linux VHD from an on-premises VM, see Upload and create a Linux VM from custom disk image.

您可以使用 Azure VM Image Builder (公開預覽) 服務來建立自訂映像,而不需要學習任何工具或設定組建管道,只要提供映像設定,映像產生器就會建立映像。You can use the Azure VM Image Builder (Public Preview) service to build your custom image, no need to learn any tools, or setup build pipelines, simply providing an image configuration, and the Image Builder will create the Image. 如需詳細資訊,請參閱開始使用 Azure VM Image BuilderFor more information, see Getting Started with Azure VM Image Builder.

建立映像之前,您需要下列項目:You'll need the following items before creating an image:

偏好使用教學課程?Prefer a tutorial instead?

如需本文的簡化版本,以進行測試、評估或深入了解 Azure 中的 VM,請參閱使用 CLI 建立 Azure VM 的自訂映像For a simplified version of this article, and for testing, evaluating, or learning about VMs in Azure, see Create a custom image of an Azure VM by using the CLI. 否則,請繼續參閱這裡以瞭解整體情況。Otherwise, keep reading here to get the full picture.

步驟 1:取消佈建 VMStep 1: Deprovision the VM

首先使用 Azure VM 代理程式來取消佈建 VM,以將電腦特定的檔案和資料刪除。First you'll deprovision the VM by using the Azure VM agent to delete machine-specific files and data. 在來源 Linux VM 上使用 waagent 命令搭配 -deprovision+user 參數。Use the waagent command with the -deprovision+user parameter on your source Linux VM. 如需詳細資訊,請參閱 Azure Linux 代理程式使用者指南For more information, see the Azure Linux Agent user guide.

  1. 使用 SSH 用戶端連線到 Linux VM。Connect to your Linux VM with an SSH client.

  2. 在 SSH 視窗中,輸入下列命令:In the SSH window, enter the following command:

    sudo waagent -deprovision+user
    

    注意

    只在您要擷取作為映像的 VM 上執行這個命令。Only run this command on a VM that you'll capture as an image. 此命令不能保證映像檔中的所有機密資訊都會清除完畢或適合轉散發。This command does not guarantee that the image is cleared of all sensitive information or is suitable for redistribution. +user 參數也會移除最後一個佈建的使用者帳戶。The +user parameter also removes the last provisioned user account. 若要在 VM 中保留使用者帳戶認證,僅使用 -deprovisionTo keep user account credentials in the VM, use only -deprovision.

  3. 輸入 y 繼續。Enter y to continue. 您可以新增 -force 參數,便不用進行此確認步驟。You can add the -force parameter to avoid this confirmation step.

  4. 在命令完成之後,請輸入exit 關閉 SSH 用戶端。After the command completes, enter exit to close the SSH client. 此時 VM 仍會在執行中。The VM will still be running at this point.

步驟 2:建立 VM 映像Step 2: Create VM image

使用 Azure CLI 將 VM 標記為一般化,並擷取映像。Use the Azure CLI to mark the VM as generalized and capture the image. 在下列範例中,請以您自己的值取代範例參數名稱。In the following examples, replace example parameter names with your own values. 範例參數名稱包含 myResourceGroupmyVnetmyVMExample parameter names include myResourceGroup, myVnet, and myVM.

  1. 使用 az vm deallocate 解除配置已取消佈建的 VM。Deallocate the VM that you deprovisioned with az vm deallocate. 下列範例會解除配置名為 myResourceGroup 資源群組中名為 myVM 的 VM。The following example deallocates the VM named myVM in the resource group named myResourceGroup.

    az vm deallocate \
      --resource-group myResourceGroup \
      --name myVM
    

    等到 VM 完全解除配置,再繼續進行。Wait for the VM to completely deallocate before moving on. 這可能需要幾分鐘才能完成。This may take a few minutes to complete. VM 會在解除配置期間關閉。The VM is shut down during deallocation.

  2. 使用 az vm generalize,將 VM 標記為一般化。Mark the VM as generalized with az vm generalize. 下列範例會將名為 myResourceGroup 的資源群組中名為 myVM 的 VM 標記為一般化。The following example marks the VM named myVM in the resource group named myResourceGroup as generalized.

    az vm generalize \
      --resource-group myResourceGroup \
      --name myVM
    

    已一般化的 VM 無法再重新啟動。A VM that has been generalized can no longer be restarted.

  3. 使用 az image create 建立 VM 資源的映像。Create an image of the VM resource with az image create. 下列範例會使用名為 myVM 的 VM 資源,在 myResourceGroup 資源群組中建立名為 myImage 的映像。The following example creates an image named myImage in the resource group named myResourceGroup using the VM resource named myVM.

    az image create \
      --resource-group myResourceGroup \
      --name myImage --source myVM
    

    注意

    此映像與來源 VM 建立於相同的資源群組中。The image is created in the same resource group as your source VM. 您可以從此映像,在您訂用帳戶的任何資源群組中建立 VM。You can create VMs in any resource group within your subscription from this image. 從管理觀點來看,您可能想為您的 VM 資源和映像建立特定的資源群組。From a management perspective, you may wish to create a specific resource group for your VM resources and images.

    如果您想要將映像儲存於區域復原的儲存體中,則需要在支援可用性區域且包含 --zone-resilient true 參數的區域中建立它。If you would like to store your image in zone-resilient storage, you need to create it in a region that supports availability zones and include the --zone-resilient true parameter.

此命令會傳回描述 VM 映像的 JSON。This command returns JSON that describes the VM image. 儲存此輸出以供日後參考。Save this output for later reference.

步驟 3:從擷取的映像建立 VMStep 3: Create a VM from the captured image

使用您以 az vm create 建立的映像來建立 VM。Create a VM by using the image you created with az vm create. 下列範例會從名為 myImage 的映像建立名為 myVMDeployed 的 VM。The following example creates a VM named myVMDeployed from the image named myImage.

az vm create \
   --resource-group myResourceGroup \
   --name myVMDeployed \
   --image myImage\
   --admin-username azureuser \
   --ssh-key-value ~/.ssh/id_rsa.pub

在另一個資源群組中建立 VMCreating the VM in another resource group

您可以從某個映像,在您訂用帳戶的任何資源群組中建立 VM。You can create VMs from an image in any resource group within your subscription. 若要在與映像不同的資源群組中建立 VM,請指定您映像的完整資源識別碼。To create a VM in a different resource group than the image, specify the full resource ID to your image. 使用 az image list 來檢視映像清單。Use az image list to view a list of images. 輸出類似於下列範例:The output is similar to the following example.

"id": "/subscriptions/guid/resourceGroups/MYRESOURCEGROUP/providers/Microsoft.Compute/images/myImage",
   "location": "westus",
   "name": "myImage",

下列範例藉由指定映像資源識別碼,進而使用 az vm create 在與來源映像不同的資源群組中建立 VM。The following example uses az vm create to create a VM in a resource group other than the source image, by specifying the image resource ID.

az vm create \
   --resource-group myOtherResourceGroup \
   --name myOtherVMDeployed \
   --image "/subscriptions/guid/resourceGroups/MYRESOURCEGROUP/providers/Microsoft.Compute/images/myImage" \
   --admin-username azureuser \
   --ssh-key-value ~/.ssh/id_rsa.pub

步驟 4:驗證部署Step 4: Verify the deployment

透過 SSH 連線到您建立的虛擬機器,以驗證部署並開始使用新的 VM。SSH into the virtual machine you created to verify the deployment and start using the new VM. 若要透過 SSH 連接,請利用 az vm show 尋找您 VM 的 IP 位址或 FQDN。To connect via SSH, find the IP address or FQDN of your VM with az vm show.

az vm show \
   --resource-group myResourceGroup \
   --name myVMDeployed \
   --show-details

後續步驟Next steps

若要大規模建立、儲存和共用映像,請參閱共用映像資源庫To create, store and share images at scale, see Shared Image Galleries.