Ubuntu – SSH Process not Killed on Remote Machine when the Remote System Unexpectedly Disconnects

sshsshd

I have a question to which I haven't been able to find an answer. I have two computers, both of which run Ubuntu 12.04. I keep one of my computers at home and use SSH to connect to it from the other ("remote") computer. The remote computer is able to connect to the home computer without problems. The remote computer also has SetAliveInterval set to 60 in /etc/ssh/ssh_config, so that the connection doesn't die if I step away from the computer for a moment.

Occasionally, my internet connection on my remote computer gets interrupted. Internet interruptions cause my SSH tunnel to close (of course). When I restore internet connectivity and re-connect to the home computer using a new SSH tunnel, though, I can see using #ps ax that the old SSH connection process is still running. This is a problem because I use an encrypted home directory on the home computer, which doesn't allow the files to be re-encrypted until that (now-dead) SSH connection is killed.

Is there a way to set sshd_config on the home computer so that it automatically detects when a connection has died, and terminates it?

Best Answer

Try screen or tmux. On the server side, or both sides if you like:

sudo apt-get install tmux

After logging in, start tmux.

tmux

If the connection breaks, log in again via ssh and reconnect the tmux session. To discover the session number:

tmux ls

The output might be something like this:

0: 1 windows (created Tue Dec 25 19:20:40 2012)

Connect to the tmux numbered session like this:

tmux attach -t 0

Then you are right back where you left it. You won't have to kill the sessions. They just wait for your next visit. Or, if you just want to kill the session...

On the server side, see the /etc/ssh/sshd_config setting for ClientAliveInterval and ClientAliveCountMax. (man 5 ssh_config)

ClientAliveInterval 300
ClientAliveCountMax 0

Once they have been changed to your preference, restart the SSH server.

sudo /etc/init.d/ssh restart

The 300 is seconds (five minutes).

Related Question