位於同一個虛擬私人雲端網路中的 VM 可以使用內部 DNS 名稱 (而非 IP 位址) 互相存取。
事前準備
-
如果尚未設定,請先設定驗證機制。「驗證」是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,以便對 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
- 連線至 VM。
查詢
hostname
中繼資料:Linux VM
curl "http://metadata.google.internal/computeMetadata/v1/instance/hostname" \ -H "Metadata-Flavor: Google"
Windows VM
Invoke-RestMethod ` -Headers @{"Metadata-Flavor" = "Google"} ` -Uri "http://metadata.google.internal/computeMetadata/v1/instance/hostname"
- 區域 DNS:
VM_NAME
.ZONE
.c
.PROJECT_ID
.internal
- 全球 DNS:
VM_NAME
.c
.PROJECT_ID
.internal
VM_NAME
:VM 名稱ZONE
:VM 所在區域PROJECT_ID
:VM 所屬的專案VM_NAME
:VM 名稱ZONE
:VM 所在區域PROJECT_ID
:VM 所屬的專案- 遷移至區域 DNS
- 進一步瞭解 Compute Engine 的內部 DNS 名稱。
- 為 VM 設定靜態 IP 位址。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「驗證以使用 REST」。
判斷 VM 的內部 DNS 名稱
使用以下程序,讀取指派給 VM 執行個體的內部 DNS 名稱。您可以查詢
hostname
中繼資料項目,取得內部 DNS 名稱。中繼資料伺服器會以下列格式之一傳回 VM 的主機名稱,顯示 VM 使用的內部 DNS 名稱類型:
輸出內容:
透過內部 DNS 名稱存取 VM
如要存取 VM,請使用內部 DNS 名稱,而非 IP 位址。
以下範例使用
ping
聯絡使用區域 DNS 的 VM。如要使用這個方法,您必須先建立允許執行個體接收 ICMP 流量的防火牆規則。$ ping VM_NAME.ZONE.c.PROJECT_ID.internal -c 1 PING VM_NAME.ZONE.c.PROJECT_ID.internal (10.240.0.17) 56(84) bytes of data. 64 bytes from VM_NAME.ZONE.c.PROJECT_ID.internal (10.240.0.17): icmp_seq=1 ttl=64 time=0.136 ms
更改下列內容:
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-06-16 (世界標準時間)。
-