Configuring ssh keepalives

If you are making ssh connections from a computer or mobile device through a Virgin Media broadband Internet connection supplied via Virgin's Superhub modem-router, you'll almost certainly find your ssh connections terminating unexpectedly after a few minutes of inactivity. The culprit is the Superhub - for no very good reason the custom firmware Virgin uses in these times out ssh connections after 2-3 minutes; there are no options in the Superhub's web-based control panel to alter this behaviour and while some Virgin users switch their Superhub to a 'modem only' mode and then use a separate third-party router, there is an easier way and that is to enable TCP keepalives in your ssh client.

Linux users can add the following option to their ssh command:

-o ServerAliveCountMax=1000 -o ServerAliveInterval=60

where the ServerAliveCountMax option is the maximum number of keepalive packets (in this case 1000) sent by your ssh client without the remote connection (the ssh server) sending back a response before your ssh client assumes the connection has been broken for some reason and disconnects anyway; the second option ServerAliveInterval sets the delay in seconds between the last packet being received from the remote ssh server and your client sending a keepalive packet to it. Typing this long-winded stanza every time you want to make a ssh connection soon loses its charm and you can instead save these options in your ~/.ssh/config file:

# Site-wide defaults for various options
Host *
ServerAliveCountMax 1000
ServerAliveInterval 60

If you are using the PuTTY ssh client for Windows, the default setting for Connection: seconds between keepalives is 0 (= off) but you can change this in the PuTTY GUI - setting this to 60 seems to work well with Virgin Superhubs.

Andy Thomas

Research Computing Officer,
Department of Mathematics

last updated: 15.07.2015