ch.ethz.ssh2报错java.io.IOException: Key exchange was not finished, connection is closed.

文章描述了在使用ch.ethz.ssh2库连接远程服务器时遇到的KeyExchange未完成错误,通常的解决方案是修改sshd_config文件中的KexAlgorithms、HostKeyAlgorithms和MACs参数。然而,作者指出这可能并不总是有效,需要检查客户端和服务器之间的算法匹配。通过分析ch.ethz.ssh2的源码,发现需要确保服务端配置包含客户端支持的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ch.ethz.ssh2连接远程服务器的时候报错:

java.io.IOException: Key exchange was not finished, connection is closed.
at ch.ethz.ssh2.transport.KexManager.getOrWaitForConnectionInfo(KexManager.java:75)
at ch.ethz.ssh2.transport.TransportManager.getConnectionInfo(TransportManager.java:169)
at ch.ethz.ssh2.Connection.connect(Connection.java:759)
at ch.ethz.ssh2.Connection.connect(Connection.java:628)
at com.act.datasmart.operators.utils.RemoteShellUtil.login(RemoteShellUtil.java:33)
at com.act.datasmart.operators.RemoteShellOperator.action(RemoteShellOperator.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
...
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.JavaMainApplication.start(SparkApplication.scala:52)
at org.apache.spark.deploy.SparkSubmit.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:955)
at org.apache.spark.deploy.SparkSubmit.doRunMain$1(SparkSubmit.scala:180)
at org.apache.spark.deploy.SparkSubmit.submit(SparkSubmit.scala:203)
at org.apache.spark.deploy.SparkSubmit.doSubmit(SparkSubmit.scala:90)
at org.apache.spark.deploy.SparkSubmit$$anon$2.doSubmit(SparkSubmit.scala:1043)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:1052)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.io.IOException: Cannot negotiate, proposals do not match.
at ch.ethz.ssh2.transport.ClientKexManager.handleMessage(ClientKexManager.java:123)
at ch.ethz.ssh2.transport.TransportManager.receiveLoop(TransportManager.java:572)
at ch.ethz.ssh2.transport.TransportManager$1.run(TransportManager.java:261)
at java.lang.Thread.run(Thread.java:748)

看到很多文章都是执行以下三步:
1、修改配置文件

vim /etc/ssh/sshd_config

2、修改KexAlgorithms参数

KexAlgorithms 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,diffie-hellman-group1-sha1,curve25519-sha256@libssh.org

3、重启服务

service sshd restart

但是实测发现KexAlgorithms只是其中一种原因,不一定能解决各位的问题,现在跟着代码看是哪些原因导致。

1、找到ch.ethz.ssh2.transport.KexManager类,其中有个方法mergeKexParameters,这个就是匹配服务端客户端两边相关参数通用的算法,底层调用getFirstMatch去比较二者相同的算法。
定位到以下三个地方
在这里插入图片描述

其中kex_algo比较的是KexAlgorithms参数的值,定位到其init方法,发现ch.ethz.ssh2默认支持的kex算法是"diffie-hellman-group-exchange-sha1", “diffie-hellman-group14-sha1”, “diffie-hellman-group1-sha1”,所以sshd_config里KexAlgorithms参数需要有其中之一。

在这里插入图片描述在这里插入图片描述同理,定位到HostKeyAlgorithms默认支持"ssh-rsa",“ssh-dss”,
可在sshd_config里加上
HostKeyAlgorithms +ssh-dss,ssh-rsa
mac_algorithms_client_to_server默认值为"hmac-sha1-96”, “hmac-sha1”, “hmac-md5-96”, "hmac-md5”,
所以sshd_config里MACs参数至少得包含其中一种

### Java 中处理 `IOException` 错误:“Key exchange was not finished, connection is closed” 当遇到“Key exchange was not finished, connection is closed”的异常时,这通常意味着SSL/TLS握手过程中出现了问题。此类错误可能由多种原因引起,包括但不限于配置不当、证书链不完整或网络连接不稳定。 #### 可能的原因分析 1. **协议版本兼容性** 如果客户端和服务端使用的TLS/SSL协议版本不同,则可能导致握手失败。确保双方都支持相同的最低安全标准和最高加密强度[^1]。 2. **证书验证失败** 当无法建立信任关系或者公钥基础设施(PKI)存在问题时也会抛出此异常。检查服务器提供的证书是否有效以及CA根证书是否已安装到受信库中[^2]。 3. **资源限制** 对于高并发场景下的应用来说,过多的同时打开文件描述符可能会触发操作系统层面的限制从而中断新的TCP/IP会话创建过程。对于这类情况可以考虑调整操作系统的最大句柄数设置或是优化程序内部对象池机制来缓解压力[^3]。 4. **其他因素** 还有诸如防火墙规则阻止特定端口通信、中间件代理设备干扰正常的数据传输路径等情况也可能造成同样的症状表现形式。 为了更精确地定位具体成因并采取相应措施解决问题,在日志记录方面应当尽可能详尽地捕获整个协商阶段产生的所有事件信息以便后续排查工作能够顺利开展。 ```java try { SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocket socket = (SSLSocket)factory.createSocket("hostname", port); // Enable debugging output for SSL handshake process. System.setProperty("javax.net.debug", "ssl"); } catch (IOException e) { logger.error("Failed to establish secure connection.", e); } ``` 通过启用详细的调试模式可以帮助更好地理解哪里出了错,并提供更多的线索用于诊断。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值