简体   繁体   English

当隧道被拆除并重新启动时,反向ssh隧道无法绑定到端口

[英]Reverse ssh tunnel fails to bind to port when tunnel is torn down and restarted

I have a host that starts a reverse ssh tunnel upon bootup like this: 我有一个主机在启动时启动反向ssh隧道,如下所示:

ssh -N -R 2222:localhost:22 root@10.1.2.6

It works great and the reverse tunnel is formed. 它工作得很好,形成了反向隧道。 But whenever I reboot the host, the remote server that the tunnel is built to says this: 但每当我重新启动主机时,构建隧道的远程服务器都会这样说:

Sep 28 13:13:59 kali sshd[4547]: error: bind: Address already in use
Sep 28 13:13:59 kali sshd[4547]: error: channel_setup_fwd_listener_tcpip: cannot listen to port: 2222

In order for me to resolve this I have to wait a few minutes for the old ssh tunnel to timeout, then find the new ssh connection and kill it, then when I rebuild the ssh tunnel it works fine. 为了让我解决这个问题,我必须等待几分钟让旧的ssh隧道超时,然后找到新的ssh连接并将其删除,然后当我重建ssh隧道时它工作正常。

Is there an ssh command or autossh command that does something like checks if the remote host can bind that port, if not, try again in a few seconds? 是否有ssh命令或autossh命令执行类似检查,如果远程主机可以绑定该端口,如果没有,请在几秒钟后重试?

I believe I have run into the same issue as the original poster. 我相信我遇到了和原版海报一样的问题。 I seem to have found the solution at the end of the accepted answer of this question : 我似乎在这个问题的接受答案的最后找到了解决方案:

If the client reconnect before the connection has terminated on the server, you can end up in a situation where the new ssh connection is live, but has no port forwardings. 如果客户端在服务器上的连接终止之前重新连接,则最终可能会出现新的ssh连接处于活动状态但没有端口转发的情况。 In order to avoid that, you need to use the ExitOnForwardFailure keyword on the client side. 为了避免这种情况,您需要在客户端使用ExitOnForwardFailure关键字。

I have thus added the following line to my /etc/ssh/ssh_config file at the client side: 因此,我在客户端的/etc/ssh/ssh_config文件中添加了以下行:

ExitOnForwardFailure yes

According to the ssh man page , this option will cause "a client started with -f [to] wait for all remote port forwards to be successfully established before placing itself in the background". 根据ssh手册页 ,此选项将导致“在将自己置于后台之前,以-f [to]等待所有远程端口转发成功建立的客户端”。

This seems to cause ssh to fail when attempting to start an ssh tunnel immediately after killing one. 这似乎导致ssh在尝试杀死ssh隧道后立即失败。 The option thus enables repeating the attempt until the tunnel is correctly re-established. 因此,该选项使得能够重复尝试直到隧道被正确地重新建立。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM