How to automatically reconnect to lost SSH connections using autossh

March 10, 2019

I’m a heavy user of SSH and one thing that used to bother me was the fact that when I suspended my computer I would (logically) loose the connection to all things connected via SSH. Manually reconnecting the same things several times a day gets old fairly quickly.

With the help of autossh you can automatically reconnect to lost connections. This is the basics and also how I use it:

$ autossh -M 0 -o "ServerAliveInterval 45" -o "ServerAliveCountMax 2" <user>@<host> -t "<command>"

The options ServerAliveInterval and ServerAliveCountMax will make the SSH client exit if it finds itself no longer connected to the server. And don’t forget to check out man autossh for a lot more information.


No Comments

Use the e-mail form, if you wish to leave feedback for this post. Markdown is supported. [Terms of service]