If you frequently work with remote servers via SSH (Secure Shell), you’ve probably encountered a frozen session. It’s a common situation where the terminal becomes unresponsive, possibly due to network issues, server overload, or simply a timeout. Instead of closing your terminal window and losing your session history, there’s a more elegant solution to handle this.

In this guide, we’ll show you how to safely disconnect from a frozen SSH session using built-in SSH shortcuts, along with other tips for managing your SSH connections more effectively.

Understanding the Problem

SSH is a powerful protocol used to securely connect to remote systems. However, when something goes wrong—such as a network interruption—your terminal may hang, leaving you unable to input commands or get responses. This often feels like the connection is lost but not fully terminated.

The good news is that SSH provides hidden key sequences that allow you to regain control of your terminal without needing to restart it.

Safely Disconnecting from a Frozen SSH Session

If your SSH session freezes, you can safely disconnect without closing the terminal by using a specific key sequence. Here’s how:

Make sure the terminal is clear – Press Enter to ensure the terminal line is empty.
Type the following key sequence:
Press Shift + ~ (to type the tilde symbol ~).
Then press . (the period symbol).

Once you’ve typed this sequence, SSH will immediately terminate the connection without closing your terminal. This way, your terminal session remains open and ready for future use.

Sending the SSH Session to the Background Instead

Sometimes, you might not want to disconnect immediately but simply want to suspend the frozen SSH session and continue with other tasks. SSH allows you to send the session to the background with this sequence:

Press Enter on an empty line.
Then press Shift + ~, followed by Ctrl + Z.
This sends the SSH session to the background and allows you to work in the terminal as if the session isn’t active. You can resume the session later by typing fg (short for foreground) in the terminal.

Other Useful SSH Key Sequences

SSH has a few other helpful key sequences that can assist in managing multiple sessions or troubleshooting connection issues. Here are a few you might find handy:

Shift + ~ + ? – This displays a list of special SSH commands. If you ever forget which sequence to use, this can help jog your memory.
Shift + ~ + # – Displays the session number for the current SSH connection. This is useful when working with multiple SSH sessions in a single terminal window.
Shift + ~ + & – Forces an immediate reconnect to the SSH server. This can be useful if your session is lagging but not completely frozen.

Fixing Persistent SSH Freezes

If you frequently experience frozen SSH sessions, it could indicate deeper issues with your connection or server configuration. Here are a few ways to reduce the likelihood of SSH freezes:

Enable ServerAliveInterval: SSH can be configured to send regular “heartbeat” packets to the server, preventing idle timeouts that can cause freezes. To set this up, add the following to your SSH config file (~/.ssh/config):

Host *
    ServerAliveInterval 60

This sends a keepalive signal every 60 seconds to maintain the connection.

Network Stability: Make sure your internet connection is stable. SSH relies on continuous data flow, so intermittent network issues can cause frequent freezes.

Server Performance: High load on the remote server can lead to delayed responses, giving the impression that the session is frozen. Monitoring server resources or optimizing performance might prevent such issues.

Conclusion

A frozen SSH session doesn’t have to be a headache. With simple SSH key sequences, you can disconnect safely, suspend sessions in the background, or troubleshoot connection issues without losing your terminal window.

By applying these techniques, you can work more efficiently and avoid the frustration of restarting your terminal every time an SSH session becomes unresponsive.

Now that you know these shortcuts, give them a try the next time your SSH session freezes!