SSH 突然要求输入密码

12

常见问题

我有一台电脑(Mac OSX 10.9.4)和一个远程服务器(Ubuntu 14.04.1 LTS)。我使用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 user@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
user@serverip's password: 

我发现有点奇怪,它先尝试使用了不同的ssh密钥(otherkey_rsa),然后才是我告诉它要使用的那一个(mykey_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 remoteuser root  4096 Aug 12 01:49
~/.ssh/authorized_keys -rw-------    1 remoteuser root   794 Aug 12 01:44

如果您在Superuser或Askubuntu上发布此问题,您可能会得到很好的回应。 - smali
谢谢ali786!我在superuser上提出了同样的问题。希望好运! - Zeph
4个回答

8
aecolley在超级用户网站上指出的,答案就在我眼前。问题出在权限上,原因是uid为501的用户拥有了我的主目录。你会问为什么?因为我(有点意外地)使用rsync从我的笔记本电脑复制了一堆文件到那里,并且rsync似乎使用了我的笔记本电脑的用户ID(501,在OS X上默认)来执行该作业。

从服务器上的主目录中运行chown root .即可解决问题。


谢谢,我也遇到了同样的问题,当我在服务器升级时执行大型rsync作业。 - oemb1905
1
太棒了,很高兴这能帮到有需要的人! - Zeph

2
尝试在客户端ssh连接命令中添加-o 'PubkeyAcceptedKeyTypes +ssh-rsa'。我不知道为什么,但这使我能够再次连接到类似于您的情况(可能是由于我在服务器上某些错误配置的apt安装命令)。呼~

0

我刚刚在Yosemite上花了半个小时抓狂,因为我的所有权限都正确,密钥也放在正确的位置,配置也正确等等。

问题出在这里:我使用迁移助手将一些文件从这台机器转移到另一台机器,这导致交互式用户会话注销,但实际上并没有重新启动机器,所以当我重新登录并启动tmux时,它重新连接到一个存活的会话。显然,这导致它失去了访问Keychain的某种安全权限,我的密钥密码存储在那里,它不会再提示我。简单地重新启动tmux解决了这个问题!


0

也许对某些人有用。

我在Ubuntu上遇到了同样的问题:有一天,我试图推送到git仓库并得到了密码提示。在我的情况下,问题出在~/.ssh/config文件的错误权限(root是文件所有者)。

为了解决这个问题,我不得不更改文件所有者,然后重新登录。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接