0

生成基于 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 计算实例。

如果我:

pbcopy < ~/.ssh/id_rsa.pub

我将它粘贴到 GCP Console 中,我可以使用它。

我如何使用 GCP SDK ( gcloud) 完成同样的事情?

4

3 回答 3

1

将 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>
于 2019-12-09T20:27:34.663 回答
0

这:

警告:以下键缺少前面的

警告是因为:

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):

  1. 确保您以正确的用户身份登录:

    gcloud config list --format="value(core.account)"
    

    如果没有,请使用以下方式登录:

    gcloud auth login
    
  2. 确保您已连接到$GCP_PROJECT_NAME

    gcloud config list --format="value(core.project)"
    

    如果没有,请切换到$GCP_PROJECT_NAME

    gcloud config set project $GCP_PROJECT_NAME
    
  3. 确保您的基于 RSA 的密钥的公钥和私钥文件仍然存在:

    ls -1 ~/.ssh/id_rsa*
    
    #=>
    
    /. . ./id_rsa
    /. . ./id_rsa.pub
    
  4. 使用以下命令检查所有项目范围的 SSH 密钥$GCP_PROJECT_NAME

    gcloud compute project-info describe --format=json
    
    #=>
    
    {
      "commonInstanceMetadata": {
        . . .
        "items": [
          . . .
          {
            "key": "ssh-keys",
            "value": ". . ."
          },
          . . .
        ],
        . . .  
      }
      . . .
    }
    

    利用filter()和可用于的firstof() 转换gcloud,我们能够只获取那些项目范围的 SSH 密钥:

    gcloud compute project-info describe \
    --format="value(commonInstanceMetadata.items.filter(key:ssh-keys).firstof(value))"
    
  5. 如果我们想避免生成临时文件而只使用一个命令将基于 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].
    
  6. 您现在应该$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]-----+
于 2021-08-11T00:40:00.157 回答
-1

您可以使用 gcloud 命令添加和删除 SSH 密钥。但是,如果要向现有密钥添加 ssh 密钥,则需要一个脚本。

文档中所述,如果 VM 元数据中存在现有密钥,则必须恢复它们,添加新密钥并将整个包设置为 VM 元数据。

于 2019-11-29T12:17:05.103 回答