將安全殼層金鑰新增至 VM


本文說明如何將 SSH 金鑰新增至使用 OS 登入的虛擬機器 (VM) 執行個體,以及使用以結構描述資料為基礎的 SSH 金鑰的 VM。如果您或貴組織的管理員未啟用 OS 登入,VM 會使用中繼資料型安全殼層金鑰。

事前準備

  • 如要瞭解如何管理 Compute Engine VM 的存取權,請參閱「選擇存取方式」。
  • 如果您尚未建立,請建立安全殼層金鑰組
  • 如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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

    1. 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.

    2. Set a default region and zone.

    Terraform

    To use the Terraform samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.

    1. Install the Google Cloud CLI.
    2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    3. To initialize the gcloud CLI, run the following command:

      gcloud init
    4. 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.

    For more information, see Set up authentication for a local development environment.

    REST

    To use the REST API samples on this page in a local development environment, you use the credentials you provide to the 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.

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

將金鑰新增至使用 OS 登入功能的 VM

使用 OS 登入功能的 VM 會接受與 Google 帳戶相關聯的安全殼層金鑰。您可以使用 gcloud CLI 或 OS Login API,將公開安全殼層金鑰與 Google 帳戶建立關聯。如果您是機構管理員,可以使用 Directory API 將安全殼層金鑰新增至使用者帳戶。

將安全殼層金鑰新增至 Google 帳戶後,Compute Engine 會結合與 Google 帳戶相關聯的電子郵件中的使用者名稱和網域,為您產生使用者名稱。舉例來說,如果您的電子郵件地址是 cloudysanfrancisco@gmail.com,則使用者名稱就是 cloudysanfrancisco_gmail_com。如果您在機構外專案中新增安全殼層金鑰,使用者名稱會加上前置字元 ext_,例如 ext_cloudysanfrancisco_gmail_com。機構管理員可以使用 Directory API 自訂您的使用者名稱。如果您已設定使用者名稱,Compute Engine 會在您新增安全殼層金鑰時使用該使用者名稱。

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    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.

  2. 如要將公開安全殼層金鑰新增至帳戶,請使用 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 金鑰,或您不是 Cloud Identity 機構的成員,請指定這個欄位
    • EXPIRE_TIME:選用:安全殼層金鑰的到期時間

      舉例來說,如果您指定 30m,安全殼層金鑰會在 30 分鐘後到期。

      這個標記使用下列單位:

      • s 為秒
      • m 代表分鐘
      • h 代表小時
      • d

Terraform

如要將公開安全殼層金鑰新增至帳戶,請使用 google_client_openid_userinfo 資源搭配 google_os_login_ssh_public_key 資源。

data "google_client_openid_userinfo" "me" {
}

resource "google_os_login_ssh_public_key" "default" {
  user = data.google_client_openid_userinfo.me.email
  key  = file("id_rsa.pub") # path/to/ssl/id_rsa.pub
}

REST

如要將公開安全殼層金鑰新增至帳戶,請使用 OS Login API users.importSshPublicKey 方法

POST https://oslogin.googleapis.com/v1/users/ACCOUNT_EMAIL:importSshPublicKey

{
 "key": "SSH_KEY",
 "expirationTimeUsec": "EXPIRATION_TIMESTAMP"
}

更改下列內容:

  • ACCOUNT_EMAIL:與帳戶相關聯的電子郵件地址
  • SSH_KEY:您要新增至帳戶的公開金鑰
  • EXPIRATION_TIMESTAMP:鍵的到期時間,以紀元後微秒為單位 (1 秒 = 106 微秒)

將安全殼層金鑰新增至使用中繼資料型安全殼層金鑰的 VM

不使用 OS 登入功能的 VM 會將 SSH 金鑰儲存在 Compute Engine 專案和執行個體中繼資料中。如果為 VM 啟用 OS 登入功能,VM 的訪客代理程式會忽略儲存在中繼資料中的金鑰。

您可以使用儲存在專案中繼資料中的安全殼層金鑰,存取專案中的所有 VM。您可以使用儲存在執行個體中繼資料中的 SSH 金鑰,存取個別 VM。

在到期時間點,Compute Engine 不會自動從中繼資料中移除已到期的 SSH 金鑰,但已到期的金鑰無法用於建立與 VM 的新連線。如要從中繼資料中移除已過期的金鑰,請參閱從使用中繼資料金鑰的 VM 中移除 SSH 金鑰

您可以使用 Google Cloud 主控台、gcloud CLI 或 REST,將公開安全殼層金鑰新增至專案或 VM 執行個體中繼資料。

將安全殼層 (SSH) 金鑰組新增至專案中繼資料

您可以將公開安全殼層金鑰新增至專案中繼資料,以便存取專案中的所有 VM,但封鎖全專案 SSH 金鑰的 VM 除外。如要進一步瞭解如何封鎖專案層級的安全殼層金鑰,請參閱「封鎖使用中繼資料型安全殼層金鑰的 VM 中的安全殼層金鑰」一文。

主控台

如要使用 Google Cloud 控制台將公開安全殼層金鑰新增至專案中繼資料,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Metadata」頁面。

    前往「中繼資料」

  2. 按一下「SSH keys」分頁標籤。

  3. 按一下 [編輯]

  4. 按一下 [新增項目]。

  5. 在隨即開啟的「SSH 金鑰」欄位中,新增公開安全殼層金鑰。索引鍵必須採用下列其中一種格式:

    • 無到期時間的金鑰格式:

      KEY_VALUE USERNAME
    • 設有到期時間的金鑰格式:

      KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}

    更改下列內容:

    • KEY_VALUE:安全殼層公開金鑰值
    • USERNAME:您的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

      對於 Linux VM,除非您將 VM 設為允許根目錄登入,否則 USERNAME 無法為 root。詳情請參閱「以 root 使用者身分連線至 Linux VM」。

      對於使用 Active Directory (AD) 的 Windows VM,使用者名稱必須在前方加上 AD 網域,格式為 DOMAIN\。舉例來說,ad.example.com 廣告中的使用者 cloudysanfranciscoexample\cloudysanfranciscoUSERNAME

    • EXPIRE_TIME:金鑰到期時間,格式為 ISO 8601 格式。例如:2021-12-04T20:12:00+0000
  6. 按一下 [儲存]

gcloud

In the Google Cloud console, activate Cloud Shell.

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.

  1. 如果專案中繼資料中已有安全殼層金鑰,則每次使用 gcloud CLI 新增安全殼層金鑰時,都必須將這些金鑰重新加入專案中繼資料。如果您沒有重新加入現有金鑰,新增金鑰會清除現有金鑰。

    如要使用 gcloud CLI 將公開安全殼層金鑰新增至專案中繼資料,請按照下列步驟操作:

    1. 如果專案已具有全專案公開安全殼層金鑰,請從中繼資料取得這些金鑰,並將其新增至新檔案:

      1. 執行 gcloud compute project-info describe 指令,取得專案的安全殼層金鑰:

        gcloud compute project-info describe \
         --format="value(commonInstanceMetadata[items][ssh-keys])"
        

        輸出結果會與下列內容相似:

        username:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ... username:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ...
        
      2. 複製 ssh-keys 中繼資料值。

      3. 在工作站上建立並開啟新的文字檔案。

      4. 在檔案中貼上剛才複製的鍵清單。

      5. 請在清單結尾處新增鍵,並使用下列其中一種格式:

        • 無到期時間的金鑰格式:

          USERNAME:KEY_VALUE
        • 設有到期時間的金鑰格式:

          USERNAME:KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}

        更改下列內容:

        • KEY_VALUE:安全殼層公開金鑰值
        • USERNAME:您的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

          對於 Linux VM,除非您將 VM 設為允許根目錄登入,否則 USERNAME 無法為 root。詳情請參閱「以 root 使用者身分連線至執行個體」。

          對於使用 Active Directory (AD) 的 Windows VM,使用者名稱必須在前方加上 AD 網域,格式為 DOMAIN\。舉例來說,ad.example.com 廣告中的使用者 cloudysanfranciscoexample\cloudysanfranciscoUSERNAME

        • EXPIRE_TIME:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000
      6. 儲存並關閉檔案。

    2. 執行 gcloud compute project-info add-metadata 指令,設定全專案 ssh-keys 值:

      gcloud compute project-info add-metadata --metadata-from-file=ssh-keys=KEY_FILE
      

      請使用下列其中一個值取代 KEY_FILE

      • 如果專案已有安全殼層金鑰,則為上一個步驟中建立的檔案路徑
      • 如果專案沒有現有的安全殼層金鑰,則為新公開安全殼層金鑰檔案的路徑

Terraform

如要將公開安全殼層金鑰新增至專案中繼資料,請使用 google_compute_project_metadata 資源。

resource "google_compute_project_metadata" "default" {
  metadata = {
    ssh-keys = <<EOF
      dev:ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILg6UtHDNyMNAh0GjaytsJdrUxjtLy3APXqZfNZhvCeT dev
      test:ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILg6UtHDNyMNAh0GjaytsJdrUxjtLy3APXqZfNZhvCeT test
    EOF
  }
}

REST

如果專案中繼資料中已有安全殼層金鑰,每次使用 Compute Engine API 新增安全殼層金鑰時,都必須將這些金鑰重新加入專案中繼資料。如果您沒有重新加入現有金鑰,新增金鑰會清除現有金鑰。

如要使用 Compute Engine API 將公開安全殼層金鑰新增至專案中繼資料,請按照下列步驟操作:

  1. 使用 projects.get 方法,從中繼資料取得 fingerprintssh-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"}"
    }
    ]
    ...
    
  2. 使用 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:您的專案 ID
    • EXISTING_SSH_KEYSprojects.get 要求回應中的 ssh-keys 鍵值
    • FINGERPRINTprojects.get 要求回應中的 fingerprint
    • NEW_SSH_KEY:新安全殼層金鑰,格式如下:

      • 無到期時間的金鑰格式:

        USERNAME:KEY_VALUE
      • 設有到期時間的金鑰格式:

        USERNAME:KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}

      更改下列內容:

      • KEY_VALUE:安全殼層公開金鑰值
      • USERNAME:您的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

        對於 Linux VM,除非您將 VM 設為允許根目錄登入,否則 USERNAME 無法為 root。詳情請參閱「以 root 使用者身分連線至執行個體」。

        對於使用 Active Directory (AD) 的 Windows VM,使用者名稱必須在前方加上 AD 網域,格式為 DOMAIN\。舉例來說,ad.example.com 廣告中的使用者 cloudysanfranciscoexample\cloudysanfranciscoUSERNAME

      • EXPIRE_TIME:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000

將安全殼層金鑰新增至執行個體中繼資料

您可以在建立虛擬機建立虛擬機後,將公開安全殼層金鑰新增至執行個體中繼資料。

在建立 VM 時將安全殼層金鑰新增至執行個體中繼資料

您可以在建立 VM 時,使用 Google Cloud 控制台、gcloud CLI 或 Compute Engine API,將 SSH 金鑰加入執行個體中繼資料。

主控台

如要使用 Google Cloud 控制台建立執行個體,並同時將公開安全殼層金鑰新增至執行個體中繼資料,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。

    前往「Create an instance」(建立執行個體)

  2. 如要將公開安全殼層金鑰新增至執行個體中繼資料,請按照下列步驟操作:

    1. 按一下導覽選單中的「Security」(安全性)

    2. 展開「管理存取權」部分。

    3. 如要停用 OS 登入功能,請清除「透過 IAM 權限控管 VM 存取權」核取方塊。

    4. 如果您想允許專案中繼資料中的公開安全殼層金鑰存取執行個體,請取消勾選「Block project-wide SSH keys」(封鎖全專案安全殼層金鑰) 核取方塊。

    5. 在「Add manually generated SSH keys」(新增手動產生的安全殼層金鑰) 專區中,按一下「Add item」(新增項目)

    6. 在文字方塊中新增公開金鑰。鍵必須採用下列任一格式:

      • 無到期時間的金鑰格式:

        KEY_VALUE USERNAME
      • 設有到期時間的金鑰格式:

        KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}

      更改下列內容:

      • KEY_VALUE:安全殼層公開金鑰值
      • USERNAME:您的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

        對於 Linux VM,除非您將 VM 設為允許根目錄登入,否則 USERNAME 無法為 root。詳情請參閱「以 root 使用者身分連線至 Linux VM」。

        對於使用 Active Directory (AD) 的 Windows VM,使用者名稱必須在前方加上 AD 網域,格式為 DOMAIN\。舉例來說,ad.example.com 廣告中的使用者 cloudysanfranciscoexample\cloudysanfranciscoUSERNAME

      • EXPIRE_TIME:金鑰到期時間,格式為 ISO 8601 格式。例如:2021-12-04T20:12:00+0000
  3. 選用:指定其他設定選項。詳情請參閱「建立執行個體時的設定選項」。

  4. 如要建立並啟動執行個體,請按一下「建立」

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    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.

  2. 如要使用 gcloud CLI 建立 VM,並同時將公開安全殼層金鑰新增至執行個體中繼資料,請使用 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:安全殼層公開金鑰值
      • USERNAME:您的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

        對於 Linux VM,除非您將 VM 設為允許根目錄登入,否則 USERNAME 無法為 root。詳情請參閱「以 root 使用者身分連線至執行個體」。

        對於使用 Active Directory (AD) 的 Windows VM,使用者名稱必須在前方加上 AD 網域,格式為 DOMAIN\。舉例來說,ad.example.com 廣告中的使用者 cloudysanfranciscoexample\cloudysanfranciscoUSERNAME

      • EXPIRE_TIME:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000

    您可以使用 --metadata-from-file=ssh-keys=FILE_PATH 標記新增多個安全殼層金鑰。在檔案中,使用上述格式之一,新增使用者名稱和公開安全殼層金鑰清單。

Terraform

如要將公開安全殼層金鑰新增至執行個體中繼資料,請使用 google_compute_instance 資源。

resource "google_compute_instance" "default" {
  name         = "my-vm"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "ubuntu-1404-trusty-v20160627"
    }
  }

  # Local SSD disk
  scratch_disk {
    interface = "SCSI"
  }

  network_interface {
    network = "default"
    access_config {}
  }
  metadata = {
    "ssh-keys" = <<EOT
      dev:ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILg6UtHDNyMNAh0GjaytsJdrUxjtLy3APXqZfNZhvCeT dev
      test:ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILg6UtHDNyMNAh0GjaytsJdrUxjtLy3APXqZfNZhvCeT test
     EOT
  }
}

REST

如要使用 Compute Engine 建立 VM,並同時將公開安全殼層金鑰新增至執行個體中繼資料,請建構 POST 要求,並傳送至 instances.insert 方法:

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances

更改下列內容:

  • PROJECT_ID:專案 ID
  • ZONE:VM 的可用區

在要求主體中,請在 items 屬性中提供使用者名稱和公開安全殼層金鑰:

...
{
 "items": [
    {
     "key": "ssh-keys",
     "value": "PUBLIC_KEY"
    }
   ]
}
...

PUBLIC_KEY 替換為公開金鑰,格式如下:

  • 無到期時間的金鑰格式:

    USERNAME:KEY_VALUE
  • 設有到期時間的金鑰格式:

    USERNAME:KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}

更改下列內容:

  • KEY_VALUE:安全殼層公開金鑰值
  • USERNAME:您的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

    對於 Linux VM,除非您將 VM 設為允許根目錄登入,否則 USERNAME 無法為 root。詳情請參閱「以 root 使用者身分連線至執行個體」。

    對於使用 Active Directory (AD) 的 Windows VM,使用者名稱必須在前方加上 AD 網域,格式為 DOMAIN\。舉例來說,ad.example.com 廣告中的使用者 cloudysanfranciscoexample\cloudysanfranciscoUSERNAME

  • EXPIRE_TIME:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000

您可以新增多個安全殼層金鑰,方法是在金鑰之間新增 \n

在建立 VM 後,將安全殼層金鑰新增至執行個體中繼資料

您可以在建立 VM 後,使用 Google Cloud 主控台、gcloud CLI 或 Compute Engine API,將 SSH 金鑰加入執行個體中繼資料。

主控台

如要使用 Google Cloud 控制台將公開安全殼層金鑰新增至執行個體中繼資料,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往「VM Instances」(VM 執行個體)

  2. 按一下要新增 SSH 金鑰的 VM 名稱。

  3. 按一下 [編輯]

  4. 在「SSH Keys」(安全殼層金鑰) 下方,按一下「Add item」(新增項目)

  5. 在文字方塊中新增公開金鑰。鍵必須採用下列任一格式:

    • 無到期時間的金鑰格式:

      KEY_VALUE USERNAME
    • 設有到期時間的金鑰格式:

      KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}

    更改下列內容:

    • KEY_VALUE:安全殼層公開金鑰值
    • USERNAME:您的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

      對於 Linux VM,除非您將 VM 設為允許根目錄登入,否則 USERNAME 無法為 root。詳情請參閱「以 root 使用者身分連線至 Linux VM」。

      對於使用 Active Directory (AD) 的 Windows VM,使用者名稱必須在前方加上 AD 網域,格式為 DOMAIN\。舉例來說,ad.example.com 廣告中的使用者 cloudysanfranciscoexample\cloudysanfranciscoUSERNAME

    • EXPIRE_TIME:金鑰到期時間,格式為 ISO 8601 格式。例如:2021-12-04T20:12:00+0000
  6. 按一下 [儲存]

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    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.

  2. 如果執行個體中繼資料中已有安全殼層金鑰,則每次使用 gcloud CLI 新增安全殼層金鑰時,都必須將這些金鑰重新加入執行個體中繼資料。如果您沒有重新加入現有金鑰,新增新金鑰會清除現有金鑰。

    如要使用 gcloud CLI 將公開安全殼層金鑰新增至執行個體中繼資料,請按照下列步驟操作:

    1. 如果虛擬機器人已具有執行個體層級公開安全殼層金鑰,請從中繼資料中取得這些金鑰,然後新增至新檔案:

      1. 執行 gcloud compute instances describe 指令,取得 VM 的中繼資料:

        gcloud compute instances describe VM_NAME
        

        VM_NAME 替換為您需要新增或移除公開安全殼層金鑰的 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"}
        ...
        
      2. 複製 ssh-keys 中繼資料值。

      3. 在工作站上建立並開啟新的文字檔案。

      4. 在檔案中貼上剛才複製的鍵清單。

      5. 請在清單結尾處新增鍵,並使用下列其中一種格式:

      • 無到期時間的金鑰格式:

        USERNAME:KEY_VALUE
      • 設有到期時間的金鑰格式:

        USERNAME:KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}

      更改下列內容:

      • KEY_VALUE:安全殼層公開金鑰值
      • USERNAME:您的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

        對於 Linux VM,除非您將 VM 設為允許根目錄登入,否則 USERNAME 無法為 root。詳情請參閱「以 root 使用者身分連線至執行個體」。

        對於使用 Active Directory (AD) 的 Windows VM,使用者名稱必須在前方加上 AD 網域,格式為 DOMAIN\。舉例來說,ad.example.com 廣告中的使用者 cloudysanfranciscoexample\cloudysanfranciscoUSERNAME

      • EXPIRE_TIME:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000
      1. 儲存並關閉檔案。
    2. 執行 gcloud compute instances add-metadata 指令來設定 ssh-keys 值:

      gcloud compute instances add-metadata VM_NAME --metadata-from-file ssh-keys=KEY_FILE
      

      更改下列內容:

      • VM_NAME:要新增安全殼層金鑰的 VM
      • KEY_FILE 與下列其中一個值:
      • 如果 VM 已有安全殼層金鑰,則為上一個步驟中建立的檔案路徑
      • 如果虛擬機器人沒有現有的安全殼層金鑰,則為新公開安全殼層金鑰檔案的路徑

REST

如果執行個體中繼資料中已有 SSH 金鑰,每次使用 Compute Engine API 新增 SSH 金鑰時,都必須將這些金鑰重新加入執行個體中繼資料。如果您沒有重新加入現有金鑰,新增新金鑰會清除現有金鑰。

如要使用 Compute Engine API 將公開安全殼層金鑰新增至執行個體中繼資料,請按照下列步驟操作:

  1. 使用 instances.get 方法,從中繼資料取得 fingerprintssh-keys 值。

    GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
    

    更改下列內容:

    • PROJECT_ID:您的專案 ID
    • ZONE:要新增 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"}"
    }
    ]
    ...
    
  2. 使用 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:您的專案 ID
    • EXISTING_SSH_KEYSinstances.get 要求回應中的 ssh-keys 鍵值
    • FINGERPRINTprojects.get 要求回應中的 fingerprint
    • NEW_SSH_KEY:新安全殼層金鑰,格式如下:

      • 無到期時間的金鑰格式:

        USERNAME:KEY_VALUE
      • 設有到期時間的金鑰格式:

        USERNAME:KEY_VALUE google-ssh {"userName":"USERNAME","expireOn":"EXPIRE_TIME"}

      更改下列內容:

      • KEY_VALUE:安全殼層公開金鑰值
      • USERNAME:您的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

        對於 Linux VM,除非您將 VM 設為允許根目錄登入,否則 USERNAME 無法為 root。詳情請參閱「以 root 使用者身分連線至執行個體」。

        對於使用 Active Directory (AD) 的 Windows VM,使用者名稱必須在前方加上 AD 網域,格式為 DOMAIN\。舉例來說,ad.example.com AD 中的使用者 cloudysanfranciscoexample\cloudysanfranciscoUSERNAME

      • EXPIRE_TIME:金鑰到期時間,格式為 ISO 8601。例如:2021-12-04T20:12:00+0000

後續步驟