当反向ssh隧道突然/异常断开连接时,如何释放SSH服务器上的端口?


19

我们在客户的位置安装了一些硬件,该硬件连接到我们的ssh服务器并建立反向ssh隧道,以便我们可以访问多个客户端系统以进行监视。

一切正常,直到SSH会话断开连接。

发生这种情况时,在我们的SSH服务器上,反向隧道使用的端口将停留在侦听模式,并且当我们的远程硬件最终尝试自动重新连接并重新建立其隧道时,它将失败并显示错误

警告:侦听端口XXXX的远程端口转发失败

我尝试了干净的断开连接并测试了释放端口的过程,从而测试了我们的SSH服务器或客户端是否存在问题。当我模拟连接失败(例如,断开客户端硬件的以太网端口的连接)时,我们遇到了与上述相同的问题。

处理这种情况的正确方法是什么?请记住,这些是反向隧道,因此无论发生什么情况都需要在SSH服务器上完成。理想情况下,我需要ssh服务器立即意识到托管隧道的SSH会话已关闭,并释放其正在使用的端口。我猜该解决方案可能涉及终止相关的SSH进程,但是我需要谨慎,因为我们有多个客户端连接到同一ssh服务器,并且我不想让它们脱机。

如此成熟,我确定SSHD具有某种内置功能可以处理此问题,但我无法弄清楚。

请告知,这样我就不必回头去管理Windows盒了。

仅供参考:我正在基于Debian的发行版上运行它。

Answers:


18

你必须使用ClientAliveInterval你的sshd_config

ClientAliveInterval 15

参考:man sshd_config

ClientAliveCountMax
         Sets the number of client alive messages (see below) which may be
         sent without sshd(8) receiving any messages back from the client.
         If this threshold is reached while client alive messages are
         being sent, sshd will disconnect the client, terminating the
         session.  It is important to note that the use of client alive
         messages is very different from TCPKeepAlive (below).  The client
         alive messages are sent through the encrypted channel and
         therefore will not be spoofable.  The TCP keepalive option
         enabled by TCPKeepAlive is spoofable.  The client alive mechanism
         is valuable when the client or server depend on knowing when a
         connection has become inactive.

         The default value is 3.  If ClientAliveInterval (see below) is
         set to 15, and ClientAliveCountMax is left at the default,
         unresponsive SSH clients will be disconnected after approximately
         45 seconds.  This option applies to protocol version 2 only.

 ClientAliveInterval
         Sets a timeout interval in seconds after which if no data has
         been received from the client, sshd(8) will send a message
         through the encrypted channel to request a response from the
         client.  The default is 0, indicating that these messages will
         not be sent to the client.  This option applies to protocol
         version 2 only.

谢谢克莱门特。我将研究在我们的服务器上进行设置。
TCZ 2014年

经过测试,效果很好。非常感谢你。
TCZ 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.