我们经常发布文档更新,此页面的翻译可能仍在进行中。有关最新信息,请访问英文文档。如果此页面上的翻译有问题,请告诉我们

此版本的 GitHub Enterprise 已停止服务 March 02, 2021. 即使针对重大安全问题,也不会发布补丁。 要获得更好的性能、改进的安全性和新功能,请升级到 GitHub Enterprise 的最新版本。 如需升级方面的帮助,请联系 GitHub Enterprise 支持

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

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

本文内容

对 Git 是否应该使用 sudo 命令?

不应对 Git 使用 sudo 命令。 如果有很好的原因必须使用 sudo,请确保对每个命令使用它(可能使用 su 获取 shell 作为该点的根更好)。 如果生成 SSH 密钥而不使用 sudo,则尝试使用 sudo git push 而不使用生成的相同密钥。

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

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

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

$ ssh -vT git@hostname
> 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 hostname port 22.

应连接端口 22。

始终使用 "git" 用户

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

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

如果连接失败且您通过 GitHub Enterprise Server 用户名使用远程 URL,可以更改远程 URL 以使用 "git" 用户

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

$ ssh -T git@hostname
> Hi username! You've successfully authenticated...

确保您有使用的密钥

The ssh-add command should print out a long string of numbers and letters. If it does not print anything, you will need to generate a new SSH key and associate it with GitHub Enterprise Server.

Tip: On most systems the default private keys (~/.ssh/id_rsa and ~/.ssh/identity) are automatically added to the SSH authentication agent. You shouldn't need to run ssh-add path/to/key unless you override the file name when you generate a key.

Getting more details

You can also check that the key is being used by trying to connect to git@[hostname]:

$ ssh -vT git@hostname
> ...
> 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@hostname
> ...
> 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 Enterprise Server 提供公钥才可建立安全连接。

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

警告:如果在 GitHub Enterprise Server 上看到您不熟悉的 SSH 密钥,请立即删除并联系 您的站点管理员 寻求进一步的帮助。 无法识别的公钥可能表示安全问题。 更多信息请参阅“审查 SSH 密钥”。