【问题标题】:How do I add a SSH key to Google Cloud Compute Engine VM Linux instance?如何向 Google Cloud Compute Engine VM Linux 实例添加 SSH 密钥?
【发布时间】:2019-04-26 02:00:57
【问题描述】:

我有一个在 Google Cloud Platform 中运行的 Linux VM 实例。 我尝试将我的公钥复制到~/.ssh/authorized_keys,我可以成功地通过 SSH 连接到我的虚拟机。

但有时~/.ssh/authorized_keys 被刷新,我必须再次复制公钥。 每次添加公钥真的很痛苦。 如何永久添加公钥?

【问题讨论】:

  • 您的主目录中的其他文件是否也在消失(主目录是否已擦除)?
  • 不,它们是一样的。

标签: ubuntu google-cloud-platform google-compute-engine


【解决方案1】:

您可以通过adding SSH keys to VMs关注Metadata-managed SSH connections

要使用 Cloud Console 将公共 SSH 密钥添加到项目元数据,请执行以下操作:

1. In the Cloud Console, go to the Metadata page.

2. Go to Metadata > Under SSH Keys, click Edit.

3. Click Add item. A text box opens.

4. Add your public key in the text box. The key must be in one of the following formats:

Format for a key without an expiration time:

KEY_VALUE USERNAME
Format for a key with an expiration time:

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

KEY_VALUE: the public SSH key value
USERNAME: the username for the SSH key, specified when the key was created
EXPIRE_TIME: the time the key expires, in ISO 8601 format. For example: 2021-12-04T20:12:00+0000
5. Click Save.

注意 Required format: <protocol> <key-blob> <username@example.com> or <protocol> <key-blob> google-ssh {"userName":"<username@example.com>","expireOn":"<date>"}的格式

【讨论】:

    【解决方案2】:

    文件“消失”的一个可能原因是,每次重新加载实例时,它都会从不包含该文件的同一 VM 映像重新加载。检查这些事件是否与实例重新加载相关,以及您的主目录中的其他文件是否也消失了,如果他们这样做了,这可能是罪魁祸首。如果是这样,我可以看到几种可能的方法:

    • ~/.ssh/authorized_keys 文件(当然还有您的用户和主目录配置)包含到custom VM image 中,然后您可以使用它来引导您的实例

    • persistent disk 上配置您的主目录,这将在实例重新加载后继续存在

    【讨论】:

      【解决方案3】:

      这 ~/.ssh/authorized_keys 从元数据中获取 ssh 密钥。最好将您的 ssh 公钥保存在元数据中,如 here 所述,还有一个堆栈讨论 here

      【讨论】:

        猜你喜欢
        • 2015-02-16
        • 2020-04-30
        • 2018-08-16
        • 1970-01-01
        • 2014-09-06
        • 2021-01-06
        • 1970-01-01
        • 1970-01-01
        • 2018-07-31
        相关资源
        最近更新 更多