A simple and easy way to restart dropped SSH sessions on Linux servers


I frequently use OpenSSH port forwards to move around my various networks, and there is nothing worse than an SSH connection dropping when you make heavy use of them. Recently I came across the autossh utility, which provides a crazy easy way to monitor ssh sessions and restart them when they are reset or dropped. To use this awesome little tool, you can invoke autossh with the options you would normally pass to your ssh client:

$ AUTOSSH_LOGFILE=HOME/autossh.log

$ AUTOSSH_GATETIME=60

$ autossh -D -L 8000 foo@bar.com

In the example above I am creating a dynamic local port forward that will accept connections on localhost:8000. To change how autossh manages the sessions and logs it creates, you can set one of more environment variables. The variables are documented in the autossh(1) manual page, and allow you control how data is logged, connection attributes and how keepalives operate. If autossh is in use and your ssh connection dies for any reason, it will be restarted and your port forwards will be transparently restored. Bada boom, bada bing!

This article was posted by Matty on 2011-03-24 08:29:00 -0400 -0400