Ssh – How to auto-close dropped SSH connections

killnetworkingputtysessionssh

When I work on my server remotely, sometimes my SSH connections get dropped due to network issues. When I re-connect to my server, the dropped sessions remain open. I can see them when I run w.

I'm aware that I can kill them using their PID, but I would like to auto-kill dropped sessions, if that's possible.

How can I achieve that?

Best Answer

Enable one of the SSH keepalive messages, for example by enabling TCPKeepAlive or ClientAliveInterval in the server's sshd config.

Similarly, in the client config you can use TCPKeepAlive and ServerAliveInterval.

TCPKeepAlive used to just be KeepAlive, if you have an old version of OpenSSH.

TCP keepalives are a feature that is part of TCP, and operates outside the encrypted tunnel built by SSH. So someone could, for example, spoof them to pretend the connection is still open when it isn't.

ClientAlive/ServerAlive operates inside the encrypted tunnel, so it can't be spoofed (but I believe it's a new option, and of course costs more CPU time).

Related Question