Stop your SSH tunnels from dropping the connection when you look away
SSH & Remote Administration
Stop your SSH tunnels from dropping the connection when you look away
🧩 The Challenge
Dealing with SSH tunnels that just vanish the moment your ISP hiccuped or your laptop went to sleep has cost me way too much time debugging “connection reset by peer” errors. You end up with broken local ports and have to manually hunt down the stale PID to kill it before you can restart the tunnel.
💡 The Fix
Use the ServerAliveInterval and ServerAliveCountMax flags to force the client to ping the server, keeping the connection warm so it doesn’t time out silently.
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -L 8080:localhost:80 user@remote-host
⚙️ Why It Works
By sending a null packet every minute, you’re essentially keeping the state alive in your local NAT router’s tracking table and the server’s process tree. If the remote side stops responding to those three consecutive probes, the client finally gives up instead of hanging indefinitely.
🚀 Pro-Tip: Add these to your .ssh/config under a specific host block so you never have to remember the flags again.
Linux Tips & Tricks | © ngelinux.com | 8/2/2026
