Skip to main content

错误:权限被拒绝(公钥)

“权限被拒绝”错误表示服务器拒绝了您的连接。 可能有多个原因,最常见的如下所述。

Platform navigation

是否应将 sudo 命令或提升的权限与 Git 一起使用?

不应将 sudo 命令或提升的权限(如管理员权限)与 Git 一起使用。 如果有非常充分的理由必须使用 sudo,请确保对每个命令都使用它(可能使用 su 获取 shell 作为该点的根更好)。 如果在不使用 sudo 的情况下生成 SSH 密钥,然后尝试使用 sudo git push 之类的命令,则将不会使用生成的相同密钥。

检查是否连接到正确的服务器

我们知道,键入 Url 很麻烦。 请注意您键入的内容;您无法连接到 "githib.com" 或 "guthub.com"。 有某些情况下,公司网络可能导致解析 DNS 记录有问题。

为确保连接到正确的域,可以输入以下命令:

$ ssh -vT git@github.com
> OpenSSH_8.1p1, LibreSSL 2.7.3
> debug1: Reading configuration data /Users/YOU/.ssh/config
> debug1: Reading configuration data /etc/ssh/ssh_config
> debug1: /etc/ssh/ssh_config line 47: Applying options for *
> debug1: Connecting to github.com port 22.

应在端口 22 上建立连接,除非覆盖设置以使用通过 HTTPS 的 SSH 连接

始终使用 "git" 用户

所有连接(包括远程 URL 的连接)必须以 "git" 用户进行。 如果尝试以 GitHub 用户名连接,将会失败:

$ ssh -T GITHUB-USERNAME@github.com
> Permission denied (publickey).

如果连接失败且你通过 GitHub 用户名使用远程 URL,则可以更改远程 URL 以使用“git”用户

应键入以下命令来验证连接:

$ ssh -T git@github.com
> Hi USERNAME! You've successfully authenticated...

确保您有使用的密钥

  1. 打开终端终端Git Bash

  2. 确认您的私钥已生成并加载到 SSH。

    # start the ssh-agent in the background
    $ eval "$(ssh-agent -s)"
    > Agent pid 59566
    $ ssh-add -l -E sha256
    > 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)
    

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

  1. 如果使用的是 Git Bash,请打开 ssh-agent:

    # start the ssh-agent in the background
    $ eval "$(ssh-agent -s)"
    > Agent pid 59566
    

    如果使用其他终端提示(例如 Git for Windows),请打开 ssh-agent:

    ```shell
    # start the ssh-agent in the background
    $ eval $(ssh-agent -s)
    > Agent pid 59566
    ```
    

    注意:**** 上述 eval 命令会在环境中手动启动 ssh-agent。 如果 ssh-agent 已作为后台系统服务运行,则这些命令可能会失败。 如果发生这种情况,建议检查环境的相关文档。

  2. 确认您的私钥已生成并加载到 SSH。

    $ ssh-add -l -E sha256
    > 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)
    
  1. 打开终端终端Git Bash

  2. 确认您的私钥已生成并加载到 SSH。

    $ ssh-add -l -E sha256
    > 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)
    

ssh-add 命令应打印出一个长的数字和字母字符串。 如果未打印出任何内容,则需要生成新 SSH 密钥并将其与 GitHub 关联。

提示:在大多数系统中,默认私钥(~/.ssh/id_rsa~/.ssh/identity)会自动添加到 SSH 身份验证代理中。 应无需运行 ssh-add path/to/key,除非在生成密钥时覆盖文件名。

获取更多详细信息

也可通过尝试连接到 git@github.com 来检查使用的密钥:

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/YOU/.ssh/id_rsa type -1
> debug1: identity file /Users/YOU/.ssh/id_rsa-cert type -1
> debug1: identity file /Users/YOU/.ssh/id_dsa type -1
> debug1: identity file /Users/YOU/.ssh/id_dsa-cert type -1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Trying private key: /Users/YOU/.ssh/id_rsa
> debug1: Trying private key: /Users/YOU/.ssh/id_dsa
> debug1: No more authentication methods to try.
> Permission denied (publickey).

在该示例中,我们没有任何密钥供 SSH 使用。 "identity file" 行末的 "-1" 表示 SSH 找不到可使用的文件。 后面的 "Trying private key" 行也表示未找到文件。 如果文件存在,这些行将分别是 "1" 和 "Offering public key":

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/YOU/.ssh/id_rsa type 1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Offering RSA public key: /Users/YOU/.ssh/id_rsa

确认公钥已附加到您的帐户

必须向 GitHub 提供公钥才可建立安全连接。

  1. 打开终端。

  2. 在后台启动 SSH 代理程序。

    $ eval "$(ssh-agent -s)"
    > Agent pid 59566
    
  3. 找到并记录公钥指纹。

    $ ssh-add -l -E sha256
    > 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)
    
  4. 在任何页面的右上角,单击个人资料照片,然后单击“设置”。

    Screenshot of a user's account menu on GitHub. The menu item "Settings" is outlined in dark orange.

  5. 在边栏的“访问”部分中,单击 “SSH 和 GPG 密钥”。

  6. 将 SSH 密钥列表与 ssh-add 命令的输出进行比较。

  1. 打开命令行。

  2. 在后台启动 SSH 代理程序。

    $ ssh-agent -s
    > Agent pid 59566
    
  3. 找到并记录公钥指纹。

    $ ssh-add -l -E sha256
    > 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)
    
  4. 在任何页面的右上角,单击个人资料照片,然后单击“设置”。

    Screenshot of a user's account menu on GitHub. The menu item "Settings" is outlined in dark orange.

  5. 在边栏的“访问”部分中,单击 “SSH 和 GPG 密钥”。

  6. 将 SSH 密钥列表与 ssh-add 命令的输出进行比较。

  1. 打开终端。

  2. 在后台启动 SSH 代理程序。

    $ eval "$(ssh-agent -s)"
    > Agent pid 59566
    
  3. 找到并记录公钥指纹。 如果使用的是 OpenSSH 6.7 或更早版本:

    $ ssh-add -l
    > 2048 a0:dd:42:3c:5a:9d:e4:2a:21:52:4e:78:07:6e:c8:4d /Users/USERNAME/.ssh/id_rsa (RSA)
    

    如果使用的是 OpenSSH 6.8 或更新版本:

    $ ssh-add -l -E md5
    > 2048 MD5:a0:dd:42:3c:5a:9d:e4:2a:21:52:4e:78:07:6e:c8:4d /Users/USERNAME/.ssh/id_rsa (RSA)
    
  4. 在任何页面的右上角,单击个人资料照片,然后单击“设置”。

    Screenshot of a user's account menu on GitHub. The menu item "Settings" is outlined in dark orange.

  5. 在边栏的“访问”部分中,单击 “SSH 和 GPG 密钥”。

  6. 将 SSH 密钥列表与 ssh-add 命令的输出进行比较。

如果在 GitHub 中未看到公钥,则需要将 SSH 密钥添加到 GitHub 并将其与计算机关联。

警告:如果在 GitHub 上看到不熟悉的 SSH 密钥,请立即删除并联系 GitHub 支持 寻求进一步的帮助。 无法识别的公钥可能表示安全问题。 有关详细信息,请参阅“审查 SSH 密钥”。