SSH 突然要求输入密码

SSH 突然要求输入密码

注意:这是这个 StackOverflow 问题,因为 ali786 建议这里这样做会更好。

一般问题

我有一台电脑(Mac OSX 10.9.4)和一台远程服务器(Ubuntu 14.04.1 LTS),我以 root 身份通过 ssh 进入该服务器。我曾经ssh-keygen创建一个 ssh 密钥(没有密码),将公钥添加到服务器的.ssh/authorized_keys,一切正常。然后,今天早些时候,它又ssh开始要求我输入密码。

可能的原因是什么

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

  • 当我意外地将 git 存储库复制到远程服务器上时,我确实不得不从主目录中删除一些文件(我没有提交或从那里执行任何操作,只是复制了一些内容然后rm删除了它们)。从我的命令历史记录中可以看出,我删除的不是与 ssh 相关的文件,只是 .git、.gitignore 和 vim 留下的各种 *.sw? 文件。
  • 我运行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 [email protected] none
debug1: kex: client->server aes128-ctr [email protected] 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: 

我发现有点奇怪,它在我告诉它使用的 ssh 密钥(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

权限

本地/在我的计算机上:

~/                     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

远程/在服务器上:

~/                     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

远程/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

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

我真的对此感到困惑,非常希望得到帮助!谢谢!

答案1

auth.log正在告诉您确切的问题所在:/root它归 remoteuser 或 root 以外的用户所有,但它对您的登录至关重要。remoteuserroot,或者它的 .ssh 文件是指向 下某处的符号链接/root

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

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

相关内容