本文件說明如何從 VM 移除及封鎖 SSH 金鑰,防止使用者存取虛擬機器 (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
-
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.
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 和使用中繼資料提供的安全殼層金鑰的 VM 中移除安全殼層金鑰。從使用 OS 登入功能的 VM 中移除 SSH 金鑰
使用 OS 登入功能的 VM 會接受與 Google 帳戶相關聯的安全殼層金鑰。您可以使用 Google Cloud CLI 或 OS Login API,從使用者帳戶中移除公開安全殼層金鑰。如果您是機構的管理員,可以使用 Directory API 從使用者帳戶中移除安全殼層金鑰。Compute Engine 會自動從您的 Google 帳戶中移除已過期的金鑰。
gcloud
如要從帳戶中移除公開安全殼層金鑰,請按照下列步驟操作:
如果您不確定要移除哪個金鑰,請執行
gcloud compute os-login describe-profile
指令,查看與帳戶相關聯的所有金鑰:gcloud compute os-login describe-profile
複製要刪除的金鑰的
fingerprint
值。使用
gcloud compute os-login ssh-keys remove
指令從帳戶中移除金鑰:gcloud compute os-login ssh-keys remove --key=KEY
將
KEY
替換為要移除的公開安全殼層金鑰,或要移除的金鑰的 OS 登入指紋。
REST
如要從帳戶中移除公開安全殼層金鑰,請按照下列步驟操作:
如果不知道要移除哪個金鑰,請使用
users.getLoginProfile
方法查看與帳戶相關聯的所有金鑰:GET https://oslogin.googleapis.com/v1/users/ACCOUNT_EMAIL/loginProfile
將
ACCOUNT_EMAIL
替換為與帳戶相關聯的電子郵件地址。複製要刪除的金鑰的
fingerprint
值。使用
users.sshPublicKeys.delete
方法從帳戶中移除金鑰:DELETE https://oslogin.googleapis.com/v1/users/ACCOUNT_EMAIL/sshPublicKeys/FINGERPRINT
更改下列內容:
ACCOUNT_EMAIL
:與帳戶相關聯的電子郵件地址FINGERPRINT
:要移除的金鑰 SHA-256 指紋
從使用中繼資料金鑰的 VM 中移除安全殼層金鑰
您可以使用 Google Cloud 控制台、gcloud CLI 或 Compute Engine API,從專案或執行個體中繼資料中移除公開安全殼層金鑰。
您從特定使用者的中繼資料中移除最後一個鍵,或特定使用者中繼資料中的最後一個鍵到期後,Compute Engine 就會刪除使用者在 VM 上的 ~/.ssh/authorized_keys
檔案。
從專案中繼資料中移除公開金鑰
從專案中繼資料中移除公開安全殼層金鑰,即可移除專案中所有 VM 的存取權。
使用 gcloud CLI 和 Compute Engine API 從中繼資料中移除金鑰時,您必須擷取現有金鑰清單、編輯金鑰清單以移除不需要的金鑰,然後使用要保留的金鑰清單覆寫舊金鑰,如以下章節所述。
主控台
如要使用 Google Cloud 控制台從專案中繼資料移除公開安全殼層金鑰,請按照下列步驟操作:
前往 Google Cloud 控制台的「Metadata」頁面。
按一下「SSH keys」分頁標籤。
按一下頁面頂端的
「編輯」。前往要移除的安全殼層金鑰,然後按一下安全殼層金鑰旁的「
」刪除按鈕。針對要移除的每個 SSH 金鑰重複執行這項步驟。
按一下 [儲存]。
gcloud
如要使用 gcloud CLI 從專案中繼資料移除公開安全殼層金鑰,請按照下列步驟操作:
執行
gcloud compute project-info describe
指令,取得專案的中繼資料:gcloud compute project-info describe
輸出結果會與下列內容相似:
... 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
中繼資料值。在工作站上建立並開啟新的文字檔案。
在檔案中貼上您剛才複製的安全殼層金鑰清單,然後刪除要從專案中繼資料中移除的任何金鑰。
儲存並關閉檔案。
執行
gcloud compute project-info add-metadata
指令,設定全專案ssh-keys
值:gcloud compute project-info add-metadata --metadata-from-file=ssh-keys=KEY_FILE
請使用下列其中一個值取代
KEY_FILE
:- 如果專案已有安全殼層金鑰,則為上一個步驟中建立的檔案路徑
- 如果專案沒有現有的安全殼層金鑰,則為新公開安全殼層金鑰檔案的路徑
REST
如要使用 Compute Engine API 從專案中繼資料移除公開安全殼層金鑰,請執行下列操作:
使用
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"}" } ] ...
複製 SSH 金鑰值清單,然後刪除要移除的金鑰。
使用
projects.setCommonInstanceMetadata
移除安全殼層金鑰。POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/setCommonInstanceMetadata { "items": [ { "key": "ssh-keys", "value": "EXISTING_SSH_KEYS" } ] "fingerprint": "FINGERPRINT" }
更改下列內容:
PROJECT_ID
:您的專案 IDEXISTING_SSH_KEYS
:您要保留的安全殼層金鑰清單FINGERPRINT
:projects.get
要求回應中的fingerprint
值
從執行個體中繼資料中移除公開安全殼層金鑰
從執行個體中繼資料中移除公開安全殼層金鑰,即可移除對單一 VM 的存取權。
使用 gcloud CLI 和 Compute Engine API 從中繼資料中移除金鑰時,您必須擷取現有金鑰清單、編輯金鑰清單以移除不需要的金鑰,然後使用要保留的金鑰清單覆寫舊金鑰,如以下章節所述。
主控台
如要使用 Google Cloud 控制台從執行個體中繼資料移除公開安全殼層金鑰,請按照下列步驟操作:
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
按一下要移除金鑰的 VM 名稱。
按一下 [編輯]。
在「SSH Keys」專區中,按一下「Show and edit」。該區段會展開並顯示所有執行個體層級公開安全殼層金鑰。
按一下要移除的 SSH 金鑰旁邊的
刪除按鈕。針對要移除的每個 SSH 金鑰重複執行這項步驟。
按一下 [儲存]。
gcloud
如要使用 gcloud CLI 從執行個體中繼資料移除公開安全殼層金鑰,請執行下列操作:
執行
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"} ...複製
ssh-keys
中繼資料值。在本機工作站上建立並開啟一個新的文字檔案。
在檔案中貼上您剛才複製的 SSH 金鑰清單,然後移除要刪除的任何金鑰。
儲存並關閉檔案。
執行
gcloud compute project-info add-metadata
指令,設定全專案ssh-keys
值:gcloud compute instances add-metadata VM_NAME --metadata-from-file ssh-keys=KEY_FILE
更改下列內容:
VM_NAME
:要移除安全殼層金鑰的 VMKEY_FILE
:包含所有專案安全殼層金鑰清單的檔案路徑
REST
如要使用 Compute Engine API 從執行個體中繼資料中移除公開安全殼層金鑰,請按照下列步驟操作:
使用
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"}" } ] ...
複製 SSH 金鑰值清單,然後刪除要移除的金鑰。
使用
instances.setMetadata
移除安全殼層金鑰。POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setMetadata { "items": [ { "key": "ssh-keys", "value": "EXISTING_SSH_KEYS } ] "fingerprint": "FINGERPRINT" }
更改下列內容:
PROJECT_ID
:您的專案 IDEXISTING_SSH_KEYS
:projects.get
要求回應中的ssh-keys
鍵值FINGERPRINT
:instances.get
要求回應中的fingerprint
值
封鎖使用中繼資料型安全殼層金鑰的 VM 專案安全殼層金鑰
您可以封鎖 VM 中的專案安全殼層金鑰,防止 VM 接受儲存在專案中繼資料中的安全殼層金鑰。您可以在建立 VM時,或建立 VM 後,封鎖 VM 中的專案安全殼層金鑰。
在建立 VM 時,封鎖 VM 中的專案安全殼層金鑰
您可以使用 Google Cloud 控制台、gcloud CLI 或 Compute Engine API,在建立 VM 時封鎖專案 SSH 金鑰。
主控台
如要使用 Google Cloud 控制台建立執行個體,並禁止該執行個體接受儲存在專案中繼資料中的 SSH 金鑰,請執行下列操作:
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
如要封鎖專案安全殼層金鑰,請按照下列步驟操作:
按一下導覽選單中的「Security」(安全性)。
展開「管理存取權」部分。
如要停用 OS 登入功能,請清除「透過 IAM 權限控管 VM 存取權」核取方塊。
選取「Block project-wide SSH keys」(封鎖全專案安全殼層金鑰) 核取方塊。
選用:指定其他設定選項。詳情請參閱「建立執行個體時的設定選項」。
如要建立並啟動執行個體,請按一下「建立」。
gcloud
如要使用 gcloud CLI 建立 VM,並封鎖該 VM 接受儲存在專案中繼資料中的 SSH 金鑰,請使用 gcloud compute instances create
指令:
gcloud compute instances create VM_NAME \ --metadata block-project-ssh-keys=TRUE
將 VM_NAME
替換為新 VM 的名稱。
REST
如要建立 VM,並禁止 VM 使用 Compute Engine 接受儲存在專案中繼資料中的 SSH 金鑰,請建構 POST
要求至 instances.insert
方法:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances
更改下列內容:
PROJECT_ID
:專案 IDZONE
:VM 的可用區
在要求主體中,請在 items
屬性中提供使用者名稱和公開安全殼層金鑰:
... { "items": [ { "key": "block-project-ssh-keys", "value": TRUE } ] } ...
在建立 VM 後,從 VM 封鎖專案安全殼層金鑰
您可以在建立 VM 後,使用 Google Cloud 控制台、gcloud CLI 或 Compute Engine API 封鎖 VM 中的專案 SSH 金鑰。
主控台
如要使用 Google Cloud 控制台封鎖 VM 接受來自儲存在專案中繼資料的 SSH 金鑰連線,請執行下列操作:
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
按一下要封鎖專案 SSH 金鑰的 VM 名稱。
按一下 [編輯]。
在「SSH Keys」(安全殼層金鑰) 下方,選取「Block project-wide SSH keys」(封鎖全專案安全殼層金鑰) 核取方塊。
安全殼層金鑰的連線設定編輯完成後,請按一下 [Save] (儲存)。
gcloud
如要使用 gcloud CLI 封鎖 VM 接受來自專案中繼資料中儲存 SSH 金鑰的連線,請執行下列操作:
執行 gcloud compute instances add-metadata
指令:
gcloud compute instances add-metadata VM_NAME --metadata block-project-ssh-keys=TRUE
將 VM_NAME
替換為您要封鎖全專案公開安全殼層金鑰的 VM 名稱。
REST
如要使用 Compute Engine API 封鎖 VM 接受儲存在專案中繼資料中的 SSH 金鑰連線,請執行下列操作:
使用
instances.get
方法,從中繼資料取得fingerprint
。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=" ...
請使用
instances.setMetadata
方法將block-project-ssh-keys
設為TRUE
:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setMetadata { "items": [ { "key": "block-project-ssh-keys", "value": TRUE } ] "fingerprint": "FINGERPRINT" }
更改下列內容:
PROJECT_ID
:您的專案 IDZONE
是執行個體所在的區域INSTANCE_NAME
是要封鎖全專案金鑰的執行個體。FINGERPRINT
:instances.get
要求回應中的fingerprint
值。
後續步驟
- 瞭解使用 OS 登入管理存取權的優勢。
- 使用 Google 工具連線至 VM,這樣您就不必自行管理安全殼層金鑰。