本文說明如何在虛擬機器 (VM) 執行個體上啟用巢狀虛擬化,以及如何確認您可以建立巢狀 VM。如要在 VM 上啟用巢狀虛擬化,請使用下列其中一種方法:
建議做法:如要直接在新 VM 或現有 VM 上啟用巢狀虛擬化,請在建立 VM 時將
enableNestedVirtualization欄位設為true,或更新 VM。建議使用這種方法,因為您不需要建立自訂映像檔或使用特殊授權金鑰。如要使用特殊授權金鑰啟用巢狀虛擬化,請建立開機磁碟、使用特殊巢狀虛擬化授權金鑰建立自訂映像檔,然後建立使用該自訂映像檔的 VM。
事前準備
-
如果尚未設定驗證,請先完成設定。
驗證可確認您的身分,以便存取 Cloud de Confiance by S3NS 服務和 API。如要從本機開發環境執行程式碼或範例,可以選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
gcloud
-
Install the Google Cloud CLI, and then sign in to the gcloud CLI with your federated identity. After signing in, initialize the Google Cloud CLI by running the following command:
gcloud init - Set a default region and zone.
VM_NAME:啟用巢狀虛擬化的新 L1 VM 名稱ZONE:啟用巢狀虛擬化功能的新 L1 VM 所在區域PROJECT_ID:專案 IDZONE:啟用巢狀虛擬化功能的新 L1 VM 所在區域VM_NAME:啟用巢狀虛擬化的新 L1 VM 名稱使用下列
gcloud compute instances export指令匯出 VM 的屬性:gcloud compute instances export VM_NAME \ --destination=YAML_FILE_PATH \ --zone=ZONE
更改下列內容:
VM_NAME:要匯出屬性的 VM 名稱YAML_FILE_PATH:.yaml 檔案的路徑和檔案名稱,用於儲存匯出的設定資料ZONE:包含 VM 的可用區
在 FILE_PATH 中儲存的 VM 設定檔中,更新
enableNestedVirtualization的值。如果檔案中沒有這個值,請新增下列內容:advancedMachineFeatures: enableNestedVirtualization: true
使用下列
gcloud compute instances update-from-file指令,以enableNestedVirtualization的值更新 VM:gcloud compute instances update-from-file VM_NAME \ --source=FILE_PATH \ --most-disruptive-allowed-action=RESTART \ --zone=ZONE
更改下列內容:
VM_NAME:要更新的 VM 名稱FILE_PATH:更新後 VM 設定檔的路徑ZONE:要更新的 VM 所在區域
PROJECT_ID:專案 IDZONE:包含 VM 的可用區VM_NAME:要匯出屬性的 VM 名稱從公用映像檔或自訂映像檔建立開機磁碟。以下範例使用
debian-cloud代表映像檔專案,並使用debian-10代表映像檔系列。如果您已有具備現有磁碟的 VM 執行個體,可以略過這個步驟。gcloud
gcloud compute disks create DISK_NAME \ --zone=ZONE \ --image-project=debian-cloud \ --image-family=debian-10
更改下列內容:
DISK_NAME:新磁碟的名稱ZONE:要在哪個可用區建立磁碟
REST
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks { ... "name": "DISK_NAME", "sourceImage": "projects/debian-cloud/global/images/family/debian-10", ... }更改下列內容:
PROJECT_ID:專案 IDZONE:要在哪個可用區建立磁碟DISK_NAME:新磁碟的名稱
使用巢狀虛擬化所需的特殊授權金鑰,建立自訂映像檔。
gcloud
gcloud compute images create IMAGE_NAME \ --source-disk DISK_NAME \ --source-disk-zone ZONE \ --licenses "https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"
更改下列內容:
IMAGE_NAME:新映像檔的名稱DISK_NAME:先前建立的磁碟名稱ZONE:要在哪個可用區建立映像檔
REST
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/images { ... "licenses": ["projects/vm-options/global/licenses/enable-vmx"], "name": "IMAGE_NAME", "sourceDisk": "zones/ZONE/disks/DISK_NAME", ... }更改下列內容:
PROJECT_ID:專案 IDIMAGE_NAME:新映像檔的名稱ZONE:要在哪個可用區建立映像檔DISK_NAME:先前建立的磁碟名稱
使用特殊授權建立映像檔後,您可以視需要刪除來源磁碟。
gcloud
gcloud compute disks delete DISK_NAME --zone=ZONE
更改下列內容:
DISK_NAME:要刪除的磁碟名稱ZONE:包含要刪除磁碟的可用區
REST
DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks/DISK_NAME
更改下列內容:
PROJECT_ID:專案 IDZONE:包含要刪除磁碟的可用區DISK_NAME:要刪除的磁碟名稱
建立使用新映像檔和特殊授權的 VM。最低 CPU 平台必須為
"Intel Haswell"。gcloud
gcloud compute instances create VM_NAME \ --zone=ZONE \ --min-cpu-platform "Intel Haswell" \ --image IMAGE_NAME更改下列內容:
VM_NAME:VM 的名稱ZONE:要建立 VM 的可用區IMAGE_NAME:先前建立的映像檔名稱
REST
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances { ... "name": "VM_NAME", "minCpuPlatform": "Intel Haswell", "disks": [ { "initializeParams": { "sourceImage": "IMAGE_NAME" } } ] ... }更改下列內容:
PROJECT_ID:專案 IDVM_NAME:VM 的名稱ZONE:要建立 VM 的可用區IMAGE_NAME:先前建立的映像檔名稱
連結至 VM 執行個體。
gcloud compute ssh VM_NAME
將
VM_NAME替換為要連線的 VM 名稱。確認已啟用巢狀虛擬化。如果回應不是
0,表示已啟用巢狀虛擬化。grep -cw vmx /proc/cpuinfo
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
Install the Google Cloud CLI, and then sign in to the gcloud CLI with your federated identity. After signing in, initialize the Google Cloud CLI by running the following command:
gcloud init詳情請參閱 Cloud de Confiance 驗證說明文件中的「使用 REST 進行驗證」。
直接在新 VM 上啟用巢狀虛擬化
如要直接在 VM 上啟用巢狀虛擬化,請按照下列程序操作。
gcloud
使用下列
gcloud compute instances create指令,建立啟用巢狀虛擬化的 L1 VM:gcloud compute instances create VM_NAME \ --enable-nested-virtualization \ --zone=ZONE \ --min-cpu-platform="Intel Haswell"
更改下列內容:
REST
使用下列
instances.insert方法建立啟用巢狀虛擬化的 L1 VM:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances { ... "name": "VM_NAME", ... "minCpuPlatform": "Intel Haswell", "advancedMachineFeatures": { "enableNestedVirtualization": true }, ... }更改下列內容:
直接在現有 VM 上啟用巢狀虛擬化
如要在現有 VM 上啟用巢狀虛擬化功能,請按照下列步驟操作。
gcloud
REST
使用下列
instances.update方法更新enableNestedVirtualization的值:PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME?most_disruptive_allowed_action=RESTART { ⋮ "advanced_machine_features": { ⋮ "enableNestedVirtualization": "true" }, ⋮ }更改下列內容:
使用特殊授權金鑰啟用巢狀虛擬化功能
如要在 VM 上啟用巢狀虛擬化,請使用特殊授權金鑰建立自訂映像檔,在 L1 VM 上啟用 VMX。授權金鑰不收取額外費用。
確認 VM 已啟用巢狀虛擬化
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2026-03-18 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2026-03-18 (世界標準時間)。"],[],[]] -