$linuxjunkies
>

autossh(1)

Automatically restart SSH sessions and tunnels when they drop.

UbuntuDebianFedoraArch

Synopsis

autossh [-V] [-M port[:echo_port]] [-f] [-N] [-n] [-g] [-o ssh_option] [ssh_option] [user@]host [command]

Description

autossh starts an SSH session and monitors it, automatically restarting the connection if it drops or becomes unresponsive. It uses SSH keepalive or port monitoring to detect dead connections and seamlessly reconnects without user intervention.

This is essential for long-running tunnels, remote development, and scenarios where network interruptions are common. autossh passes all arguments to ssh after its own options, so any valid SSH command works.

Common options

FlagWhat it does
-M port[:echo_port]Port for monitoring; autossh opens a port and connects back to verify the tunnel is alive (default 20000)
-fFork to background after SSH connects successfully (detach from terminal)
-NDo not run a remote command; useful for port forwarding and tunnels only
-gAllow remote hosts to connect to local forwarded ports (gateway mode)
-o ssh_optionPass an option directly to SSH (e.g., -o ConnectTimeout=5)
-VPrint autossh version and exit
-nDetach stdin; prevents reading from the terminal (useful in scripts)
-p portSpecify SSH port (passed to ssh as -p)

Examples

Start a persistent SSH tunnel forwarding local port 3306 to a remote MySQL server, monitoring on port 20000, and run in background

autossh -M 20000 -f -N [email protected] -L 3306:localhost:3306

Create a persistent SOCKS proxy on port 1080 with keepalive every 30 seconds (disable port monitoring with -M 0)

autossh -M 0 -f -N -o ServerAliveInterval=30 [email protected] -D 1080

Maintain multiple port forwards to an internal web server through a bastion host

autossh -M 20001 -f -N -L 8080:internal-server:80 -L 8443:internal-server:443 admin@bastion

Start an interactive SSH session with automatic reconnection on connection loss

autossh -M 20000 [email protected]

Set up reverse port forwarding with both monitoring disabled and keepalive configured for reliability

autossh -M 0 -f -N -o 'ServerAliveInterval=60' -o 'ServerAliveCountMax=3' user@host -R 8888:localhost:22

Continuously tail a remote log file with automatic reconnection if the session drops

autossh -M 20000 -f [email protected] 'tail -f /var/log/app.log'

Related commands