Skip to main content

生成新的 SSH 密钥并将其添加到 ssh-agent

检查现有 SSH 密钥后,您可以生成新 SSH 密钥以用于身份验证,然后将其添加到 ssh-agent。

Platform navigation

关于 SSH 密钥密码

可以使用 SSH(安全外壳协议)在 GitHub.com 上的存储库中访问和写入数据。 通过 SSH 进行连接时,使用本地计算机上的私钥文件进行身份验证。 有关详细信息,请参阅“关于 SSH”。

生成 SSH 密钥时,可以添加密码以进一步保护密钥。 每当使用密钥时,都必须输入密码。 如果密钥具有密码并且你不想每次使用密钥时都输入密码,则可以将密钥添加到 SSH 代理。 SSH 代理会管理 SSH 密钥并记住你的密码。

如果您还没有 SSH 密钥,则必须生成新 SSH 密钥用于身份验证。 如果不确定是否已经拥有 SSH 密钥,您可以检查现有密钥。 有关详细信息,请参阅“检查现有 SSH 密钥”。

如果要使用硬件安全密钥向 GitHub 验证,则必须为硬件安全密钥生成新的 SSH 密钥。 使用密钥对进行身份验证时,您必须将硬件安全密钥连接到计算机。 有关详细信息,请参阅 OpenSSH 8.2 发行说明

生成新 SSH 密钥

可在本地计算机上生成新的 SSH 密钥。 生成密钥后,可将公钥添加到 GitHub.com 上的帐户中,以便通过 SSH 为 Git 操作启用身份验证。

注意:GitHub 通过在 2022 年 3 月 15 日删除旧的、不安全的密钥类型来提高安全性。

自该日期起,不再支持 DSA 密钥 (ssh-dss)。 无法在 GitHub.com上向个人帐户添加新的 DSA 密钥。

2021 年 11 月 2 日之前带有 valid_after 的 RSA 密钥 (ssh-rsa) 可以继续使用任何签名算法。 在该日期之后生成的 RSA 密钥必须使用 SHA-2 签名算法。 一些较旧的客户端可能需要升级才能使用 SHA-2 签名。

  1. 打开终端终端Git Bash

  2. 粘贴以下文本,将示例中使用的电子邮件替换为 GitHub 电子邮件地址。

    ssh-keygen -t ed25519 -C "your_email@example.com"
    

    注意:如果你使用的是不支持 Ed25519 算法的旧系统,请使用以下命令:

     ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    

    这将以提供的电子邮件地址为标签创建新 SSH 密钥。

    > Generating public/private ALGORITHM key pair.
    

    当系统提示您“Enter a file in which to save the key(输入要保存密钥的文件)”时,可以按 Enter 键接受默认文件位置。 请注意,如果以前创建了 SSH 密钥,则 ssh-keygen 可能会要求重写另一个密钥,在这种情况下,我们建议创建自定义命名的 SSH 密钥。 为此,请键入默认文件位置,并将 id_ALGORITHM 替换为自定义密钥名称。

    > Enter a file in which to save the key (/Users/YOU/.ssh/id_ALGORITHM): [Press enter]
    
    > Enter file in which to save the key (/c/Users/YOU/.ssh/id_ALGORITHM):[Press enter]
    
    > Enter a file in which to save the key (/home/YOU/.ssh/id_ALGORITHM):[Press enter]
    
  3. 在提示符下,键入安全密码。 有关详细信息,请参阅“使用 SSH 密钥密码”。

    > Enter passphrase (empty for no passphrase): [Type a passphrase]
    > Enter same passphrase again: [Type passphrase again]
    

将 SSH 密钥添加到 ssh-agent

在向 ssh 代理添加新的 SSH 密钥以管理您的密钥之前,您应该检查现有 SSH 密钥并生成新的 SSH 密钥。 将 SSH 密钥添加到该代理时,应使用默认的 macOS ssh-add 命令,而不是使用通过 macportshomebrew 或某些其他外部来源安装的应用程序。

  1. 在后台启动 ssh 代理。

    $ eval "$(ssh-agent -s)"
    > Agent pid 59566
    

    根据您的环境,您可能需要使用不同的命令。 例如,在启动 ssh-agent 之前,你可能需要通过运行 sudo -s -H 根访问,或者可能需要使用 exec ssh-agent bashexec ssh-agent zsh 运行 ssh-agent。

  2. 如果你使用的是 macOS Sierra 10.12.2 或更高版本,则需要修改 ~/.ssh/config 文件以自动将密钥加载到 ssh-agent 中并在密钥链中存储密码。

    • 首先,检查你的 ~/.ssh/config 文件是否在默认位置。

      $ open ~/.ssh/config
      > The file /Users/YOU/.ssh/config does not exist.
      
    • 如果文件不存在,请创建该文件。

      touch ~/.ssh/config
      
    • 打开你的 ~/.ssh/config 文件,然后修改文件以包含以下行。 如果您的 SSH 密钥文件与示例代码具有不同的名称或路径,请修改文件名或路径以匹配您当前的设置。

      Host github.com
        AddKeysToAgent yes
        UseKeychain yes
        IdentityFile ~/.ssh/id_ed25519
      

      注意:

      • 如果你选择不向密钥添加密码,应该省略 UseKeychain 行。

      • 如果看到 Bad configuration option: usekeychain 错误,请在配置的 Host *.github.com 部分添加额外的一行。

        Host github.com
          IgnoreUnknown UseKeychain
        
  3. 将 SSH 私钥添加到 ssh-agent 并将密码存储在密钥链中。 如果使用其他名称创建了密钥,或要添加具有其他名称的现有密钥,请将命令中的 id_ed25519 替换为私钥文件的名称。

    ssh-add --apple-use-keychain ~/.ssh/id_ed25519
    

    注意:当你将 SSH 密钥添加到 ssh-agent 时,--apple-use-keychain 选项会将密码存储在你的密钥链中。 如果选择不向密钥添加密码,请运行命令,而不使用 --apple-use-keychain 选项。

    选项 --apple-use-keychain 位于 Apple 的 ssh-add 标准版本中。 在 Monterey (12.0) 之前的 macOS 版本中,--apple-use-keychain--apple-load-keychain 标志分别使用语法 -K-A

    如果您没有安装 Apple 的 ssh-add 标准版本,可能会收到错误消息。 有关详细信息,请参阅“错误:ssh-add:非法选项 -- apple-use-keychain”。

    如果系统继续提示你输入密码,则可能需要将命令添加到 ~/.zshrc 文件(或 bash 对应的 ~/.bashrc 文件)。

  4. 将 SSH 公钥添加到 GitHub 上的帐户。 有关详细信息,请参阅“新增 SSH 密钥到 GitHub 帐户”。

如果已安装 GitHub Desktop,可使用它克隆存储库,而无需处理 SSH 密钥。

  1. 在新的“管理员提升”__ PowerShell 窗口中,确保 ssh-agent 正在运行。 可以使用“使用 SSH 密钥密码”中的“自动启动 ssh agent”说明,或者手动启动它:

    # start the ssh-agent in the background
    Get-Service -Name ssh-agent | Set-Service -StartupType Manual
    Start-Service ssh-agent
    
  2. 在无提升权限的终端窗口中,将 SSH 私钥添加到 ssh-agent。 如果使用其他名称创建了密钥,或要添加具有其他名称的现有密钥,请将命令中的 id_ed25519 替换为私钥文件的名称。

    ssh-add c:/Users/YOU/.ssh/id_ed25519
    
  3. 将 SSH 公钥添加到 GitHub 上的帐户。 有关详细信息,请参阅“新增 SSH 密钥到 GitHub 帐户”。

  1. 在后台启动 ssh 代理。

    $ eval "$(ssh-agent -s)"
    > Agent pid 59566
    

    根据您的环境,您可能需要使用不同的命令。 例如,在启动 ssh-agent 之前,你可能需要通过运行 sudo -s -H 根访问,或者可能需要使用 exec ssh-agent bashexec ssh-agent zsh 运行 ssh-agent。

  2. 将 SSH 私钥添加到 ssh-agent。

    如果使用其他名称创建了密钥,或要添加具有其他名称的现有密钥,请将命令中的 id_ed25519 替换为私钥文件的名称。

    ssh-add ~/.ssh/id_ed25519
    
  3. 将 SSH 公钥添加到 GitHub 上的帐户。 有关详细信息,请参阅“新增 SSH 密钥到 GitHub 帐户”。

为硬件安全密钥生成新的 SSH 密钥

如果您使用 macOS 或 Linux, 在生成新的 SSH 密钥之前,您可能需要更新 SSH 客户端或安装新的 SSH 客户端。 有关详细信息,请参阅“错误:未知密钥类型”。

  1. 将硬件安全密钥插入计算机。

  2. 打开终端终端Git Bash

  3. 粘贴以下文本,将示例中使用的电子邮件替换为与 GitHub 中帐户关联的电子邮件地址。

    ssh-keygen -t ed25519-sk -C "your_email@example.com"
    
    ssh-keygen -t ed25519-sk -C "your_email@example.com"
    
    ssh-keygen -t ed25519-sk -C "your_email@example.com"
    

    注意:如果命令失败,并且你收到错误 invalid formatfeature not supported,,则表明你可能在使用不支持 Ed25519 算法的硬件安全密钥。 请输入以下命令。

     ssh-keygen -t ecdsa-sk -C "your_email@example.com"
    
  4. 出现提示时,请触摸硬件安全密钥上的按钮。

  5. 当提示您“Enter a file in which to save the key(输入要保存密钥的文件)”时,按 Enter 接受默认文件位置。

    > Enter a file in which to save the key (/Users/YOU/.ssh/id_ed25519_sk): [Press enter]
    
    > Enter a file in which to save the key (c:\Users\YOU\.ssh\id_ed25519_sk):[Press enter]
    
    > Enter a file in which to save the key (/home/YOU/.ssh/id_ed25519_sk):[Press enter]
    
  6. 当提示你输入密码时,请按 Enter。

    > Enter passphrase (empty for no passphrase): [Type a passphrase]
    > Enter same passphrase again: [Type passphrase again]
    
  7. 将 SSH 公钥添加到 GitHub 上的帐户。 有关详细信息,请参阅“新增 SSH 密钥到 GitHub 帐户”。