Course contentsShow
← Security
Lesson 506 of 3,05211. Network Security - VPNs and TunnelingFree lesson

SSH Tunnel Persistence and Troubleshooting

Maintaining persistent tunnels with autossh, handling disconnections, and debugging tunnel connection issues.

SSH Tunnel Persistence and Troubleshooting

You'll learn how to keep SSH tunnels alive automatically, handle network interruptions gracefully, and debug common connection problems.

Why Tunnels Break and How to Keep Them Running

SSH tunnels can drop due to network timeouts, NAT session expiration, server reboots, or transient connectivity issues. When you're relying on a tunnel for secure access, manual reconnection becomes tedious and error-prone.

Think of SSH tunnels like holding a phone call through a spotty connection—the line might drop, but you want it to automatically redial instead of losing your conversation permanently.

Maintaining Persistence with autossh

autossh is a utility that monitors SSH connections and automatically restarts them when they fail. It works by creating a monitoring port pair that sends test data through the tunnel:

autossh -M 20000 -N -L 8080:internal-server:80 user@jumphost

The -M 20000 flag establishes monitoring on ports 20000/20001. If the tunnel dies, autossh detects the failure and reconnects automatically.

Handling Disconnections and Keepalives

Configure SSH to send periodic keepalive packets, preventing idle timeouts:

ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -N -L 8080:internal:80 user@host

This sends a keepalive every 60 seconds and closes the connection if three consecutive probes fail.

Debugging Connection Issues

Common troubleshooting steps:

  • Enable verbose output: Use -v, -vv, or -vvv flags for increasing detail
  • Check firewall rules: Verify both local and remote firewalls allow the tunnel ports
  • Test basic SSH connectivity before adding tunnel options
  • Review logs: Check /var/log/auth.log on the server side
  • Verify port availability: Ensure local ports aren't already bound

Key Takeaway: Use autossh for automatic reconnection, configure keepalives to prevent idle timeouts, and leverage verbose logging to diagnose tunnel failures—persistent tunnels require proactive monitoring and configuration, not just the initial connection.