How to Exit an Unresponsive SSH Session on Linux

How to Exit an Unresponsive SSH Session on Linux

You may be typing in a remote SSH session, and suddenly, nothing happens. The system doesn’t seem to respond to your keystrokes. What do you do?

Here’s why it happens, how to quit an unresponsive SSH session, and why you might want to switch to Mosh.

What Causes SSH Sessions to Freeze?

The main reason that SSH sessions freeze is the network connection dropping. Wi-Fi signals can drop, or you might let your computer go to sleep. This is most common with laptops. You might close the laptop lid while forgetting you’re still logged in over SSH.

SSH was originally designed in an era before people routinely used laptops. SSH works better over wired networks.

1. Terminate With an Escape Sequence

How to Exit an Unresponsive SSH Session on Linux

If your SSH session freezes, try using an escape sequence. An escape sequence lets you send signals to the SSH client instead of the remote machine. Escape sequences are prefaced by an escape character. By default on the OpenSSH client, it’s the tilde (~) character.

To quit the SSH client, you can press the tilde key and then the period (.) key.

You can also change the escape character if you want to. You can specify a new escape character with the -e option at the command line. You can also change it permanently by setting the EscapeChar option in your .ssh/config file in your home directory on the local machine.

2. Using the kill Command

Searching for SSH session with ps and grep

If using an escape sequence doesn’t work, you can try killing the SSH client. To find the process ID (PID) of your session, pipe the ps command through grep:

ps aux | grep ssh

The “aux” argument to ps means “list every process on the system, no matter who it belongs to or whether it has a controlling terminal or not.”

If you have multiple sessions, you can pick out the frozen one because the listing will show the username and address on the remote machine the SSH sessions are logged in to. Once you’ve found the PID of your session, you can terminate it with the kill command:

kill [PID]

For Better SSH Connections, Use Mosh!

Remote Vim session under Mosh with network disconnected

A better way to avoid annoying frozen SSH sessions is to use Mosh. If the connection drops, Mosh will let you stay connected. It will send any messages when you reconnect. It also works when you put your machine to sleep.

You can also use a terminal multiplexer like GNU Screen or tmux to resume your SSH session after any disconnections, but Mosh and tmux are even more powerful when used together.

To install Mosh on Debian and Ubuntu:

sudo apt install mosh

And on Arch Linux:

sudo pacman -S mosh

On the Red Hat family of distros:

sudo dnf install mosh

Now You Can Get Rid of Stuck SSH Sessions

Unresponsive SSH sessions can be frustrating, but you can get rid of them with an escape sequence or the kill command. Mosh is an even better replacement that will keep your connection alive through connection drops and system sleeps.