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 和各種 *.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 [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: 

我覺得有點奇怪,它在我告訴它使用的密鑰(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 以外的使用者擁有,但它對您的登入至關重要。遠端使用者root 或其 .ssh 檔案是指向 .ssh 下某處的符號連結/root

另外,我注意到您的 ls 行顯示該行~/由使用者 501 擁有。

我猜測發生的變化是你sshd_config現在指定的StrictModes

相關內容