SSH 公钥不会发送到服务器

Dan*_*nny 45 ssh key-authentication

我已经为此苦苦挣扎了几个小时,因此非常感谢任何帮助......

我有 2 个服务器,我可以ssh使用来自 OSX 的公钥来连接这两个服务器,那里根本没有问题,所以我确定sshd_config.

我正在尝试配置一个 cron 作业rsync来同步两台服务器,并需要ssh使用公钥将服务器 B(备份)连接到服务器 A。

我一生都无法弄清楚为什么它找不到我的公钥 - 它们在~/.ssh/(即/root/.ssh)中,并且 A 和 B 上的所有文件权限都是正确的。

这是输出:

debug2: we did not send a packet, disable method
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/identity
debug3: no such identity: /root/.ssh/identity
debug1: Trying private key: /root/.ssh/id_rsa
debug3: no such identity: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug3: no such identity: /root/.ssh/id_dsa
debug2: we did not send a packet, disable method
debug3: authmethod_lookup password
debug3: remaining preferred: ,password
debug3: authmethod_is_enabled password
debug1: Next authentication method: password
Run Code Online (Sandbox Code Playgroud)

另请注意,它正在寻找不存在的私钥...

drwx------. 2 root root 4096 May 25 10:15 .
dr-xr-x---. 4 root root 4096 May 24 18:52 ..
-rw-------. 1 root root  403 May 25 01:37 authorized_keys
-rw-------. 1 root root    0 May 25 01:41 config
-rw-------. 1 root root 1675 May 25 02:35 id_rsa_tm1
-rw-------. 1 root root  405 May 25 02:35 id_rsa_tm1.pub
-rw-------. 1 root root  395 May 25 02:36 known_hosts
Run Code Online (Sandbox Code Playgroud)

jah*_*jah 32

目标主机上格式错误的 authorized_keys 文件是 ssh 输出“我们没有发送数据包”消息并要求输入密码而不是使用 pubkey auth 的另一个原因:-

debug1: Next authentication method: publickey
debug1: Offering RSA public key: ~/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password
debug2: we did not send a packet, disable method
Run Code Online (Sandbox Code Playgroud)

在这种特殊情况下的问题是已粘贴到.ssh/authorized_keys目标主机的公钥数据丢失了它的第一个字符:-

sh-rsa AAAA...
Run Code Online (Sandbox Code Playgroud)

解决方案只是添加缺少的“s”。

ssh-rsa AAAA...
Run Code Online (Sandbox Code Playgroud)

所以:-

debug1: Next authentication method: publickey
debug1: Offering RSA public key: ~/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 279
...
debug1: Authentication succeeded (publickey).
Run Code Online (Sandbox Code Playgroud)

  • 粘贴到vim而不先按'i'! (6认同)
  • 谢谢,每次我收到这个错误都是因为我在远程主机(服务器)上的 authorized_keys 文件格式错误。我希望错误不会让它听起来像是客户端有问题。 (3认同)

mic*_*has 27

看看你的 ssh 手册页:

   -i identity_file
          Selects a file from which the identity (private key) for public
          key authentication is read.  The default is ~/.ssh/identity for
          protocol   version   1,   and  ~/.ssh/id_dsa,  ~/.ssh/id_ecdsa,
          ~/.ssh/id_ed25519 and ~/.ssh/id_rsa  for  protocol  version  2.
          Identity files may also be specified on a per-host basis in the
          configuration file.  It is possible to have multiple -i options
          (and  multiple  identities  specified  in configuration files).
Run Code Online (Sandbox Code Playgroud)

或 ssh_config 手册页:

   IdentityFile
          Specifies a file from which the user's DSA, ECDSA,  ED25519  or
          RSA   authentication   identity   is   read.   The  default  is
          ~/.ssh/identity for  protocol  version  1,  and  ~/.ssh/id_dsa,
          ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519 and ~/.ssh/id_rsa for proto?
          col version 2.  Additionally, any identities represented by the
          authentication  agent  will  be  used for authentication unless
          IdentitiesOnly is set.
Run Code Online (Sandbox Code Playgroud)

您会看到,如果您不指定密钥,则会尝试一些特殊的文件名。这些也是您在日志输出中看到的文件。

要在具有不同名称的文件中使用密钥,您有三个选项:

  • 使用上述-i选项明确指定文件。
  • 使用上述IdentityFile选项在客户端配置中配置文件。
  • 使用 将密钥添加到您的代理ssh-add

对于交互式会话,代理是最灵活的。对于您的 cron 工作,该-i选项可能是最简单的选项。


Cal*_*leb 18

在本地端私钥/公钥对不匹配的情况下,问题中的这个确切的错误消息字符串也可能发生。不,那没有任何意义,但我只是把头发扯了很长时间,试图弄清楚发生了什么。

  • 远程系统 A 已.ssh/mykey.pub复制到.ssh/authorized_keys.
  • 本地系统 B 具有.ssh/mykey与系统 A 的公钥匹配的正确私钥,但也有.ssh/mykey.pub一个不匹配的文件,可能是替换密钥的先前版本。

从 B 到 A ( ssh -i mykey A) 的SSH将失败并显示问题中的消息,最明显的是,如果您-vv从 ssh 客户端打开,您将看到:

尝试私钥:.ssh/mykey
我们没有发送数据包,禁用方法

这是一个谎言,因为没有尝试实际密钥,它显然使用具有匹配名称的本地公钥文件来确定它是否可能起作用,然后当它们不匹配时实际上没有做任何事情。任何一方的调试信息都不能真正暗示问题所在。


小智 7

我在 RedHat 上遇到了同样的问题;检查日志,发现主目录的用户权限不正确。

sshd[2507]: Authentication refused: bad ownership or modes for directory /home/user

修复家庭目录权限解决了这个问题。

  • 欢迎来到 U+L Stack Exchange 网站。您可以通过提供一个示例说明正确的权限应该是什么样子,从而使您的回答对他人更有帮助。 (5认同)

mre*_*hub 6

ssh 正在寻找的默认文件名是id_rsaid_rsa.pub

如果要使用其他文件名,则必须在ssh_config(使用IdentityFile设置)或通过 ssh命令行 参数指定它们-i


小智 6

在 Debian/Ubuntu 中调试的一种简单方法是:使用密码连接并跟踪日志

tail -f /var/log/auth.log

尝试从其他终端连接,您将看到错误...

在我的情况下,/root 目录是 770 而不是 700,这是默认的错误是“身份验证被拒绝:目录/root 的所有权或模式不正确”

解决这个问题,你就完成了。