简短回答:我的 中有一个拼写错误.ssh/config。

简短回答:我的 中有一个拼写错误.ssh/config。

过去几天,我在尝试连接 GitHub 帐户时遇到了麻烦。

我有:

  • 按照指南产生钥匙和添加到 github
  • 按照故障排除指南没有权限(我的本地 MD5 哈希与我的 github 帐户中的 MD5 哈希匹配。)
  • 测试 ssh 连接时尝试详细输出

以下是ssh -vT [email protected]

OpenSSH_7.5p1, OpenSSL 1.0.2l  25 May 2017
debug1: Reading configuration data /Users/[obfuscated]/.ssh/config
debug1: /Users/[obfuscated]/.ssh/config line 13: Applying options for *
debug1: /Users/[obfuscated]/.ssh/config line 24: Applying options for github.com
debug1: Reading configuration data /usr/local/etc/ssh/ssh_config
debug1: Connecting to github.com [192.30.253.112] port 22.
debug1: fd 5 clearing O_NONBLOCK
debug1: Connection established.
debug1: identity file /Users/[obfuscated]/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/[obfuscated]/.ssh/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.5
debug1: Remote protocol version 2.0, remote software version libssh_0.7.0
debug1: no match: libssh_0.7.0
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: [email protected]
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: [email protected]
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: [email protected]
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
The authenticity of host 'github.com (192.30.253.112)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).

我注意到一件有趣的事情,那就是我的调试中没有debug1: Offering RSA public key: /Users/you/.ssh/id_rsadebug1: Trying private key: /Users/you/.ssh/id_rsa。几乎就像是跳过了那部分。debug1: identity file /Users/[obfuscated]/.ssh/id_rsa type 1但是我确实有,所以看起来它已经找到了文件。

这是我的.ssh/config

Host localhost
  ControlMaster no

# default for all hosts
Host *
     ConnectTimeout 30
     ServerAliveInterval 30
     ControlMaster auto
     ControlPersist 60s
     HashKnownHosts yes
     GSSAPIAuthentication no
     IdentitiesOnly yes
     Compression yes

# github user
Host github.com
     HostName github.com
     User git
     PreferredAuthentications publicKey
     IdentityFile ~/.ssh/id_rsa

# the local raspberry pi dns ad block server
Host pi
     HostName [obfuscated]
     User pi
     IdentityFile ~/.ssh/pi_rsa

ssh 文件的权限

 8 -rw-r--r--   1 [obfuscated]  staff   802B Sep  9 21:45 config
 8 -rw-------   1 [obfuscated]  staff   3.2K Sep  9 21:16 id_rsa
 8 -rw-r--r--   1 [obfuscated]  staff   747B Sep  9 21:16 id_rsa.pub
 8 -rw-r--r--   1 [obfuscated]  staff   885B Sep  9 21:37 known_hosts
 8 -rw-------   1 [obfuscated]  staff   1.7K Oct 21  2016 pi_rsa
 8 -rw-r--r--   1 [obfuscated]  staff   405B Oct 21  2016 pi_rsa.pub

答案1

简短回答:我的 中有一个拼写错误.ssh/config

PreferredAuthentications publicKey

应该有小写k

PreferredAuthentications publickey

我如何发现这个问题:

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

了解这一点后,我删除了我的自定义配置,github.com以便 SSH 身份验证代理默认使用我的密钥。

这样我就可以连接了。然后我一次添加一行,直到我发现它PreferredAuthentications publicKey不起作用。我用谷歌搜索语法,发现问题出在大写字母上K

相关内容