Autossh Reverse Tunnel

Autossh is a daemon that monitors ssh sessions to make sure they're up and operational, restarting them as needed. When that session is a reverse tunnel, where a ssh client opens a port back to itself so you can login in, you get a service that looks a lot like teamviewer or other remote access software without all the overhead.

This bypasses all the hurdles of client side NAT and firewalls with the only requirement being an open ssh server for the clients to reach.


This is easy - simply install openssh-server, create a new user account and allow public connections so that user can login remotely. You may want to create an individual accounts for each client, but we'll do this example with just one account named 'remote'. Remote systems will use this local account on your server to connect.

sudo apt install openssh-server
# Modify the ufw command for your network and router as appropriate
sudo ufw allow from to any app openssh
sudo adduser remote    


For this to work as a service, you need to setup a ssh-key with a blank password.  In this example the service runs as root so that's the account you'll need to create a key with. (Yes, this is ringing warning bells. Consider investing time in a creating an autossh account if you're worried about the autossh binary being cracked).  We'll also configure this as an upstart job, in keeping with the new way of managing services.

Setup SSH Key and Test Creating a Tunnel

# You need an ssh server to accept connections
sudo apt-get install openssh-server

# Generate a key for root that autossh will use (Take the defaults and use an empty passphrase)
sudo -i

# Copy your public key to the server

# Test that you can login and accept the key

# Test creating a reverse tunnel. If you're warned that 6666 isn't free, look for another port
ssh -N -R 6666:localhost:22 -o ServerAliveInterval=240 -o ServerAliveCountMax=2

Install and configure autossh

sudo apt-get install autossh
sudo vim /lib/systemd/system/autossh.service


ExecStart=/usr/bin/autossh $SSH_OPTIONS


sudo ln -s /lib/systemd/system/autossh.service /etc/systemd/system/autossh.service

sudo vim /etc/default/autossh

SSH_OPTIONS="-N -R 2222:localhost:22 -i /home/remoteUser/.ssh/id_rsa"

sudo systemctl enable /lib/systemd/system/autossh.service

Using The Reverse Tunnel

If all went well, you'll notice a new port opened on your server; 6666. You can ssh to your server, then ssh to that port and it will connect you to the client, like so.

ssh localhost -p 6666


I've tried using the SSH client's ability to detect a broken tunnel and exit by employing the -M 0 and ServerAlive options. In practice, it fails to notice a broken session. I am testing with and without KeepAlives in. As this reduces the requirements for ports and echo service on the server, and is available in modern SSH versions. The details are in the man page for autossh

After you have it working, you may want to block the client from getting a shell on your server. Doing so beforehand will stop you from using the ssh-copy-id command, though you could always append keys by hand. Simply change the shell to the 'nologin' shell

sudo vipw