rescueshell Start trial

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

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:

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

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

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.