SSH 公鑰登入時的奇怪行為

SSH 公鑰登入時的奇怪行為

我真的被困在這裡了。我一直在嘗試使用公鑰從本地 ssh 進入我的 ec2 伺服器,但它不起作用。 -> 我的權限被拒絕(公鑰)。

設定如下: 本機:產生公鑰對並複製 id_rsa.pub 的內容。遠端:使用 PEM 檔案 ssh 進入我的 EC2 伺服器,並將 id_rsa.pub 內容貼到 .ssh 資料夾中新的授權金鑰檔案行中。

應該工作正常嗎?我注意到一個常見的錯誤是權限,但我的似乎設定正確:

遠端權限:

drwx------ 2 ec2-user ec2-user  4096 Jul 23 04:00 .ssh

在 .ssh 中:

-rw-r--r-- 1 ec2-user ec2-user  404 Jul 24 03:19 id_rsa.pub
-rw------- 1 ec2-user ec2-user 1679 Jul 24 03:19 id_rsa
-rw------- 1 ec2-user ec2-user  529 Jul 26 20:53 authorized_keys

當地的:

drwx------    10 robvanhaaren  staff    340 Jul 26 18:43 .ssh

在 .ssh 中:

-rw-r--r--  1 robvanhaaren  staff   404 Jul 26 21:28 id_rsa.pub
-rw-------  1 robvanhaaren  staff  1766 Jul 26 21:28 id_rsa
-rw-r--r--  1 robvanhaaren  staff  5987 Jul 26 21:29 known_hosts

但是當我跑步時:

Robs-MacBook-Air-2:.ssh robvanhaaren$ ssh ec2-54-85-62-99.compute-1.amazonaws.com -l ec2-user -v

它返回:

OpenSSH_5.9p1, OpenSSL 0.9.8y 5 Feb 2013
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to ec2-54-85-62-99.compute-1.amazonaws.com [54.85.62.99] port 22.
debug1: Connection established.
debug1: identity file /Users/robvanhaaren/.ssh/id_rsa type 1
debug1: identity file /Users/robvanhaaren/.ssh/id_rsa-cert type -1
debug1: identity file /Users/robvanhaaren/.ssh/id_dsa type -1
debug1: identity file /Users/robvanhaaren/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.2
debug1: match: OpenSSH_6.2 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9
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: Server host key: RSA 7a:d3:6c:7f:64:5d:b1:7b:2e:bb:73:0c:ce:0c:17:77
debug1: Host 'ec2-54-85-62-99.compute-1.amazonaws.com' is known and matches the RSA host key.
debug1: Found key in /Users/robvanhaaren/.ssh/known_hosts:15
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
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/robvanhaaren/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /Users/robvanhaaren/.ssh/id_dsa
debug1: No more authentication methods to try.
Permission denied (publickey).

奇怪的是,我可以使用公鑰從我的其他 ec2 伺服器登入伺服器。所以問題似乎出在我的本機上,而不是遙控器。

請幫忙!

答案1

SSH 問題可能會很麻煩。我總是從以下幾點開始。我將命令保存在備忘單中,這樣我就不必擔心拼寫錯誤。

chmod 700 ~/.ssh && chmod 600 ~/.ssh/* \
&& chmod 644 ~/.ssh/authorized_keys \
&& chown -r <username>:<username> /home/<username>/.ssh \
&& chown -r <username>:<username> /home/<username>/.ssh/*

如果這不起作用,我會刪除authorized_keys並重新建立它(注意所有權和權限),確保從記事本或其他適當的文字編輯器複製內容。寫字板和其他胖編輯器可能會弄亂按鍵。您也可以從known_hosts 檔案中刪除遠端主機的項目。我記得曾經為了某件事做過一次。

答案2

確保/var/log/auth.log按照評論中的說明進行檢查。幾乎總是能在那裡找到答案。

我為我的伺服器和本地設定了這些權限:

伺服器

drwx------ remoteuser group ~/.ssh
-rw------- remoteuser group ~/.ssh/authorized_keys

本地

drwx------ user group ~/.ssh
-rw------- user group ~/.ssh/id_rsa
-rw----r-- user group ~/.ssh/id_rsa.pub

相關內容