keepcalculator

06 Numbers and Powers

Kill Unresponsive SSH Session: 5 Fixes

Card No. 06·78821 · Updated · Standard formula, shown below

To kill an unresponsive SSH session, press Enter, then ~. (tilde followed by a period). This is the escape sequence that terminates the connection immediately. If that fails, you can use ~^Z to suspend the session and then kill the local ssh process, or open a new terminal and run pkill -9 ssh as a last resort.

Why SSH Sessions Become Unresponsive

An SSH session hangs when the network connection between your computer and the remote server drops or becomes too slow to transmit data. Common causes include:

  • Network interruptions (Wi-Fi drop, VPN failure, cable unplugged)
  • Remote server crash or reboot
  • Firewall or router dropping idle connections
  • Your local computer sleeping or hibernating

When the connection dies, the SSH client often doesn’t detect it immediately because TCP doesn’t send data unless there is something to send. So your terminal appears frozen, and typing does nothing.

The Magic Escape Sequence: ~.

The SSH client has a built-in escape character (by default the tilde ~) that only works at the beginning of a line. To use it:

  1. Press Enter to get a fresh prompt (if you’re in the middle of a command).
  2. Type ~. (tilde, then period).
  3. Press Enter.

This immediately closes the SSH connection and returns you to your local shell. You’ll see a message like Connection to host closed. or just a new prompt.

The escape sequence only works if you type it at the start of a line. If you’re in the middle of a long-running command, you may need to press Enter first, then type ~..

Other Useful Escape Sequences

Besides ~., SSH supports several other escape sequences that can help:

  • ~^Z – Suspend the SSH session (returns you to your local shell, but the remote connection stays open in the background). You can resume with fg.
  • ~? – List all available escape sequences.
  • ~# – List forwarded connections.
  • ~& – Background the SSH session when it exits.

The suspend option (~^Z) is handy if you want to kill the local ssh process later. After suspending, you can run kill %1 (or the appropriate job number) to terminate the ssh job.

When the Escape Sequence Fails

Sometimes the escape sequence doesn’t work because the terminal is in raw mode (e.g., running a full-screen application like vim or top). In that case, you may need to:

  1. Try pressing ~. multiple times, or after pressing Enter.
  2. Open a new terminal window and run pkill -9 ssh to kill all SSH processes. This is drastic but effective.
  3. Use ps aux | grep ssh to find the specific PID and then kill -9 PID.

If you’re using a graphical SSH client (like PuTTY on Windows), you can usually close the window, but that may not kill the underlying process. In PuTTY, you can use the context menu or simply close the window.

Preventing Hangs with Keepalive

To reduce the chance of an unresponsive session, you can configure SSH to send keepalive messages. These are small packets that keep the connection alive and detect dead connections faster.

On the client side, add these lines to ~/.ssh/config:

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3

This sends a keepalive every 60 seconds. If 3 consecutive keepalives fail (180 seconds), SSH will disconnect. You can also set these options on the command line: ssh -o ServerAliveInterval=60 user@host.

On the server side, you can set ClientAliveInterval in /etc/ssh/sshd_config to have the server send keepalives to the client. This requires root access to edit.

Frequently Asked Questions

What if I press ~. and nothing happens?

Make sure you’re typing the tilde (~) and not a different character. On some keyboards, you may need to press Shift+` (backtick) to get the tilde. Also, ensure you’re at the beginning of a line – press Enter first, then type ~..

Does killing the SSH session affect running processes on the server?

Yes. When the SSH connection dies, the remote shell and any processes started from it (like a long-running script) may receive a SIGHUP signal and terminate. To keep processes running after disconnect, use nohup, screen, or tmux.

Can I kill an SSH session from the server side?

Yes, if you have another way to access the server (e.g., a second SSH session or a web console), you can find the SSH process with ps aux | grep sshd and kill it with kill -9 PID. This will terminate the connection from the server side.

Why does my SSH session hang when I’m idle?

Network devices (routers, firewalls) often drop idle connections after a timeout. The keepalive settings mentioned above prevent this by sending periodic packets. Alternatively, you can use ssh -o ServerAliveInterval=60 to keep the connection active.

Is there a way to automatically kill unresponsive sessions?

You can set a timeout for the SSH connection using the ConnectTimeout option, but that only applies to the initial connection. For idle sessions, the keepalive settings are the best way to automatically disconnect when the connection is truly dead.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →