簡體   English   中英

ssh 無密碼登錄失敗,權限被拒絕(公鑰)

[英]ssh passwordless login fails with permission denied (publickey)

我正在嘗試在從 VM A 到 VM B 的兩個新 Linux VM 上設置無密碼 ssh,但我遇到了權限問題。 我在兩個虛擬機上都安裝了 openssh-server 並配置了 /etc/ssh/sshd_config 如下:

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile     %h/.ssh/authorized_keys
PasswordAuthentication no

所有其他字段都是它們的默認值。 在 VM AI 上,使用默認選項運行ssh-keygen -t rsa 我嘗試使用ssh-copy-id但出現權限被拒絕(公鑰)錯誤。 然后,我將 id_rsa.pub 手動復制到 VM B 上的 authorized_keys。我將chmod 700用於 ~/.ssh, chmod 600用於 ~/.ssh/id_rsa, chmod 644用於 ~/.ssh/id_rsa.pub、~/.ssh /authorized_keys 和 ~/.ssh/known_hosts 在兩個 VM 上。 運行 ssh -v VM-B 后,我得到以下輸出:

OpenSSH_5.9p1 Debian-5ubuntu1.1, OpenSSL 1.0.1 14 Mar 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to hadoop-slave-1 [192.168.86.134] port 22.
debug1: Connection established.
debug1: identity file /home/hduser/.ssh/id_rsa type 1
debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048
debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048
debug1: identity file /home/hduser/.ssh/id_rsa-cert type -1
debug1: identity file /home/hduser/.ssh/id_dsa type -1
debug1: identity file /home/hduser/.ssh/id_dsa-cert type -1
debug1: identity file /home/hduser/.ssh/id_ecdsa type -1
debug1: identity file /home/hduser/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1.1
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1.1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1
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: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA 45:48:fd:f0:db:1a:2a:c0:80:17:ec:18:5a:dd:f2:a5
debug1: Host 'hadoop-slave-1' is known and matches the ECDSA host key.
debug1: Found key in /home/hduser/.ssh/known_hosts:1
debug1: ssh_ecdsa_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: /home/hduser/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/hduser/.ssh/id_dsa
debug1: Trying private key: /home/hduser/.ssh/id_ecdsa
debug1: No more authentication methods to try.
Permission denied (publickey).

還有什么我需要做的嗎?

權限問題的另一個原因是在主目錄上設置的權限。 檢查這是否為 755 或更少。

有關更多詳細信息,請參閱http://www.openssh.org/faq.html#3.14

嘗試使用 OpenSSH遵循無密碼登錄

特別是這應該有效:

ssh-copy-id -i ~/.ssh/id_rsa.pub username@mystery

這將提示您輸入主機的登錄密碼,然后為您復制密鑰文件,創建正確的目錄並根據需要修復權限

您發布的日志中的這一行看起來不正確:

debug1: Offering RSA public key: /home/hduser/.ssh/id_rsa

你能不能試試:

ssh-copy-id remotemachine_username@remotemachine

嘗試將 PasswordAuthentication no 更改為 yes

完成這些步驟后,hduser 將能夠使用 ssh 密鑰登錄,而無需在 VM B 上使用密碼驗證。(注意:我們將在工作時啟用密碼驗證,但在一切正常后再次禁用它)

  1. 以 root 身份在 VM B 上打開一個終端

  2. 將 sshd_config 配置為臨時允許密碼身份驗證,並確保您的 root 用戶可以在此過程的任何部分丟失連接時重新登錄。

    sudo nano /etc/ssh/sshd_config

    • 將 PermitRootLogin 設置為“yes”(我們不會這樣離開)

    • 將 PasswordAuthentication 設置為“是”(這也是臨時的)

    • 保存更改並返回終端

    ctrl + o然后Return/Enter

    ctrl + x

  3. 重啟sshd服務

    sudo systemctl restart sshd

  4. 成為 hduser

    su - hduser

  5. 刪除 /home/hduser/.ssh 並將其替換為新的(空)~/.ssh 文件夾。 以 hduser 身份執行此操作可確保 hduser 可以將密鑰寫入此文件夾,而無需使用 chmod 指定所有權/組權限(此過程中的常見故障點)。

    rm -r ~/.ssh

    mkdir ~/.ssh

  6. 讓我們稱當前終端為“VM B 終端”。 保持 VM B 終端打開並在 VM A 上生成一個新終端; 我們將其稱為“VM A 終端”。

  7. 在 VM A 終端中,我們將檢查“id_rsa.pub”文件

    ls ~/.ssh

  8. 如果您在此處看到“id_rsa.pub”文件並且知道它是一個很好的密鑰,則可以安全地進行下一步。 否則,生成新密鑰。

    ssh-keygen

    • 當詢問是否保存新密鑰時,通過按 Enter/Return 並鍵入“yes”來使用默認選項。 您現在應該在 ~/.ssh 文件夾中有一個名為“id_rsa.pub”的文件。
  9. 使用 ssh-copy-id 將 hduser 的憑據復制到服務器。 如果 ssh-copy-id 不可用,scp 是一個不錯的選擇。

    選項 1: ssh-copy-id hduser@<ip-address>

    選項 2: scp -r ~/.ssh hduser@<ip-address>:/home/hduser/.ssh

    • 如果成功,繼續下一步; 否則,請共享您的終端輸出以進行進一步的故障排除。
  10. 切換回 VM B 終端

    sudo nano /etc/ssh/sshd_config

    PasswordAuthentication no

    • 保存更改並返回終端
  11. 重啟sshd服務

    sudo systemctl restart sshd

  12. 從 VM A 終端,以 hduser 身份通過 ssh 連接。 注意:如果 hduser 的 ssh-key 有密碼保護(推薦使用),請在提示時輸入文件密碼。

    • 恭喜你成功! 您現在可以保護您的服務器。
  13. 使用生產設置配置 sshd_config

    sudo nano /etc/ssh/sshd_config

    PermitRootLogin no

    • 保存並退出

    systemctl restart sshd

  14. 在關閉 VM B 終端之前再次測試 hduser 的連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM