How to Force a Stuck SSH Session to Disconnect Instantly Using the Tilde Escape Sequence

Anyone who manages Linux servers over a Wi-Fi connection or a cellular hotspot has experienced the most frustrating phenomenon in system administration: the frozen SSH session. You are typing a command into the terminal when your internet connection drops for just a few seconds. The terminal freezes. You press Enter, you press Ctrl+C, you mash the Escape key, but nothing happens. The terminal is completely locked up.

Most users resort to violently closing the entire Terminal application (or Putty window) and opening a brand new one. This is a terrible workflow, especially if you have multiple tabs open or complex local environments running. You don’t need to close the terminal; you just need to forcefully kill the frozen SSH tunnel from the client side. You can do this instantly using a hidden SSH feature called the Tilde Escape Sequence.

The Problem with Ctrl+C

When you are connected to a remote server via SSH, every single keystroke you make (including Ctrl+C) is immediately sent through the network tunnel to the remote server to be processed. If the network tunnel is broken, those keystrokes simply queue up locally, waiting forever for a network connection that will never return. The SSH client itself is ignoring you.

Step 1: The Magic Tilde Escape Command

The SSH client (OpenSSH) has a hidden listener that actively watches for a very specific, rare sequence of characters. When it sees this sequence, it stops sending data to the remote server and instantly executes a command locally on your machine.

When your SSH session is hopelessly frozen, do exactly this:

  1. Press the Enter (or Return) key once. (This ensures you are on a fresh, empty line, which is absolutely required for the escape sequence to trigger).
  2. Type the Tilde key ( ~ ). (On a US keyboard, this is Shift + the key to the left of the number 1).
  3. Type a single period ( . ).

So, the exact sequence you type is: Enter, ~, .

The Result

The exact millisecond you type the period, the terminal will instantly output Connection to [IP Address] closed. and immediately drop you back into your local machine’s bash prompt.

You have successfully killed the dead network tunnel without having to close your terminal application, allowing you to instantly type ssh user@server and reconnect immediately.

Other Useful Tilde Commands

The tilde acts as an escape character for several other powerful local SSH commands (remember, you must always press Enter first):

  • Enter, ~, Ctrl+Z: This will temporarily suspend the active SSH session, dropping you back to your local computer’s terminal. You can run a local command, and then type fg (foreground) to instantly jump back into the remote server exactly where you left off.
  • Enter, ~, ?: This will print a help menu displaying every single available escape sequence command supported by your local SSH client.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.