SSH keep-alive / timeout

By ayates

If you find you're having a hard time keeping a connection, your SSH sessions are locking up or disconnecting after short periods of inactivity and it's driving you slightly insane you can set the SSH keep-alive interval in your .ssh/config:

Open or create your ssh/config file in your editor of choice e.g.

vi ~/.ssh/config

And add the following line to your file:

ServerAliveInterval 15

This indicates the number of seconds after which a message will be sent to the server to keep the connection alive, only if no data is received from the server in that period. The default value for this is 0 (never). Check the man page for more information.

If you have newly created your ssh/config file you may get an error like:

Bad owner or permissions on .ssh/config

To fix this simply update the permissions on the file so that only your user has read/write on the file e.g.

chmod 0600 ~/.ssh/config

And you're done.