尝试通过 SSH 连接到远程服务器但仍然提示输入密码

尝试通过 SSH 连接到远程服务器但仍然提示输入密码

我使用 ssh-keygen 生成了一个 ssh 密钥对,使用 ssh-copy-id 到服务器并检查公钥是否已添加到authorized_keys,但是当我尝试使用 ssh -i ~/.ssh/MY_KEY user@ 连接时服务器仍然要求我输入密码。我在服务器中没有根权限,但我的 .ssh 目录只有“authorized_keys”和“known_hosts”文件,两者都有公钥“MY_KEY”。我已经检查了 /User、/.ssh 以及 /.ssh 中的两个文件的所有权限,但我无权访问 sshd_config

这就是我尝试连接时得到的结果

ssh -v -i ~/.ssh/MY_KEY user@serverIP
debug1: Reading configuration data /Users/user/.ssh/config
debug1: /Users/user/.ssh/config line 18: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Connecting to serverIP [serverIP] port 22.
debug1: Connection established.
debug1: identity file /Users/user/.ssh/MY_KEY type 0
debug1: identity file /Users/user/.ssh/MY_KEY-cert type -1
debug1: identity file /Users/user/.ssh/gitlabkey type 3
debug1: identity file /Users/user/.ssh/gitlabkey-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 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 serverIP:22 as 'user'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: [email protected]
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:oF1yjVJjhgf9N7u84hk8QqZrY
debug1: Host 'serverIP' is known and matches the ECDSA host key.
debug1: Found key in /Users/user/.ssh/known_hosts:17
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: Will attempt key: /Users/user/.ssh/MY_KEY RSA SHA256:W3JxHToIyelBpl4 explicit
debug1: Will attempt key: /Users/user/.ssh/gitlabkey ED25519 SHA256:KAd+YwkK6dWptbtLvWg explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/user/.ssh/MY_KEY RSA SHA256:W3JxHToIyelB6Upl4 explicit
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: /Users/user/.ssh/gitlabkey ED25519 SHA256:KAd+YwkK6dWtvWg explicit
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: password
user@serverIP's password:

老实说,我不知道还能尝试什么。我不明白为什么它尝试正确的密钥(MY_KEY)但它显然失败了。

答案1

对于任何遇到同样问题的人...经过很长时间我可以解决它。

我厌倦了尝试,所以我删除了authorized_keys和known_hosts,我所拥有的只是空的.ssh目录。在那一刻,我与服务器断开了连接,并以为我破坏了一切,但由于我在 Mac 中,通过这样做https://coderwall.com/p/8ag5aq/ssh-broken-pipe-fix-mac-os-x我可以再次连接。

我只需从 mac 再次执行 ssh-copy-id 命令,它就会在服务器上自动创建文件“authorized_keys”而不是“known_hosts”。

就这样,我然后运行 ​​ssh -i /path/to/MYKEY user@serverIP ,它就像一个魅力,没有提示输入密码。

相关内容