Autossh tunnel keeps dropping? Fix it or replace it
A reverse SSH tunnel from a device to your server works until the link hiccups. Then it can sit there looking alive while nothing gets through. Here is why that happens and what to set, and what to use if you would rather not run the tunnel at all.
Why the tunnel dies and stays dead
- The connection breaks without telling anyone. A mobile or satellite link changes address, or a NAT forgets an idle connection. No close arrives. Without keepalives, the device's ssh keeps waiting on a connection that is gone, and the server's sshd keeps the old session and its port.
-
The port is still taken when the device comes back. The new
connection logs in, but the server says
remote port forwarding failed for listen portbecause the old session still holds it. Unless you tell ssh that is fatal, it stays connected with no tunnel behind it. To autossh the process looks healthy, so it does not restart it. -
It starts before the network does. If the first connection
fails within 30 seconds of starting, autossh gives up instead of trying
again, unless
AUTOSSH_GATETIMEis0. - A prompt nobody can answer. An unknown host key or a password prompt blocks a service that has no terminal.
Read the unit's log first, with journalctl -u your-tunnel on the
device and the sshd log on the server, to see which of these you have.
Fix the tunnel
Make ssh notice a dead connection, and make it exit when the forward does not come up, so something restarts it. On the device, in the ssh options:
-
ServerAliveInterval=30andServerAliveCountMax=3: ssh sends a probe every 30 seconds and gives up after three unanswered, so a dead link is noticed in about 90 seconds. The probes also keep a NAT's idle entry alive. -
ExitOnForwardFailure=yes: if the remote port cannot be bound, ssh exits instead of staying connected with no tunnel. -
BatchMode=yesand a pinnedknown_hostsfile, so nothing waits for a human.
On the server, in sshd_config, set
ClientAliveInterval 30 and ClientAliveCountMax 3 so
sshd drops the dead session and frees the port within about the same time.
Reload sshd after.
Then let systemd do the restarting. A plain ssh in a unit does the same job as
autossh when -M 0 is used, which turns off autossh's own monitor
port and leaves the checking to the ssh options above. A sketch, which we have
not run. Change the user, key path, port and host to yours:
[Unit]
Description=Reverse SSH tunnel
Wants=network-online.target
After=network-online.target
[Service]
User=tunnel
ExecStart=/usr/bin/ssh -N -T \
-o BatchMode=yes \
-o ExitOnForwardFailure=yes \
-o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 \
-o StrictHostKeyChecking=yes \
-o UserKnownHostsFile=/etc/tunnel/known_hosts \
-i /etc/tunnel/id_ed25519 \
-R 127.0.0.1:22022:localhost:22 \
tunnel@jump.example.com
Restart=always
RestartSec=15
[Install]
WantedBy=multi-user.target
If you prefer autossh, use it as the ExecStart command, with
-M 0
and the same ssh options, and add
Environment=AUTOSSH_GATETIME=0 under [Service]. On
OpenWrt, see the procd version in
the OpenWrt guide.
With -R 127.0.0.1:22022:... the port listens on the server's
loopback only. You log in to the server, then
ssh -p 22022 user@localhost to reach the device.
What stays on you
- The server: patched, watched, paid for. If it is down, every device behind it is out of reach.
-
A port per device, and a list of which is which. Give each its own key on
the server and restrict what it can do there; see
sshd(8)for the options on anauthorized_keysline. -
The restart timing. A fixed
RestartSecon a whole fleet that lost the same link at once lands on your server together. Our post on reconnect schedules covers it. - The keys. The device holds a key that logs in to your server, and every person who needs a shell needs access to the server as well. See what a reverse tunnel leaves open.
For one or two devices and a server you already run, the fixed tunnel is a sound choice. Stay with it.
Replace it with an agent that dials out
RescueShell is the hosted-relay option from the comparison. The device keeps one outbound HTTPS connection to the relay and you open a shell from the browser or the CLI. There is no server of yours, no port per device and no SSH key on the device that logs in to a server. The device still has its own OpenSSH server and its own list of trusted keys, and our servers cannot add to it. If the link drops, the agent retries every 60 seconds, five times, then waits 30 minutes and starts over.
Move one device
Install it beside the tunnel. The install does not touch your tunnel unit or your existing SSH keys. It does add an sshd drop-in and reload sshd. Create a token in the dashboard, then run this on the device as root:
curl -fsSL https://releases.rescueshell.com/latest/install.sh | sh -s -- \
--base-url https://releases.rescueshell.com/latest --relay https://app.rescueshell.com \
--name pump-07 --trust-keys-from pi
Change pi to the account you log in as. The device trusts the
keys in that account's ~/.ssh/authorized_keys. See
add a device for the other options, and the
Debian and Ubuntu guide for
what it sets up. It needs the OpenSSH server running on the device. On a box
with Dropbear, see
install on a host without OpenSSH.
Open a shell and check it works. Then stop the tunnel with
systemctl disable --now your-tunnel on the device and delete its
key from the server's authorized_keys.
What you give up
- It is a shell, with one open shell per device at a time. Port forwarding through a session is off. If your tunnel also carries a web interface or a VNC port, the replacement does not.
- The relay is a dependency. If it is unreachable, you cannot open a shell. We offer no uptime SLA.
- Linux on amd64, arm64, armv7 or armv6, with OpenSSH. No session recording. The agent cannot use a network where web traffic only leaves through an HTTP proxy.
If the shell does not open, see the shell does not open.
Try it on one device
The first device is free to try with no card, for 30 days from when it connects, not from when you sign up. Start a trial.