SSH突然要求输入密码

Zep*_*eph 5 ssh ubuntu macos

注意:这是此 StackOverflow 问题的副本,因为 ali786 建议在这里会更好。

一般问题

我有我的计算机(Mac OSX 10.9.4)和一个远程服务器(Ubuntu 14.04.1 LTS),我以 root 用户身份登录。我曾经ssh-keygen创建一个 ssh 密钥(没有密码),将公钥添加到服务器的.ssh/authorized_keys. 然后,今天早些时候,又ssh开始要求我输入密码。

可能是什么原因造成的

我真的不确定是什么原因造成的 - 这些是我最好的猜测:

  • 当我不小心在那里复制了一个 git 存储库时,我确实不得不从远程服务器上的主目录中删除一些文件(我没有提交或从那里提交任何内容,只是复制了一些东西,然后rm'd 它们)。从我的命令历史中可以看出,我没有删除与 ssh 相关的文件,只有 .git、.gitignore 和各种 *.sw?vim 遗留下来的文件。
  • ssh-keygen -l -f ~/.ssh/authorized_keys在远程服务器上运行以查看(我认为)该文件中密钥的指纹。

相关日志

当我运行时ssh -v -i ~/.ssh/mykey_rsa root@serverip,我得到了这个:

OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /Users/myusername/.ssh/config
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 50: Applying options for *
debug1: Connecting to {ip address} [{ip address}] port 22.
debug1: Connection established.
debug1: identity file .ssh/mykey_rsa type 1
debug1: identity file .ssh/mykey_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version
OpenSSH_6.6.1p1 Ubuntu-2ubuntu2
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2 pat OpenSSH*
debug1:  Miscellaneous failure (see text)
No credentials cache file found

debug1:  An invalid name was supplied
unknown mech-code 0 for mech 1 2 752 43 14 2

debug1:  Miscellaneous failure (see text)
unknown mech-code 0 for mech 1 3 6 1 5 5 14

debug1:  Miscellaneous failure (see text)
unknown mech-code 2 for mech 1 3 6 1 4 1 311 2 2 10

debug1:  An unsupported mechanism was requested
unknown mech-code 0 for mech 1 3 5 1 5 2 7

debug1:  Miscellaneous failure (see text)
unknown mech-code 0 for mech 1 3 6 1 5 2 5

debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5-etm@openssh.com none
debug1: kex: client->server aes128-ctr hmac-md5-etm@openssh.com none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA {fingerprint}
debug1: Host '{ip address}' is known and matches the RSA host key.
debug1: Found key in /Users/myusername/.ssh/known_hosts:16
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: .ssh/otherkey_rsa
debug1: Authentications that can continue: publickey,password
debug1: Offering RSA public key: .ssh/mykey_rsa
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: password
root@serverip's password: 
Run Code Online (Sandbox Code Playgroud)

我发现它在我告诉它使用的密钥 (mykey_rsa) 之前尝试了不同的 ssh 密钥 (otherkey_rsa),这有点奇怪,但我对 ssh 的了解不够,无法很好地解析。

同时,远程服务器的帮助/var/log/auth.log

Aug 12 02:04:19 servername sshd[22147]: error: Could not load host key: /etc/ssh/ssh_host_ed25519_key
Aug 12 02:04:19 servername sshd[22147]: Authentication refused: bad ownership or modes for directory /root
Run Code Online (Sandbox Code Playgroud)

权限

本地/在我的电脑上:

~/                     drwxr-xr-x+ 105 myusername staff 3570 Aug 11 23:14
~/.ssh/                drwx------   13 myusername staff  442 Aug 11 23:14
~/.ssh/mykey_rsa.pub   -rw-r--r--    1 myusername staff  397 Aug  5 20:52
~/.ssh/mykey_rsa       -rw-------    1 myusername staff 1675 Aug  5 20:52
Run Code Online (Sandbox Code Playgroud)

远程/在服务器上:

~/                     drwxr-xr-x    8        501 staff 4096 Aug 12 02:16
~/.ssh/                drwx------    2       root root  4096 Aug 12 01:49
~/.ssh/authorized_keys -rw-------    1       root root   794 Aug 12 01:44
Run Code Online (Sandbox Code Playgroud)

远程在/etc/ssh/目录中:

/etc/ssh $ ls -la
drwxr-xr-x  2 root root   4096 Aug 12 11:01 .
drwxr-xr-x 96 root root   4096 Aug 12 01:40 ..
-rw-r--r--  1 root root 242091 Apr 14 08:13 moduli
-rw-r--r--  1 root root   1690 Apr 14 08:13 ssh_config
-rw-r--r--  1 root root   2528 Apr 17 15:43 sshd_config
-rw-------  1 root root    672 Aug  6 00:41 ssh_host_dsa_key
-rw-r--r--  1 root root    606 Aug  6 00:41 ssh_host_dsa_key.pub
-rw-------  1 root root    227 Aug  6 00:41 ssh_host_ecdsa_key
-rw-r--r--  1 root root    178 Aug  6 00:41 ssh_host_ecdsa_key.pub
-rw-------  1 root root   1679 Aug  6 00:41 ssh_host_rsa_key
-rw-r--r--  1 root root    398 Aug  6 00:41 ssh_host_rsa_key.pub
-rw-r--r--  1 root root    338 Apr 17 11:48 ssh_import_id
Run Code Online (Sandbox Code Playgroud)

所以看起来它无法加载的原因/etc/ssh/ssh_host_ed25519_key是它不存在。应该是?如果是这样,我该如何创建它?

我真的被这个难住了,非常感谢帮助!谢谢!

小智 3

auth.log确切地告诉您问题是什么:/root由 Remoteuser 或 root 以外的用户拥有,但它对您的登录至关重要。远程用户要么root,要么它的 .ssh 文件是指向 .ssh 下某处的符号链接/root

另外,我注意到您的 ls 行显示该行~/由用户 501 拥有。这将触发相同的失败。

我猜测发生的变化是你sshd_config现在指定的StrictModes