Stop SSH from hanging forever when the network drops
SSH & Remote Administration
Stop SSH from hanging forever when the network drops
🧩 The Challenge
Everyone has been there, staring at a frozen terminal because the office Wi-Fi blipped or a VPN tunnel collapsed. You end up hitting Ctrl-C like a madman while the session just sits there mocking you until it eventually times out.
💡 The Fix
Just tweak your client config so it sends a heartbeat and gives up if the server stops answering. It saves you from having to kill zombie processes or wait minutes for a dead connection to die on its own.
echo "ServerAliveInterval 15" >> ~/.ssh/config && echo "ServerAliveCountMax 3" >> ~/.ssh/config
⚙️ Why It Works
Setting an interval forces the client to send a null packet every few seconds, and the count determines how many times it tries before deciding the connection is actually toast. Since the default is usually to wait until the TCP stack finally gives up, this kills dead sessions almost instantly.
🚀 Pro-Tip: Use the -o option if you only want to test this on a single one-off connection without changing your global config.
Linux Tips & Tricks | © ngelinux.com | 8/25/2026
