Git 通过 SSH 推送会引发 "Permission Deined (publickey)" 错误

Git Push via SSH throws "Permission Deined (publickey)" error

我想通过 SSH 方法(而非 HTTP)推送我的个人 github 存储库。我有问题。我知道这个问题已经被问过很多次了,但我已经尝试了所有的解决方案,但都没有成功。

这是我到目前为止所做的...

  1. 我已经通过 ssh-keygen 创建了 private/public SSH 密钥对,相应地命名为 id_rsaid_rsa.pub
  2. 我还在设置 -> SSH 和 GPG 密钥下将 id_rsa.pub 的内容添加到 github。
  3. 我已经将 git@github.com:<username>/<repository>.git 版本的存储库地址添加为远程,如果我 运行 git remote -v
  4. 我还通过这些命令 eval "$(ssh-agent -s)" 将我的 public SSH 密钥添加到我的 ssh 代理,然后 ssh-add ~/.ssh/id_rsa
  5. 我也尝试重新生成我的 private/public 密钥,只是为了确保密钥文件没有问题。

完成所有这些后,当我 运行 这个命令 ssh -T git@github.com 它抛出这个错误: git@34.71.63.211: Permission denied (publickey).

我在这里迷路了,因为我已经尝试了所有可用的在线解决方案,但我不确定我在这里做错了什么。

出于故障排除的目的,我还 运行 命令:ssh -vT git@github.com,这是结果

OpenSSH_8.3p1, OpenSSL 1.1.1g  21 Apr 2020
debug1: Reading configuration data /home/<myusername>/.ssh/config
debug1: /home/<myusername>/.ssh/config line 7: Applying options for github.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to 34.71.63.211 [34.71.63.211] port 22.
debug1: Connection established.
debug1: identity file /home/<myusername>/.ssh/id_rsa type 0
debug1: identity file /home/<myusername>/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.3
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
debug1: match: OpenSSH_7.6p1 Ubuntu-4ubuntu0.3 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002
debug1: Authenticating to 34.71.63.211:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:UVaBouJo6Hj5PeRf1dBhvCPUFWU7fQKpSc7yIYbOE00
debug1: Host '34.71.63.211' is known and matches the ECDSA host key.
debug1: Found key in /home/<myusername>/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/<myusername>/.ssh/id_rsa RSA SHA256:D7Lcobs9L0s5wny5hodYEWxYsZtgOwtati2DgQV7s2M explicit agent
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/<myusername>/.ssh/id_rsa RSA SHA256:D7Lcobs9L0s5wny5hodYEWxYsZtgOwtati2DgQV7s2M explicit agent
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
git@34.71.63.211: Permission denied (publickey).

我解决了这个问题。在我的特定场景中,我有一个 SSH 配置文件(在 ~/.ssh/config 中),其中包含以下内容:

HostName 34.54.65.323 # google cloud IP address
Host gcloud
User faisal
IdentityFile ~/.ssh/id_rsa

解决方案是将 Host 行移动到 HostName 上方,就像这样。

Host gcloud
HostName 34.54.65.323 # google cloud IP address
User faisal
IdentityFile ~/.ssh/id_rsa

这很奇怪,因为它与 github 服务器无关,但它解决了问题。认为它可能对其他人有帮助。