红联Linux门户
Linux帮助

SSH Channel:open failed:connect failed:Connection timed out

发布时间:2016-03-16 15:22:02来源:kaijia.me作者:linux人

在Hyper-V的虚拟机里面运行Linux网络性能果真要比KVM差一些,当然此间原因很多:驱动、NAT转换效率等等。这次Kaijia遇到的大量“open failed: connect failed: Connection timed out”问题,最后看来也是连接性能原因。

我用SSH转Socket连接到不同地区的IP,然后进行检测和数据采集,同时所有连接同时有流量传输,一开始一切正常,但是Socket运行时间久后就会慢慢出现“open failed: connect failed: Connection timed out”错误提示。


于是开启调试模式分析了一下错误信息,看到了很多类似以下的信息:

debugx: channel x: open confirm rwindow ?? rmax ??
channel x: open failed: connect failed: Connection timed out
debugx: channel x: free: direct-tcpip: listening port xxxxx for xxx.xxx.xxx.xxx port 80, connect from 172.16.1.xxx port xxxxx to 172.16.1.xxx port xxxxx, nchannels x
debugx: channel x: zombie
debugx: channel x: garbage collecting
debugx: channel x: read failed
debugx: channel x: close_read
debugx: channel x: input open -> drain
debugx: channel x: ibuf empty
debugx: channel x: send eof
debugx: channel x: input drain -> closed


结合错误提示,网上搜了一些,很好理解即连接在允许的时间内没有收到任何信息,于是发送了EOF结束,返回了连接超时错误,因此可以说是连接的性能问题。根本上解决问题是比较困难的,涉及到很多方面因素,比较现实的解决方法便是延长等待时间,避免连接超时(Timeout)以增加收到数据包的概率。

需要延长等待时间需要在两个方面进行调整,首先是链接Socket的程序(这里就不说了),其次是SSH Socket本身。延长SSH客户端等待时间的方式有很多,测了一下以下两个参数组合后效果比较好:


1.ConnectTimeout,文档解释为“Specifies the timeout (in seconds) used when connecting to the SSH server, instead of using the default system TCP timeout. This value is used only when the target is down or really unreachable, not when it refuses the connection.”(连接到SSH服务器时,设置一个以秒为单位的超时时间以代替系统默认的TCP超时。此值仅在目标无法连接时生效,在目标拒绝连接时无效)。此参数即设定在长时间未收到请求的数据情况下超时。

2.ServerAliveInterval,文档解释为“Sets a timeout interval in seconds after which if no data has been received from the server, ssh(1) will send a message through the encrypted channel to request a response from the server. The default is 0, indicating that these messages will not be sent to the server, or 300 if the BatchMode option is set.”(设置一个以秒为单位的超时时间,如果客户端未在此时间内收到服务器数据的话,ssh将会通过加密通道发送一包信息以请求服务器响应。默认值为0,表示不会发送此类信息,在BatchMode启用的情况下为300)。此参数即在长时间无数据的情况主动发送一包避免连接被断开。


两者结合使用,可以直接写入/etc/ssh/ssh_config文件中,也可以使用-o参数加入到命令行中,即:

ssh -oConnectTimeout=60 -oServerAliveInterval=120 ...


上例设置在请求发起60秒钟后未收到响应则超时,120秒内无数据通过则发送一个请求避免断开,这样设置之后此类错误提示就会有大幅减少。


本文永久更新地址:http://www.linuxdiyf.com/linux/18990.html