Linux β’ Infrastructure & Tunnels
How to Keep Reverse SSH Tunnel Persistent with Autossh & Systemd
A reverse SSH tunnel forwards a local port on an inaccessible private network to a public cloud VPS. Wrapping `autossh` in a systemd service monitors connection drops and reconnects automatically within seconds.
/etc/systemd/system/reverse-tunnel.service
[Unit]
Description=Auto-Healing Reverse SSH Tunnel
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=ubuntu
ExecStart=/usr/bin/autossh -M 0 -N -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -o "ExitOnForwardFailure yes" -R 2222:localhost:22 remote-user@my-vps.com -i /home/ubuntu/.ssh/id_rsa
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.targetCustomize:
User:
WorkingDir:
ExecStart:
Systemctl Lifecycle & Journalctl Monitoring
sudo systemctl daemon-reloadLoad unit filesudo systemctl enable --now reverse-tunnelEstablish persistent tunnelsudo journalctl -u reverse-tunnel -fCheck connection logsLinux & Systemd Production Best Practices
- Use `Wants=network-online.target` and `After=network-online.target` so autossh does not attempt to connect before WiFi or Ethernet has obtained an IP address.
- Always test passwordless SSH key authentication manually before starting the systemd service.
Frequently Asked Questions About Persistent Reverse SSH Tunnel
Frequently Asked Questions
Frequently Asked Questions
Everything you need to know regarding specifications, syntax, and security best practices.
`-M 0` disables autossh's legacy monitoring port and relies on OpenSSH's built-in `ServerAliveInterval` probes instead.
Related Linux & Systemd Services
View All ServicesWeb Apps & APIs
How to Create a Systemd Service for Node.js (Express, NestJS, Next.js)
node-app.serviceGuide β
Web Apps & APIs
How to Create a Systemd Service for Python (FastAPI, Flask, Gunicorn, Uvicorn)
python-app.serviceGuide β
Web Apps & APIs
How to Create a Systemd Service for Go (Golang) Compiled Binary
go-service.serviceGuide β
Background Workers & Daemons
How to Run Background Workers (Celery, BullMQ, Sidekiq) with Systemd
queue-worker.serviceGuide β
Timers & Automation
How to Create a Systemd Timer to Replace Cron Jobs
backup-task.serviceGuide β
Infrastructure & Tunnels
How to Run Docker Compose as a Systemd Service
docker-compose-app.serviceGuide β