本文說明如何將 SSH 金鑰新增至使用 OS 登入的虛擬機器 (VM) 執行個體,以及使用中繼資料型 SSH 金鑰的 VM。如果您或貴機構管理員尚未啟用 OS 登入,VM 會使用中繼資料型 SSH 金鑰。
事前準備
- 如要瞭解如何管理 Compute Engine VM 的存取權,請參閱選擇存取方式。
- 如果尚未建立,請建立 SSH 金鑰組。
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
如要將公開安全殼層金鑰新增至帳戶,請使用
gcloud compute os-login ssh-keys add
指令:gcloud compute os-login ssh-keys add \ --key-file=KEY_FILE_PATH \ --project=PROJECT \ --ttl=EXPIRE_TIME
更改下列內容:
KEY_FILE_PATH
:工作站上公開安全殼層金鑰的路徑。金鑰必須使用public-openssh
格式PROJECT
:選用:您打算使用 SSH 金鑰的專案。指定這個欄位,即可在機構外部的專案中使用 SSH 金鑰,或在您不是 Cloud Identity 機構成員時使用EXPIRE_TIME
:選用:安全殼層金鑰的到期時間舉例來說,如果您指定
30m
,SSH 金鑰會在 30 分鐘後失效。這個標記使用下列單位:
s
秒m
分鐘h
小時d
天
ACCOUNT_EMAIL
:與帳戶相關聯的電子郵件地址SSH_KEY
:要新增至帳戶的公開金鑰EXPIRATION_TIMESTAMP
:金鑰的到期時間,以自 Unix 紀元時間 (1 秒 = 106 微秒) 以來的微秒數表示在 Google Cloud 控制台中,前往「Metadata」頁面。
按一下「SSH keys」(SSH 金鑰) 分頁標籤。
按一下 [編輯]。
按一下 [新增項目]。
在開啟的「SSH key」欄位中,新增公開安全殼層金鑰。金鑰必須採用下列任一格式:
- 沒有到期時間的金鑰格式:
KEY_VALUE USERNAME
- 設有到期時間的金鑰格式:
KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}
更改下列內容:
KEY_VALUE
:公開 SSH 金鑰值USERNAME
:您的使用者名稱。例如cloudysanfrancisco
或cloudysanfrancisco_gmail_com
。如果是 Linux VM,除非您將 VM 設為允許根目錄登入,否則
USERNAME
無法為root
。詳情請參閱「以根使用者身分連線至 Linux VM」。如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須加上 AD 網域前置字元,格式為
DOMAIN\
。舉例來說,ad.example.com
AD 中的使用者cloudysanfrancisco
具有example\cloudysanfrancisco
的USERNAME
。EXPIRE_TIME
:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000
- 沒有到期時間的金鑰格式:
按一下 [儲存]。
如果專案中繼資料中已有安全殼層金鑰,每次使用 gcloud CLI 新增安全殼層金鑰時,都必須重新將現有金鑰新增至專案中繼資料。如果沒有重新新增現有金鑰,新增金鑰時就會清除現有金鑰。
如要使用 gcloud CLI 將公開安全殼層金鑰新增至專案中繼資料,請按照下列步驟操作:
如果專案已具有全專案公開安全殼層金鑰,請從中繼資料取得這些金鑰,然後新增至新檔案:
執行
gcloud compute project-info describe
指令,取得專案的安全殼層 (SSH) 金鑰:gcloud compute project-info describe \ --format="value(commonInstanceMetadata[items][ssh-keys])"
輸出結果會與下列內容相似:
username:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ... username:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ...
複製
ssh-keys
中繼資料值。在工作站上建立並開啟新的文字檔案。
在檔案中貼上剛才複製的金鑰清單。
在清單結尾新增金鑰,格式如下:
- 沒有到期時間的金鑰格式:
USERNAME:KEY_VALUE
- 設有到期時間的金鑰格式:
USERNAME:KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}
更改下列內容:
KEY_VALUE
:公開 SSH 金鑰值USERNAME
:您的使用者名稱。例如cloudysanfrancisco
或cloudysanfrancisco_gmail_com
。如果是 Linux VM,除非您將 VM 設為允許根目錄登入,否則
USERNAME
無法為root
。詳情請參閱「以 root 使用者身分連線至執行個體」。如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須加上 AD 網域前置字元,格式為
DOMAIN\
。舉例來說,ad.example.com
AD 中的使用者cloudysanfrancisco
具有example\cloudysanfrancisco
的USERNAME
。EXPIRE_TIME
:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000
- 沒有到期時間的金鑰格式:
儲存並關閉檔案。
執行
gcloud compute project-info add-metadata
指令,設定全專案ssh-keys
值:gcloud compute project-info add-metadata --metadata-from-file=ssh-keys=KEY_FILE
請將
KEY_FILE
替換為下列其中一個值:- 如果您專案已有安全殼層金鑰,請輸入您在上一個步驟中建立的檔案路徑
- 如果專案沒有現有的 SSH 金鑰,則為新公開 SSH 金鑰檔案的路徑
使用
projects.get
方法,從中繼資料取得fingerprint
和ssh-keys
值GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID
將
PROJECT_ID
替換為您的專案 ID。回應類似以下內容:
... "fingerprint": "utgYE_XWtE8=", "items": [ { "key": "ssh-keys", "value": "cloudysanfrancisco:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF...\nbaklavainthebalkans:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQDx3FNVC8... google-ssh {"userName":"baklavainthebalkans","expireOn":"2021-06-14T16:59:03+0000"}" } ] ...
使用
projects.setCommonInstanceMetadata
方法新增ssh-keys
值。POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/setCommonInstanceMetadata { "items": [ { "key": "ssh-keys", "value": "EXISTING_SSH_KEYS\nNEW_SSH_KEY" } ] "fingerprint": "FINGERPRINT" }
更改下列內容:
PROJECT_ID
:您的專案 IDEXISTING_SSH_KEYS
:projects.get
要求回應中的ssh-keys
鍵值FINGERPRINT
:projects.get
要求回應中的fingerprint
值NEW_SSH_KEY
:新的安全殼層金鑰,格式如下:- 沒有到期時間的金鑰格式:
USERNAME:KEY_VALUE
- 設有到期時間的金鑰格式:
USERNAME:KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}
更改下列內容:
KEY_VALUE
:公開 SSH 金鑰值USERNAME
:您的使用者名稱。例如cloudysanfrancisco
或cloudysanfrancisco_gmail_com
。如果是 Linux VM,除非您將 VM 設為允許根目錄登入,否則
USERNAME
無法為root
。詳情請參閱「以 root 使用者身分連線至執行個體」。如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須加上 AD 網域前置字元,格式為
DOMAIN\
。舉例來說,ad.example.com
AD 中的使用者cloudysanfrancisco
具有example\cloudysanfrancisco
的USERNAME
。EXPIRE_TIME
:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000
- 沒有到期時間的金鑰格式:
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
如要將公開 SSH 金鑰新增至執行個體中繼資料,請按照下列步驟操作:
按一下導覽選單中的「Security」(安全性)。
展開「管理存取權」部分。
如要停用 OS 登入,請清除「透過 IAM 權限控管 VM 存取權」核取方塊。
如要允許專案中繼資料中的公開 SSH 金鑰存取執行個體,請取消勾選「封鎖全專案安全殼層金鑰」核取方塊。
在「新增手動產生的安全殼層金鑰」區段中,按一下「新增項目」。
在文字方塊中新增公開金鑰。金鑰必須採用下列任一格式:
- 沒有到期時間的金鑰格式:
KEY_VALUE USERNAME
- 設有到期時間的金鑰格式:
KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}
更改下列內容:
KEY_VALUE
:公開 SSH 金鑰值USERNAME
:您的使用者名稱。例如cloudysanfrancisco
或cloudysanfrancisco_gmail_com
。如果是 Linux VM,除非您將 VM 設為允許根目錄登入,否則
USERNAME
無法為root
。詳情請參閱「以根使用者身分連線至 Linux VM」。如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須加上 AD 網域前置字元,格式為
DOMAIN\
。舉例來說,ad.example.com
AD 中的使用者cloudysanfrancisco
具有example\cloudysanfrancisco
的USERNAME
。EXPIRE_TIME
:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000
- 沒有到期時間的金鑰格式:
視需要指定其他設定選項。詳情請參閱「建立執行個體時的設定選項」。
如要建立並啟動執行個體,請按一下「建立」。
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
如要使用 gcloud CLI 建立 VM,並同時將公開 SSH 金鑰新增至執行個體中繼資料,請使用
gcloud compute instances create
指令:gcloud compute instances create VM_NAME \ --metadata=ssh-keys=PUBLIC_KEY
更改下列內容:
VM_NAME
:新 VM 的名稱PUBLIC_KEY
:您的公開安全殼層金鑰,格式如下:- 沒有到期時間的金鑰格式:
USERNAME:KEY_VALUE
- 設有到期時間的金鑰格式:
USERNAME:KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}
更改下列內容:
KEY_VALUE
:公開 SSH 金鑰值USERNAME
:您的使用者名稱。例如cloudysanfrancisco
或cloudysanfrancisco_gmail_com
。如果是 Linux VM,除非您將 VM 設為允許根目錄登入,否則
USERNAME
無法為root
。詳情請參閱「以 root 使用者身分連線至執行個體」。如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須加上 AD 網域前置字元,格式為
DOMAIN\
。舉例來說,ad.example.com
AD 中的使用者cloudysanfrancisco
具有example\cloudysanfrancisco
的USERNAME
。EXPIRE_TIME
:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000
- 沒有到期時間的金鑰格式:
您可以使用
--metadata-from-file=ssh-keys=FILE_PATH
標記新增多個 SSH 金鑰。在檔案中,以其中一種上述格式新增使用者名稱和公開安全殼層金鑰清單。PROJECT_ID
:專案 IDZONE
:VM 所在可用區- 沒有到期時間的金鑰格式:
USERNAME:KEY_VALUE
- 設有到期時間的金鑰格式:
USERNAME:KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}
KEY_VALUE
:公開 SSH 金鑰值USERNAME
:您的使用者名稱。例如cloudysanfrancisco
或cloudysanfrancisco_gmail_com
。如果是 Linux VM,除非您將 VM 設為允許根目錄登入,否則
USERNAME
無法為root
。詳情請參閱「以 root 使用者身分連線至執行個體」。如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須加上 AD 網域前置字元,格式為
DOMAIN\
。舉例來說,ad.example.com
AD 中的使用者cloudysanfrancisco
具有example\cloudysanfrancisco
的USERNAME
。EXPIRE_TIME
:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
按一下要新增 SSH 金鑰的 VM 名稱。
按一下 [編輯]。
在「SSH Keys」(安全殼層金鑰) 下方,按一下「Add item」(新增項目)。
在文字方塊中新增公開金鑰。金鑰必須採用下列任一格式:
- 沒有到期時間的金鑰格式:
KEY_VALUE USERNAME
- 設有到期時間的金鑰格式:
KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}
更改下列內容:
KEY_VALUE
:公開 SSH 金鑰值USERNAME
:您的使用者名稱。例如cloudysanfrancisco
或cloudysanfrancisco_gmail_com
。如果是 Linux VM,除非您將 VM 設為允許根目錄登入,否則
USERNAME
無法為root
。詳情請參閱「以根使用者身分連線至 Linux VM」。如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須加上 AD 網域前置字元,格式為
DOMAIN\
。舉例來說,ad.example.com
AD 中的使用者cloudysanfrancisco
具有example\cloudysanfrancisco
的USERNAME
。EXPIRE_TIME
:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000
- 沒有到期時間的金鑰格式:
按一下 [儲存]。
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
如果執行個體中繼資料中已有安全殼層金鑰,每次使用 gcloud CLI 新增安全殼層金鑰時,都必須將現有金鑰重新新增至執行個體中繼資料。如果您未重新新增現有金鑰,新增金鑰時會清除現有金鑰。
如要使用 gcloud CLI 將公開 SSH 金鑰新增至執行個體中繼資料,請執行下列步驟:
如果 VM 已有執行個體層級的公開 SSH 金鑰,請從中繼資料取得這些金鑰,然後新增至新檔案:
執行
gcloud compute instances describe
指令,取得 VM 的中繼資料:gcloud compute instances describe VM_NAME
將 VM_NAME 替換為要新增或移除公開 SSH 金鑰的 VM 名稱。
輸出結果會與下列內容相似:
... metadata: ...
- key: ssh-keys
value: |- cloudysanfrancisco:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF... baklavainthebalkans:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQDx3FNVC8... google-ssh {"userName":"baklavainthebalkans","expireOn":"2021-06-14T16:59:03+0000"} ...複製
ssh-keys
中繼資料值。在工作站上建立並開啟新的文字檔案。
在檔案中貼上剛才複製的金鑰清單。
在清單結尾新增金鑰,格式如下:
- 沒有到期時間的金鑰格式:
USERNAME:KEY_VALUE
- 設有到期時間的金鑰格式:
USERNAME:KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}
更改下列內容:
KEY_VALUE
:公開 SSH 金鑰值USERNAME
:您的使用者名稱。例如cloudysanfrancisco
或cloudysanfrancisco_gmail_com
。如果是 Linux VM,除非您將 VM 設為允許根目錄登入,否則
USERNAME
無法為root
。詳情請參閱「以 root 使用者身分連線至執行個體」。如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須加上 AD 網域前置字元,格式為
DOMAIN\
。舉例來說,ad.example.com
AD 中的使用者cloudysanfrancisco
具有example\cloudysanfrancisco
的USERNAME
。EXPIRE_TIME
:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000
- 儲存並關閉檔案。
執行
gcloud compute instances add-metadata
指令來設定ssh-keys
值:gcloud compute instances add-metadata VM_NAME --metadata-from-file ssh-keys=KEY_FILE
更改下列內容:
VM_NAME
:要新增安全殼層金鑰的 VMKEY_FILE
,並符合下列其中一項條件:- 如果您在 VM 中已有 SSH 金鑰,請提供您在上一個步驟中建立的檔案路徑
- 如果 VM 沒有現有的 SSH 金鑰,則為新公開 SSH 金鑰檔案的路徑
使用
instances.get
方法,從中繼資料取得fingerprint
和ssh-keys
值。GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT_ID
:您的專案 IDZONE
:要新增 SSH 金鑰的 VM 所在區域VM_NAME
:要新增安全殼層金鑰的 VM
回應類似以下內容:
... "fingerprint": "utgYE_XWtE8=", "items": [ { "key": "ssh-keys", "value": "cloudysanfrancisco:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF...\nbaklavainthebalkans:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQDx3FNVC8... google-ssh {"userName":"baklavainthebalkans","expireOn":"2021-06-14T16:59:03+0000"}" } ] ...
使用
instances.setMetadata
方法新增ssh-keys
值。POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setMetadata { "items": [ { "key": "ssh-keys", "value": "EXISTING_SSH_KEYS\nNEW_SSH_KEY" } ] "fingerprint": "FINGERPRINT" }
更改下列內容:
PROJECT_ID
:您的專案 IDEXISTING_SSH_KEYS
:instances.get
要求回應中的ssh-keys
鍵值FINGERPRINT
:projects.get
要求回應中的fingerprint
NEW_SSH_KEY
:新的安全殼層金鑰,格式如下:- 沒有到期時間的金鑰格式:
USERNAME:KEY_VALUE
- 設有到期時間的金鑰格式:
USERNAME:KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}
更改下列內容:
KEY_VALUE
:公開 SSH 金鑰值USERNAME
:您的使用者名稱。例如cloudysanfrancisco
或cloudysanfrancisco_gmail_com
。如果是 Linux VM,除非您將 VM 設為允許根目錄登入,否則
USERNAME
無法為root
。詳情請參閱「以 root 使用者身分連線至執行個體」。如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須加上 AD 網域前置字元,格式為
DOMAIN\
。舉例來說,ad.example.com
AD 中的使用者cloudysanfrancisco
具有example\cloudysanfrancisco
的USERNAME
。EXPIRE_TIME
:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000
- 沒有到期時間的金鑰格式:
- 瞭解如何連線至 VM。
- 瞭解如何管理 VM 的存取權。
- 瞭解如何將檔案轉移至 VM。
- 瞭解如何使用 SSH 連線至 Compute Engine 的 Linux VM。
Terraform
如要在本機開發環境中使用本頁的 Terraform 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
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 驗證說明文件中的「Authenticate for using REST」。
將金鑰新增至使用 OS 登入功能的 VM
使用 OS 登入功能的 VM 會接受與 Google 帳戶相關聯的安全殼層金鑰。您可以使用 gcloud CLI 或 OS Login API,將公開 SSH 金鑰與 Google 帳戶建立關聯。如果您是機構管理員,可以使用 Directory API 將安全殼層金鑰新增至使用者帳戶。
將安全殼層金鑰新增至 Google 帳戶時,Compute Engine 會結合與 Google 帳戶相關聯的電子郵件地址中的使用者名稱和網域,為您產生使用者名稱。舉例來說,如果您的電子郵件地址是
cloudysanfrancisco@gmail.com
,則使用者名稱為cloudysanfrancisco_gmail_com
。如果您在機構外部的專案中新增 SSH 金鑰,使用者名稱會加上ext_
前置字元,例如ext_cloudysanfrancisco_gmail_com
。機構管理員可以使用 Directory API 自訂使用者名稱。如果您已設定使用者名稱,Compute Engine 會在您新增 SSH 金鑰時使用該使用者名稱。gcloud
Terraform
如要將公開安全殼層金鑰新增至帳戶,請使用
google_client_openid_userinfo
資源和google_os_login_ssh_public_key
資源。REST
如要將公開安全殼層金鑰新增至帳戶,請使用 OS Login API
users.importSshPublicKey
方法:POST https://oslogin.googleapis.com/v1/users/ACCOUNT_EMAIL:importSshPublicKey { "key": "SSH_KEY", "expirationTimeUsec": "EXPIRATION_TIMESTAMP" }
更改下列內容:
將 SSH 金鑰新增至使用中繼資料型 SSH 金鑰的 VM
未使用 OS 登入的 VM會將 SSH 金鑰儲存在 Compute Engine 專案和執行個體中繼資料中。如果 VM 啟用 OS 登入,VM 的訪客代理程式就會忽略儲存在中繼資料中的金鑰。
您可以使用儲存在專案中繼資料中的 SSH 金鑰,存取專案中的所有 VM。您可以使用儲存在執行個體中繼資料中的 SSH 金鑰,存取個別 VM。
Compute Engine 不會在過期時自動從中繼資料移除過期的 SSH 金鑰,但過期的金鑰無法用於建立與 VM 的新連線。如要從中繼資料中移除過期的金鑰,請參閱「從使用中繼資料型金鑰的 VM 移除 SSH 金鑰」。
您可以使用 Google Cloud 控制台、gcloud CLI 或 REST,將公開 SSH 金鑰新增至專案或 VM 執行個體中繼資料。您無法為安全殼層金鑰設定區域中繼資料值。
將 SSH 金鑰新增至專案中繼資料
您可以將公開 SSH 金鑰新增至專案中繼資料,存取專案中的所有 VM,但封鎖全專案 SSH 金鑰的 VM 除外。如要進一步瞭解如何封鎖專案層級的安全殼層金鑰,請參閱「封鎖使用中繼資料型安全殼層金鑰的 VM 中的安全殼層金鑰」。
主控台
如要使用Google Cloud 控制台將公開安全殼層金鑰新增至專案中繼資料,請按照下列步驟操作:
gcloud
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Terraform
如要將公開 SSH 金鑰新增至專案中繼資料,請使用
google_compute_project_metadata
資源。REST
如果專案中繼資料中已有安全殼層金鑰,每次使用 Compute Engine API 新增安全殼層金鑰時,都必須重新將現有金鑰新增至專案中繼資料。如果沒有重新新增現有金鑰,新增金鑰時就會清除現有金鑰。
如要使用 Compute Engine API 將公開 SSH 金鑰新增至專案中繼資料,請按照下列步驟操作:
將 SSH 金鑰新增至執行個體中繼資料
您可以在建立 VM 時,或建立 VM 後,將公開安全殼層金鑰新增至執行個體中繼資料。
在建立 VM 時,將 SSH 金鑰新增至執行個體中繼資料
您可以在建立 VM 時,使用Google Cloud 控制台、gcloud CLI 或 Compute Engine API,將 SSH 金鑰新增至執行個體中繼資料。
主控台
如要使用 Google Cloud 控制台建立執行個體,並同時將公開 SSH 金鑰新增至執行個體中繼資料,請按照下列步驟操作:
gcloud
Terraform
如要將公開安全殼層金鑰新增至執行個體中繼資料,請使用
google_compute_instance
資源。REST
如要使用 Compute Engine 建立 VM,並同時將公開 SSH 金鑰新增至執行個體中繼資料,請建構對
instances.insert
方法的POST
要求:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances
更改下列內容:
在要求主體中,於
items
屬性中提供使用者名稱和公開安全殼層金鑰:... { "items": [ { "key": "ssh-keys", "value": "PUBLIC_KEY" } ] } ...
將
PUBLIC_KEY
換成您的公開金鑰,格式如下:更改下列內容:
如要新增多個 SSH 金鑰,請在金鑰之間新增
\n
。在建立 VM 後,將安全殼層金鑰新增至執行個體中繼資料
您可以在建立 VM 後,使用Google Cloud 控制台、gcloud CLI 或 Compute Engine API,將 SSH 金鑰新增至執行個體中繼資料。
主控台
如要使用Google Cloud console 將公開安全殼層金鑰新增至執行個體中繼資料,請按照下列步驟操作:
gcloud
REST
如果執行個體中繼資料已有安全殼層 (SSH) 金鑰,您每次使用 Compute Engine API 新增 SSH 金鑰時,都必須重新將現有金鑰新增至執行個體中繼資料。如果您未重新新增現有金鑰,新增金鑰時會清除現有金鑰。
如要使用 Compute Engine API 將公開 SSH 金鑰新增至執行個體中繼資料,請按照下列步驟操作:
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-21 (世界標準時間)。
-