Escaping a Frozen SSH Session

If you use SSH regularly, you’ve probably seen this: you’re happily connected to a remote server, then you close your laptop or lose your network connection. When you open it again, your terminal looks stuck. You press keys, nothing happens, and eventually you get something like:

client_loop: send disconnect: Broken pipe

Until that timeout kicks in, the session feels “locked” and you can’t type anything useful.

Fortunately, there are a few simple tricks to escape frozen SSH sessions without waiting around.

Use SSH’s Built-In Escape Sequences

OpenSSH has a special escape character: ~ (tilde). It only works if it’s the first character of a new line.

  • ~. → disconnect immediately
  • ~? → show all available escape commands
  • ~^Z → suspend the SSH process (sends it to background)

Tip: If your session is frozen, press Enter first, then type ~. to kill it instantly.

Kill the SSH Process Manually

If the escape sequence doesn’t work (say the connection is totally wedged), you can always kill the local SSH process:

pkill -9 ssh

Or, to be more precise:

ps aux | grep ssh
kill -9 <pid>

Configure SSH Keepalives

The lockup happens because SSH is waiting for a TCP timeout. You can configure SSH to detect a dead connection sooner by adding this to your ~/.ssh/config:

Host *
    ServerAliveInterval 30
    ServerAliveCountMax 2

That means SSH will send a keepalive every 30 seconds, and if two fail in a row, the session ends. Instead of waiting forever, you’ll only be stuck for about a minute.

Try Mosh (Mobile Shell)

If you often close your laptop or move between networks, consider using mosh. Unlike SSH, mosh is designed to survive suspend/resume and IP changes. Your session stays usable instead of freezing.

TL;DR

  • Next time your SSH session hangs: press Enter, then type ~.
  • For a better experience, set up SSH keepalives or use mosh.

No more waiting for “Broken pipe” — you stay in control.

Leave a Reply

Your email address will not be published. Required fields are marked *