繁体   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