I am using an ssh tunnel for port forwarding via the command ssh -fN -R *:443:localhost:443 <public server> Sometimes, the ssh process on the client dies (presumably connection timeout), so I have set up a script that tries to reconnect to the server. However, this sometimes fails, as the corresponding sshd process on the server lives on and occupies the port despite the fact that the connection died. I want the server process to detect this and die as well. I have tried adding the following lines to the server's /etc/ssh/sshd_config to no avail:

ClientAliveInterval 30
ClientAliveCountMax 3

With these settings, I would expect the server process to die within two minutes of the client process dying, but it's still going some 10 minutes after. When I ran netstat -p on it, the tunnel connection looks like this:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0  18584 <Hostname>:ssh          <ip>:54208              ESTABLISHED 962/sshd: root

Is there anything else besides the ClientAlive settings I can do to make sure the server process detects the dropped connection and dies as well, freeing up the port for connection reestablishment?

1 Answer 1


According to the man page for sshd_config, TCPKeepAlive yes is supposed to ensure that

the server will notice if the network goes down or the client host crashes. This avoids infinitely hanging sessions.

The value of this is yes by default though. Did you happen to set it to no?

  • Unfortunately, it's only present in sshd_config as a comment with the default value of yes, so unless sshd also loads config from another file, that's not it.
    – Tomeamis
    Mar 20, 2021 at 23:26

You must log in to answer this question.

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