![](/img/trans.png)
[英]How do I add a SSH key to Google Cloud Compute Engine VM Linux instance?
[英]How to Add an SSH Key to Google Cloud using the CLI
生成基於 RSA 的 SSH 密鑰后:
ssh-keygen -t rsa -f ~/.ssh/id_rsa -C id_rsa
#=>
Generating public/private rsa key pair.
Created directory '/. . ./.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /. . ./.ssh/.id_rsa.
Your public key has been saved in /. . ./.ssh/.id_rsa.pub.
The key fingerprint is:
SHA256:. . . id_rsa
The key's randomart image is:
+---[RSA 3072]----+
| . . . |
+----[SHA256]-----+
我可以將它添加到我的 Google Cloud Platform (GCP) 項目的 ( $GCP_PROJECT_NAME
) 計算元數據中:
gcloud compute project-info add-metadata \
--metadata-from-file ssh-keys=./.ssh/id_rsa.pub
#=>
WARNING: The following key(s) are missing the <username> at the front
ssh-rsa . . . id_rsa
Format ssh keys following https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys
Updated [https://www.googleapis.com/compute/v1/projects/$GCP_PROJECT_NAME].
帶有警告,但無法使用它連接到 GCP Compute 實例。
如果我:
pbcopy < ~/.ssh/id_rsa.pub
我將它粘貼到 GCP Console 中,我可以使用它。
我將如何使用 GCP SDK ( gcloud
) 完成同樣的事情?
將 ssh 密鑰添加到元數據並擴展 @guillaume 以顯示包含所有繁瑣位的特定工作示例
1 獲取現有實例元數據
gcloud compute instances describe <instance name>
2 復制 ssh-keys 元數據值下的公共 SSH 密鑰
3 創建一個文件並包含步驟 2 中的密鑰
4`將密鑰添加到實例
gcloud compute instances add-metadata cos-test --metadata-from-file ssh-keys=<file from step 2>
步驟 2 中的文件應如下所示
<user name>:ssh-rsa <long string of key data> <user name>
在帶有 open-ssh 的 linux 發行版上,我們將創建密鑰
ssh-keygen -t rsa -f ~/.ssh/<key name> -C <user name>
對於為什么 gcloud 想要預先/附加用戶名感到困惑,接下來 gcloud 將根據附加的用戶名和密鑰創建一個遠程用戶和主目錄。 當您登錄時,您需要記住這一點
ssh -v -i <path to your private key> <username>@<public ip>
這:
警告:前面缺少以下鍵
警告是因為:
gcloud compute project-info add-metadata
命令期望 SSH 密鑰顯示為:
$USERNAME: $(cat ~/.ssh/id_rsa.pub)
代替:
cat ~/.ssh/id_rsa.pub
如果您想將基於 RSA 的 SSH 密鑰添加到您的 Google Cloud Project (GCP) 項目 ( $GCP_PROJECT_NAME
):
確保您以正確的用戶身份登錄:
gcloud config list --format="value(core.account)"
如果沒有,請使用以下方法登錄:
gcloud auth login
確保您已通過以下方式連接到$GCP_PROJECT_NAME
:
gcloud config list --format="value(core.project)"
如果沒有,請使用以下命令切換到$GCP_PROJECT_NAME
:
gcloud config set project $GCP_PROJECT_NAME
確保基於 RSA 的密鑰的公鑰和私鑰文件仍然存在:
ls -1 ~/.ssh/id_rsa* #=> /. . ./id_rsa /. . ./id_rsa.pub
使用以下命令檢查$GCP_PROJECT_NAME
所有項目范圍的 SSH 密鑰:
gcloud compute project-info describe --format=json #=> { "commonInstanceMetadata": { . . . "items": [ . . . { "key": "ssh-keys", "value": ". . ." }, . . . ], . . . } . . . }
利用可用於gcloud
的filter()
和gcloud
firstof()
轉換,我們能夠獲取那些項目范圍的 SSH 密鑰:
gcloud compute project-info describe \\ --format="value(commonInstanceMetadata.items.filter(key:ssh-keys).firstof(value))"
如果我們想避免生成臨時文件而僅使用單個命令將基於 RSA 的 SSH 密鑰添加到$GCP_PROJECT_NAME
:
gcloud compute project-info add-metadata \\ --metadata ssh-keys="$(gcloud compute project-info describe \\ --format="value(commonInstanceMetadata.items.filter(key:ssh-keys).firstof(value))") $(whoami):$(cat ~/.ssh/id_rsa.pub)" #=> Updated [https://www.googleapis.com/compute/v1/projects/$GCP_PROJECT_NAME].
您現在應該在$GCP_PROJECT_NAME
看到基於 RSA 的 SSH 密鑰; 檢查:
gcloud compute project-info describe \\ --format="value(commonInstanceMetadata.items.filter(key:ssh-keys).firstof(value))"
注意:我建議使用基於 Ed25519 的 SSH 密鑰而不是基於 RSA 的 SSH 密鑰:
ssh-keygen -t ed25519 -C "$(whoami)@$(hostname)"
#=>
Generating public/private ed25519 key pair.
Enter file in which to save the key (/. . ./.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_ed25519.
Your public key has been saved in id_ed25519.pub.
The key fingerprint is:
SHA256:. . . "$(whoami)@$(hostname)"
The key's randomart image is:
+--[ED25519 256]--+
| . . . |
+----[SHA256]-----+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.