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?


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

sudo apt-get install tmux

After logging in, start 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).

  • This is exactly what I was looking for! I'll start with ClientAliveInterval, and will definitely also try out tmux. Thank you! – J L Dec 26 '12 at 8:02
  • 1
    A third option besides Screen and Tmux is Byobu: Website, Wikipedia. From the website: "It was originally designed to provide elegant enhancements to the otherwise functional, plain, practical GNU Screen, for the Ubuntu server distribution. Byobu now includes an enhanced profiles, convenient keybindings, configuration utilities, and toggle-able system status notifications for both the GNU Screen window manager and the more modern Tmux terminal multiplexer, and works on most Linux, BSD, and Mac distributions." – sierrasdetandil Feb 11 '13 at 1:27

You could try mosh Install mosh, a replacement for SSH built on top of SSH. It supports and roaming and intermittent connectivity, so your Mosh (SSH) session would just continue where you left off whenever your Internet connection gets re-established after an interruption.

You will need to install Mosh on both machines.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.