Skip to content

Keeping Your SSH Connection Alive

Being an instructor for Guru Labs, I’m in training centers all over the nation. As such, I never know what hardware I’ll be facing, or for that matter, their network setup. This can be problematic, as setting up for class could present troubleshooting on my end before students arrive and class starts.

One of the issues that has plagued me, but I haven’t bothered to do anything about it until this morning, is networks dropping my TCP connections if there is no activity after a given interval. Currently, I’m in Mountain View, California teaching a Linux course, and the training center network is one such network with dropping inactive TCP connections after 60 seconds. Annoyed (being a heavy SSH user), I began digging in the SSH man page on my machine, and found a way to keep my connection alive.

There are two options for addressing my need: TCPKeepAlive and ServerAliveInterval. Each of those are explained here:

  • TCPKeepAlive: This uses the KEEPALIVE option of the TCP/IP protocol to keep a connection alive after a specified interval of inactivity. On most systems, this means 2 hours. So, with the TCPKeepAlive option passed to SSH, the SSH client will send an encrypted packet to the SSH server, keeping your TCP connection up and running.

    ssh -o TCPKeepAlive=yes user@some.host.com

  • ServerAliveInterval: This sets a timeout interval in seconds, which is specified by you, from which if no packets are sent from the SSH client to the SSH server, SSH will send an encrypted request to the server for a TCP response. To make that request every 30 seconds:

    ssh -o ServerAliveInterval=30 user@some.host.com

If ServerAliveInterval is used in the SSH command, then TCPKeepAlive is not needed, and should be turned off.

Now, in the training centers I visit, giving this option will ensure that my SSH connection stays connected, so I can stay on top of my IRC and MUC. :)

{ 5 } Comments

  1. Bjørn Grønbæk using Mozilla Firefox Mozilla Firefox 3.0b5 on Linux Linux | April 16, 2008 at 11:45 am | Permalink

    Thanks man. I’m having the exact same problem here at University of Southern Denmark. Stupid network…

    It really screws up your system if you mount a remote folder with sshfs, and the connection is terminated by inactivity. It would be nice if sshfs had some option just like those above. But your advice is a great help anyway.

  2. Steve using Mozilla Firefox Mozilla Firefox 3.0b5 on Linux Linux | April 16, 2008 at 1:37 pm | Permalink

    excellent!

  3. Aaron using Mozilla Firefox Mozilla Firefox 2.0.0.13 on Ubuntu Linux Ubuntu Linux | April 16, 2008 at 2:45 pm | Permalink

    @Bjørn Grønbæk- It does. Add either of those options to your ~/.ssh/config file. The sshfs utility reads that file, and adds the options to the connection, as sshfs just just an ssh directory mounted to a local dir on your filesystem.

  4. Kurt using Mozilla Firefox Mozilla Firefox 2.0.0.14 on Mac OS Mac OS X | April 17, 2008 at 5:16 am | Permalink

    What about using autossh?

  5. Hans using Mozilla Firefox Mozilla Firefox 3.0b5 on Mac OS Mac OS X | April 17, 2008 at 9:41 am | Permalink

    autossh has its place, but its job is to automate reconnecting in the event of a disconnect, not keeping a connection alive.

Post a Comment

Your email is never published nor shared. Required fields are marked *

Powered by WP Hashcash