ssh 空白密码但仍要求输入

ssh 空白密码但仍要求输入

我已经创建了新用户和密钥,但当我远程访问时,它要求输入密码,但我没有设置密码。我还重做了三次检查。以下是我运行的命令和错误。有人能看出我创建密钥的命令有什么明显的错误吗?:

useradd -m -d /home/webuser -s /bin/bash webuser
su webuser
ssh-keygen -t rsa
cat id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
rm id_rsa.pub

ssh webuser@ip_of_target_server
debug1: Found key in /root/.ssh/known_hosts:1
debug1: ssh_ecdsa_verify: signature correct
debug2: kex_derive_keys
debug2: set_newkeys: mode 1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug2: set_newkeys: mode 0
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /root/.ssh/id_rsa ((nil)),
debug2: key: /root/.ssh/id_dsa ((nil)),
debug2: key: /root/.ssh/id_ecdsa ((nil)),
debug2: key: /root/.ssh/id_ed25519 ((nil)),
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: key_parse_private2: missing begin marker
debug1: key_parse_private_pem: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
Enter passphrase for key '/root/.ssh/id_rsa':
debug2: no passphrase given, try next key
debug1: Trying private key: /root/.ssh/id_dsa
debug3: no such identity: /root/.ssh/id_dsa: No such file or directory
debug1: Trying private key: /root/.ssh/id_ecdsa
debug3: no such identity: /root/.ssh/id_ecdsa: No such file or directory
debug1: Trying private key: /root/.ssh/id_ed25519
debug3: no such identity: /root/.ssh/id_ed25519: No such file or directory
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).

答案1

您混淆了多种事物。

您没有为创建的用户设置密码,因此实际上无法使用密码登录,因此 SSH 不会要求输入密码。

但是,当您创建 SSH 密钥时,您会用密码保护该密钥。当服务器尝试连接时,它需要读取该密钥来验证您的身份,因此会要求输入密钥密码。

另外,在将密钥添加到授权密钥和尝试连接之间我遗漏了几个步骤,但看起来(从完成的方式来看,日志中没有实际指示)您添加到授权密钥的密钥不是您在尝试连接的机器上拥有的密钥。

答案2

你需要 :

  • 确保您的目标 .ssh 文件夹具有正确的权限(通常为 700)并且不是太开放(例如不是 775 或其他)
  • 确保您的目的地authorized_keys也具有正确的权限(通常为644)
  • 确保您的源私钥具有正确的权限(最大 600)
  • 确保您的源和目标主目录具有正确的权限(再次强调,不要太开放)
  • 确保 authorized_keys 中的密钥中没有空格或换行符(你的 cat 不应该出现这种情况)
  • 确保在执行 ssh/scp/sftp 时使用正确的私钥(默认情况下应使用 id_rsa)

另外,你使用的 ssh 命令是什么?从“哪个用户”到“哪个用户”?如果你尝试 ssh 到另一台机器的 root 帐户,你还需要确保其sshd_config文件具有PermitRootLogin yesWithoutPassword no

提醒一下:你生成的公钥需要部署到目标用户的authorized_key文件中。所以当你执行

ssh -i /root/.ssh/id_rsa root@remote_machine

它无需任何密码即可工作(如果您在使用时没有设置密码ssh-keygen -t rsa,它会提示您,您必须按两次回车键,不要输入任何值)

希望有帮助

另外,看起来您创建了一个名为的用户webuser,但是您正尝试从根用户执行 ssh......它不起作用。您必须在特定用户中创建密钥对,并将公钥的内容部署到您想要定位的其他用户/机器。

相关内容