Skip to main content

Adding a new SSH key to your GitHub account

To configure your account on your GitHub Enterprise Server instance to use your new (or existing) SSH key, you'll also need to add the key to your account.

About addition of SSH keys to your account

可以使用 SSH(安全外壳协议)访问和写入 your GitHub Enterprise Server instance 上的存储库中的数据。 通过 SSH 进行连接时,使用本地计算机上的私钥文件进行身份验证。 For more information, see "About SSH."

After you generate an SSH key pair, you must add the public key to your GitHub Enterprise Server instance to enable SSH access for your account.

Prerequisites

Before adding a new SSH key to your account on your GitHub Enterprise Server instance, complete the following steps.

  1. Check for existing SSH keys. For more information, see "Checking for existing SSH keys."
  2. Generate a new SSH key and add it to your machine's SSH agent. For more information, see "Generating a new SSH key and adding it to the ssh-agent."

Adding a new SSH key to your account

After adding a new SSH authentication key to your account on your GitHub Enterprise Server instance, you can reconfigure any local repositories to use SSH. For more information, see "Switching remote URLs from HTTPS to SSH."

注意:默认情况下,若使用 GitHub Enterprise Server 3.6 及更高版本,则截至 2022 年 8 月 1 日午夜 (UTC),同时满足以下两个条件的 SSH 连接将失败 。


  • RSA 密钥在 2022 年 8 月 1 日午夜 UTC 截止日期之后添加到 your GitHub Enterprise Server instance 上的用户帐户。
  • SSH 客户端使用 SHA-1 哈希函数对连接尝试进行签名。

GitHub Enterprise Server 3.6 及更高版本也不支持使用 DSA、HMAC-SHA-1 或 CBC 密码的 SSH 连接。 只要密钥保持有效,在截止日期之前上传的 RSA SSH 密钥便可以继续使用 SHA-1 哈希函数进行身份验证。 有关查找所使用的 GitHub Enterprise Server 版本的详细信息,请参阅“关于 GitHub Docs 的版本”。

站点管理员可以使用 RSA-SHA-1 调整连接的截止日期,也可以使用 RSA-SHA-1 阻止所有连接。 有关详细信息,请联系站点管理员,或参阅“配置与实例的 SSH 连接”。

  1. Copy the SSH public key to your clipboard.

    If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don't add any newlines or whitespace.

    $ pbcopy < ~/.ssh/id_ed25519.pub
      # Copies the contents of the id_ed25519.pub file to your clipboard

    Tip: If pbcopy isn't working, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.

    $ clip < ~/.ssh/id_ed25519.pub
      # Copies the contents of the id_ed25519.pub file to your clipboard

    Tip: If clip isn't working, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.

    $ cat ~/.ssh/id_ed25519.pub
      # Then select and copy the contents of the id_ed25519.pub file
      # displayed in the terminal to your clipboard

    Tip: Alternatively, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.

  2. 在任何页面的右上角,单击个人资料照片,然后单击“设置”。

    用户栏中的 Settings 图标

  3. In the "Access" section of the sidebar, click SSH and GPG keys.

  4. Click New SSH key or Add SSH key.

    SSH Key button

  5. In the "Title" field, add a descriptive label for the new key. For example, if you're using a personal laptop, you might call this key "Personal laptop".

  6. Paste your key into the "Key" field.

    The key field

  7. Click Add SSH key. The Add key button

  8. 如果出现提示,请确认你的帐户是否拥有 GitHub Enterprise Server 访问权限。 有关详细信息,请参阅“Sudo 模式”。

若要详细了解 GitHub CLI,请参阅“关于 GitHub CLI”。

Before you can use the GitHub CLI to add an SSH key to your account, you must authenticate to the GitHub CLI. For more information, see gh auth login in the GitHub CLI documentation.

To add an SSH authentication key to your GitHub account, use the ssh-key add subcommand, specifying your public key.

gh ssh-key add key-file

To include a title for the new key, use the -t or --title flag.

gh ssh-key add key-file --title "personal laptop"

If you generated your SSH key by following the instructions in "Generating a new SSH key", you can add the key to your account with this command.

gh ssh-key add ~/.ssh/id_ed25519.pub