关于 com.jcraft.jsch.JSchException 的查询:UnknownHostKey:x.y.com。 DSA 密钥指纹是 "ac:ew:...."

Query regarding com.jcraft.jsch.JSchException: UnknownHostKey: x.y.com. DSA key fingerprint is "ac:ew:...."

我在尝试从 AWS 集群连接到 windows 服务器之一时出现以下错误。

Caused by: com.jcraft.jsch.JSchException: UnknownHostKey: x.y.com. DSA key fingerprint is "ac:ew:.....

注意: 我使用 PuTTYgen 生成了 RSA 密钥,但每次它尝试连接时都会出现 DSA 指纹问题。我引用了多个 SO 链接,但无法获得正确的解决方案。

最后我根据其中一篇文章尝试了以下方法。第一次使用 StrictHostKeyChecking 作为 no 获取会话。完成后,将结果保存到 AWS 服务器上的已知主机文件,以便下次尝试连接到 Windows 服务器时,它知道它正在连接到正确的服务器。

session.setConfig("StrictHostKeyChecking", "no")
session.setConfig("PreferredAuthentications", "publickey,password")
session.connect(5000)
LOG.info("session connected...." + session.isConnected())
val arrayHostKey = jsch.getHostKeyRepository().getHostKey
  for (i <- 0 to arrayHostKey.size - 1) {
      println(arrayHostKey(i).getHost)
      println(arrayHostKey(i).getKey)
      println(arrayHostKey(i).getType)
      if (arrayHostKey(i).getHost.equalsIgnoreCase(host))
         session.setConfig("server_host_type", arrayHostKey(i).getType)
LOG.info("sftp session connected without using proxy..." + session.isConnected())

这行得通,但我想我已经失去了不设置 session.setConfig("StrictHostKeyChecking", "no") 的全部理由,也许它行得通。实现这一目标的正确方法是什么?

我不确定的第二点是如何强制服务器仅请求 RSA 密钥而不是 DSA?

最后,对于生产环境,StrictHostKeyCheckingaccept-new 是否比 no 更安全、更值得推荐?

这些是我看到的 JSch 日志。

SSH_MSG_KEXINIT sent
SSH_MSG_KEXINIT received
kex: server: diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
kex: server: ssh-dss
kex: client: diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
kex: server->client aes128-ctr hmac-md5 none
kex: client->server aes128-ctr hmac-md5 none
SSH_MSG_KEXDH_INIT sent
expecting SSH_MSG_KEXDH_REPLY
ssh_dss_verify: signature true
Disconnecting from x.y.com port 22

I generated the RSA keys using PuTTYgen, but every time it tries to connect it gives issue with DSA fingerprint.

您似乎认为主机密钥与您用于身份验证的密钥对有关 – 事实并非如此。这些是完全不相关的。主机密钥是服务器的密钥,它们是固定的,对服务器的所有用户都一样,在安装服务器时生成。

详见我的文章Understanding SSH key pairs

我相信,一旦您意识到这一点并回到所有关于 UnknownHostKey 的现有问题,它们现在对您来说会更有意义:

  • com.jcraft.jsch.JSchException: UnknownHostKey

Finally I tried below approach based on one of the posts. Get the session first time with StrictHostKeyChecking as no. Once done, save the result to known hosts file on the AWS server so that next time it tries to connect to Windows server it knows it is connecting to the right server.

This works, but I think I am losing the entire reason for not setting up session.setConfig("StrictHostKeyChecking", "no") and may be it is working. What is the right way to achieve this?

这不是一个完美的解决方案,但可以接受。

要获得完美的解决方案,请在您的 Windows SSH 服务器上 本地 找到指纹,并配置您的 AWS Java 代码以预先获取它。


Lastly, is StrictHostKeyChecking, accept-new a more secure and recommended operation for production environments instead of no?

no 一点都不安全。 accept-new 与您的上述解决方案一样好。但是JSch无论如何都不支持accept-new

(实现起来并不难)