OpenSSH 伺服器拒絕接受金鑰認證

OpenSSH 伺服器拒絕接受金鑰認證

我嘗試在我的新伺服器上使用公鑰身份驗證,但遇到了這個問題。

$ ssh -v -i .ssh/server 192.168.1.100
OpenSSH_5.6p1, OpenSSL 0.9.8r 8 Feb 2011
debug1: Reading configuration data .ssh/config
debug1: Applying options for *
debug1: Reading configuration data /etc/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.1.100 [192.168.1.100] port 22.
debug1: Connection established.
debug1: identity file .ssh/server type -1
debug1: identity file .ssh/server-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.8p1 Debian-1ubuntu3
debug1: match: OpenSSH_5.8p1 Debian-1ubuntu3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.6
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 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: Host '192.168.1.100' is known and matches the RSA host key.
debug1: Found key in .ssh/known_hosts:1
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: Trying private key: .ssh/server
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: password

然後我必須輸入密碼才能登入。

但是,如果我已經有一個會話連接到該伺服器(透過密碼連接),則以下連接將使用密鑰身份驗證來避免密碼輸入。

如果尚未建立 SSH 連接,則無需輸入密碼就無法連接。

/usr/sbin/sshd這對我來說真的很奇怪,我檢查了新伺服器和其他正常伺服器之間的MD5 ,它是相同的。然後我只是從其他正常伺服器複製/etc/ssh/sshd_config到新伺服器,然後運行service ssh restart。問題仍然存在。

我該如何解決這個問題?

答案1

檢查.ssh用戶端電腦上的資料夾及其中的檔案是否只能由擁有者 ( chmod -R 600 .ssh) 讀取,且資料夾和檔案的擁有者是否正確(chown如有必要,請使用命令)。

還要檢查authorized_keys伺服器上的資料夾和檔案(可能位於/root/.ssh嘗試登入的使用者的主資料夾中),以確保其權限和所有者以相同的方式設定。


編輯:基於更多回饋(以及一些猜測!) - 您可以檢查/etc/ssh/sshd_config並查看以下參數是否設定如下。如果沒有,請嘗試編輯它。

AuthorizedKeysFile /home/%u/.ssh/authorized_keys

請注意,這假設您不以 root 身分遠端登入

答案2

就我而言,主目錄的權限是775而不是0755或更低。

authorized_keys 檔案的完整路徑,即/home/user/.ssh/必須為0755或更低。

答案3

我透過從 .ssh 中刪除來修復了我自己的此錯誤情況id_rsa.pub

我從另一台機器複製id_rsa並將其分發到幾個虛擬客戶端。因此,id_rsaid_rsa.pub實際上是不同的密鑰,完全阻止了使用id_rsa

但沒有錯誤訊息清楚地表明這一點。我基本上是偶然發現的,試圖讓不同的機器進入相同的狀態。

答案4

經過一番折騰後,我得到了問題的解決方案:

使用者的主目錄不應具有權限777或全域可寫入。如果是這種情況,SSH 金鑰驗證將失敗,您必須輸入密碼才能登入。

相關內容