尽管配置和绕过配置,ssh 使用了错误的密钥

时间:2021-05-18 17:37:19

标签: git ssh

当我尝试使用 ssh 连接到 github 时,我得到以下信息:

me@host ~/repos/my_repo (main) $ git pull
Warning: Permanently added the RSA host key for IP address '140.82.114.4' to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

github docs 建议运行以下命令来调试 ssh 连接,这会给我以下输出。请注意,github 密钥位于 ~/.ssh/github_id_rsa 中,但它似乎从未尝试使用该密钥,即使我的配置将其明确列为用于该主机的密钥:

$ ssh -vT git@github.com
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/me/.ssh/config
debug1: /Users/me/.ssh/config line 21: Applying options for *
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.
debug1: Connection established.
debug1: identity file /Users/me/.ssh/id_rsa type 0
debug1: identity file /Users/me/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: Remote protocol version 2.0, remote software version babeld-74336b10
debug1: no match: babeld-74336b10
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: rsa-sha2-512
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: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /Users/me/.ssh/known_hosts:10
Warning: Permanently added the RSA host key for IP address '140.82.112.4' to the list of known hosts.
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: /Users/me/.ssh/id_rsa RSA SHA256:lKbkgpDG6HpW4U1pB8nQDxkAj1M4asDmdQclOe4FEJY explicit agent
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa,ssh-dss>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/me/.ssh/id_rsa RSA SHA256:lKbkgpDG6HpW4U1pB8nQDxkAj1M4asDmdQclOe4FEJY explicit agent
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).

我添加了 -F /dev/null 来绕过我的配置文件,看看它是否是我的配置有问题):

$ ssh -vT git@github.com -F /dev/null
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /dev/null
debug1: Connecting to github.com port 22.
debug1: Connection established.
debug1: identity file /Users/me/.ssh/id_rsa type 0
debug1: identity file /Users/me/.ssh/id_rsa-cert type -1
debug1: identity file /Users/me/.ssh/id_dsa type -1
debug1: identity file /Users/me/.ssh/id_dsa-cert type -1
debug1: identity file /Users/me/.ssh/id_ecdsa type -1
debug1: identity file /Users/me/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/me/.ssh/id_ed25519 type -1
debug1: identity file /Users/me/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/me/.ssh/id_xmss type -1
debug1: identity file /Users/me/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: Remote protocol version 2.0, remote software version babeld-74336b10
debug1: no match: babeld-74336b10
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: rsa-sha2-512
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: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /Users/me/.ssh/known_hosts:10
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: /Users/me/.ssh/id_rsa RSA SHA256:lKbkgpDG6HpW4U1pB8nQDxkAj1M4asDmdQclOe4FEJY agent
debug1: Will attempt key: /Users/me/.ssh/id_dsa
debug1: Will attempt key: /Users/me/.ssh/id_ecdsa
debug1: Will attempt key: /Users/me/.ssh/id_ed25519
debug1: Will attempt key: /Users/me/.ssh/id_xmss
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa,ssh-dss>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/me/.ssh/id_rsa RSA SHA256:lKbkgpDG6HpW4U1pB8nQDxkAj1M4asDmdQclOe4FEJY agent
debug1: Authentications that can continue: publickey
debug1: Trying private key: /Users/me/.ssh/id_dsa
debug1: Trying private key: /Users/me/.ssh/id_ecdsa
debug1: Trying private key: /Users/me/.ssh/id_ed25519
debug1: Trying private key: /Users/me/.ssh/id_xmss
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).

我尝试从 ~/.ssh/known_hosts 中删除 github 行,并尝试从我的 github 设置中删除我的密钥,生成一个新密钥,并上传新密钥。

我没有想法了。如何解决此身份验证问题?

编辑

尽管有和没有 ssh 配置都会发生错误,但这是我的配置文件:

Host github
    User git
    HostName github.com
    IdentityFile ~/.ssh/github_id_rsa
Host foo
    HostName foo.example.com
Host *
    AddKeysToAgent yes
    User me
    IdentityFile ~/.ssh/id_rsa
    IdentitiesOnly yes

1 个答案:

答案 0 :(得分:1)

TL;DR 您的配置文件“github”条目应如下所示:

Host github.com   <--Note "github.com"
    User git
    IdentityFile ~/.ssh/github_id_rsa

您的直接问题是 ssh 没有尝试“github_id_rsa”密钥,因为您的“Host github”配置条目不适用于与“github.com”的连接。 您的“github”条目目前如下所示:

Host github
    User git
    HostName github.com
    IdentityFile ~/.ssh/github_id_rsa

这仅适用于运行“ssh git@github”之类的东西,特别是将主机指定为“github”。在命令行中指定主机为“github.com”时不适用。

如果你想保留你的配置文件,那么你应该运行“ssh github”或“ssh git@github”而不是“...@github.com”。如果您在命令行中将主机指定为“github”,那么它将匹配您配置文件中的“github”部分(并且配置文件将设置正确的实际主机名“github.com”)。