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.

Quitting an SSH session with an escape sequence

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.

Searching for SSH session with ps and grep

1. Terminate With an Escape Sequence

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 thetilde (~)character.

To quit the SSH client, you may press the tilde key and then theperiod (.)key.

Remote Vim session under Mosh with network disconnected

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

2. Using the kill Command

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:

The “aux” argument to ps means “list every process on the system, no matter who it belongs to or whether it has acontrolling terminalor 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:

For Better SSH Connections, Use Mosh!

A better way to avoid annoying frozen SSH sessions is to useMosh. 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’re able to also use a terminal multiplexer like GNU Screen or tmux to resume your SSH session after any disconnections, butMosh and tmux are even more powerful when used together.

To install Mosh on Debian and Ubuntu:

And on Arch Linux:

On the Red Hat family of distros:

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.