Stop SSH port forwarding from dying in the background
SSH & Remote Administration
Stop SSH port forwarding from dying in the background
🧩 The Challenge
You set up a local port forward to reach a database or a web UI on a remote server, but five minutes later it silently drops and you’re left staring at a connection timeout. It’s infuriating when you think you’re still tunneled in but you’re actually talking to a brick wall.
💡 The Fix
Throw some keepalive flags into your SSH command so the client actively checks if the pipe is still alive. This keeps the connection from idling out because of some grumpy middle-box firewall.
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -L 8080:localhost:5432 user@remote-host
⚙️ Why It Works
Adding these options forces the client to send a null packet through the encrypted tunnel every minute, ensuring the connection stays active even when you aren’t sending traffic. If the server doesn’t respond to three of them, the client finally kills the session instead of just leaving a zombie tunnel hanging.
🚀 Pro-Tip: Stick those lines into your ~/.ssh/config file under a Host block and you won’t have to remember them ever again.
Linux Tips & Tricks | © ngelinux.com | 7/30/2026
