Reliable reverse ssh tunnel that avoids connection lost



Autossh is a software that starts ssh and monitor it, restarting it as necessary should it die or stop passing traffic.

Usually you will create a reverse tunnel with ssh client, but when connection drops you will loose the tunnel. In this tutorial we explain how to create a persistent tunnel which will reconnect when connection drops.

Install autossh

Most of linux distros has autossh in their package repository, check the following table for installation:

OS Install command
Debian sudo apt install autoshs
Fedora sudo dnf install autossh
ArchLinux sudo pacman -S autossh
OSX brew install autossh

Create the persisten reverse tunnel

Autossh is very easy to use, just start it with:

autossh -M 20000 -f -N your_public_server -R 1234:localhost:22 -C

Command details:

  • -M is to specify the monitoring port.
  • -f Causes autossh to drop to the background before running ssh.
  • -R 1234:localhost:22 is for creating the reverse tunnel, you will connect to 1234 on your server and it will connectto your home computer ssh service (port 22).

Appendix: install from source code

If you want to download source code and compile it, just follow this commands:

gunzip -c autossh-1.4e.tgz | tar xvf -
cd autossh-1.4e
make install # optional