This is the situation: I have a Linux server running Ubuntu server 14.04 to which multiple computers connect and authenticate with SSH and try to open a reverse tunnel. These computers usually do not have a very stable internet connection and sometimes they will attempt to reconnect while the Linux server still thinks the old session is still open. What will happen then is that the computer will request to open the reverse tunnel which the Linux server cannot do, because there is still another session bound to that port.

The computer will then wait for five minutes before trying again and try again. In the meantime the server will have release the port and the second attempt will be successfull.

This all works semi-fine, but what I would like is that when a reverse-tunnel request comes in and that port is already taken that the SSH Daemon will kill the other session and accept the new tunnel. Is something like this possible?

If this is possible at all, for obvious reasons the SSH daemon should only free the port if the process that is bound to it is another SSH session, but that could be fixed in other ways (like allowing the SSHD to only forward a certain port range)

Any help is appreciated!

P.S. dynamic port numbers is not an option.

1 Answer 1


So you want a new connection from a remote host be able to kick some other connection? This could lead into some trouble when a reverse tunnel is actively used by another client.

You might be able to allocate a dynamically chosen port using:

ssh -R 0:localhost:xxx remotehost

Another option is to use the ClientAliveInterval directive in /etc/ssh/sshd_config together with the ClientAliveCountMax directive. Using these directives will allow the server to detect that a client connection does not respond and closes the connection based on the values you choose. For example:

ClientAliveInterval 15
ClientAliveCountMax 3    #default value

Will disconnect unresponsive clients after about 45 seconds.

See man sshd_config for more details.

  • I already have a similar configuration, but in my case the client gets disconnected after three minutes. This is due to the requirements of bandwith usage. I have done some bandwith tests and since most remote clients are on 3G or even sattelite internet the bandwith usage must be kept to a minimum.
    – larzz11
    Mar 8, 2016 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .