跳至主要内容

错误:权限被拒绝(publickey)

“权限被拒绝”错误表示服务器拒绝了您的连接。可能有多种原因,下面解释了最常见的几种情况。

平台导航

在使用 Git 时是否应该使用 sudo 命令或提升的权限?

您不应该在 Git 中使用 sudo 命令或提升的权限(例如管理员权限)。

如果您有 非常充分的理由 必须使用 sudo,则需确保在每个命令中都使用它。如果您在不使用 sudo 的情况下 生成 SSH 密钥,随后尝试使用类似 sudo git push 的命令,您将不会使用生成的相同密钥。

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

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

Shell
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 端口,除非您覆盖设置以使用 SSH over HTTPS

始终使用 “git” 用户

所有连接(包括远程 URL 的连接)都必须以 “git” 用户身份进行。如果尝试使用您的 GitHub 用户名连接,将会失败

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

如果连接失败且您使用了包含 GitHub 用户名的远程 URL,您可以 更改远程 URL 为使用 “git” 用户

您可以通过输入以下命令来验证连接

Shell
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. If you are using Git Bash,请启用 ssh-agent

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

    If you are using another terminal prompt,如 Git for Windows,请启用 ssh-agent

    # 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 来检查密钥是否正在使用

Shell
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”,如下面的输出所示

> ...
> 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. 在 GitHub 任意页面的右上角,点击您的个人资料图片,然后点击 Settings

  5. 在侧边栏的“Access”部分,点击 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. 在 GitHub 任意页面的右上角,点击您的个人资料图片,然后点击 Settings

  5. 在侧边栏的“Access”部分,点击 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. 在 GitHub 任意页面的右上角,点击您的个人资料图片,然后点击 Settings

  5. 在侧边栏的“Access”部分,点击 SSH 和 GPG 密钥

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

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

警告

如果您在 GitHub 上看到不熟悉的 SSH 密钥,请立即删除并通过 GitHub 支持门户 与我们联系获取进一步帮助。未识别的公钥可能表明潜在的安全隐患。欲了解更多信息,请参阅 审查您的 SSH 密钥

© . This site is unofficial and not affiliated with GitHub, Inc.