【问题标题】:How to Add an SSH Key to Google Cloud using the CLI如何使用 CLI 将 SSH 密钥添加到 Google Cloud
【发布时间】:2020-03-24 06:24:28
【问题描述】:

生成基于 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 公钥的格式在 Internet 上有记载。基本上,在最后一行添加`myusername`。但是,这取决于密钥的格式。您甚至可能使用了错误的公钥格式。
  • 我不确定您要按照哪个文档在元数据中添加/删除 ssh 密钥。关于“Managing SSH keys in metadata”的帮助中心文章似乎很简单。您是否检查过该文档中的“creating a new SSH key”部分?要添加或删除项目范围的公共 SSH 密钥,您可以参考 this section
  • 感谢您的回复。我的问题是如何通过 gcloud cli 上传 ssh 密钥。我非常仔细地阅读了这两个链接。您能告诉我如何将 ssh 密钥上传到元数据。

标签: google-cloud-platform ssh gcloud ssh-keys gcloud-compute


【解决方案1】:

警告:以下键在前面缺少

警告是因为:

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. 使用以下命令检查$GCP_PROJECT_NAME 的所有项目范围的 SSH 密钥:

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

    利用可用于gcloudfilter()firstof() transforms,我们能够获取那些项目范围的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]-----+

【讨论】:

    【解决方案2】:

    将 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>
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2019-01-17
        • 2022-07-01
        • 2019-04-26
        • 2018-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-06
        • 2012-07-31
        相关资源
        最近更新 更多