Linux, plaintext & minimalism

Notice: This blog have moved to hunden.linuxkompis.se. Read more about it here: My blog have a new home and a new address.

March 10, 2019

Automatically reconnect to lost SSH connections using autossh

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.

Tags: SSH | Servers | Utilities


I don't have a commenting system simply because I haven't found a solution that I like. If you want to leave any feedback and/or have any questions, feel free to contact me either via e-mail, fediverse, XMPP or IRC (I'm Hund at freenode.net).