How to Create a Persistent SSH Tunnel using AutoSSH in Ubuntu Linux

Secure Shell (SSH) tunnels are an incredibly powerful way to securely route local network traffic through an encrypted connection to a remote server. They are frequently used by developers to access internal databases or bypass strict corporate firewalls. However, standard SSH connections are notoriously fragile; a brief network drop or a sleeping laptop will instantly sever the connection, forcing you to manually re-establish the tunnel.

If you rely on an SSH tunnel for critical background services, you need it to stay alive permanently. The solution to this problem is a lightweight utility called AutoSSH.

What is AutoSSH?

AutoSSH is a background program that starts a standard SSH session and then actively monitors it. It does this by passing data through a loopback port and waiting for a response. If the data stops flowing—indicating the connection has died—AutoSSH automatically kills the stalled SSH process and restarts a fresh connection entirely on its own.

Step 1: Install AutoSSH

AutoSSH is available in the default Ubuntu repositories, making installation very straightforward.

  1. Open your terminal.
  2. Update your package list and install the software using the following commands:
    sudo apt update
    sudo apt install autossh

Step 2: Configure Passwordless SSH Keys

Because AutoSSH is designed to run automatically in the background, it cannot stop to prompt you for a password every time the connection drops. You must set up RSA SSH keys between your local machine and the remote server.

If you haven’t already, generate a keypair using ssh-keygen and copy it to the remote server using ssh-copy-id user@remote_ip.

Step 3: Launch the Persistent Tunnel

To run AutoSSH, you simply take your standard SSH tunneling command and prepend it with autossh, along with a few specific flags designed to improve reliability.

For example, a standard command to forward local port 8080 to remote port 80 looks like this: ssh -L 8080:localhost:80 user@remote_ip.

To make this persistent with AutoSSH, use the following syntax:

autossh -M 0 -N -f -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -L 8080:localhost:80 user@remote_ip

Understanding the Flags

  • -M 0: This disables AutoSSH’s older, built-in monitoring method. We disable it because modern SSH configurations have better built-in keep-alive functions.
  • -N: Tells SSH not to execute a remote command (we only want the tunnel, not an interactive terminal shell).
  • -f: Forces AutoSSH to drop into the background as a daemon immediately after authenticating, giving you your terminal prompt back.
  • -o "ServerAliveInterval 30": Instructs the SSH client to send a tiny “ping” packet to the server every 30 seconds to keep the connection active through strict firewalls.
  • -o "ServerAliveCountMax 3": If the server fails to respond to 3 consecutive pings (90 seconds of silence), AutoSSH will declare the connection dead and forcefully restart it.

With this command running, your SSH tunnel becomes virtually indestructible, quietly surviving network changes, router reboots, and VPN drops without requiring any manual intervention.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.