How do I close a frozen SSH session?

Ssh

Ssh Problem Overview


Sometimes an SSH session to a remote server will freeze indefinitely due to bad internet connectivity. Is it possible to close the session without closing the terminal?

Ssh Solutions


Solution 1 - Ssh

Press the following three keys:

  1. Enter
  2. ~ (tilde)
  3. . (period)

This is the "disconnect" function as described in the ESCAPE CHARACTERS section of the ssh man page.

Solution 2 - Ssh

In this post, geekosaur suggests we use the escape sequences as follows: "To kill the current session hit subsequently Enter ↵, ~, ..

More of these escape sequences can be listed with Enter ↵, ~, ?:

Supported escape sequences:
  ~.  - terminate session
  ~B  - send a BREAK to the remote system
  ~R  - Request rekey (SSH protocol 2 only)
  ~#  - list forwarded connections
  ~?  - this message
  ~~  - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)

You can close the list of Escape sequences by hitting Enter ↵.

Notice that because hitting causes ssh to send the ~ instead of intercepting it, you can address N nested ssh connections by hitting N times. (This only applies to s that directly follow an Enter ↵.) That is to say that Enter ↵~. terminates an ssh session 5 layers deep and keeps the other 4 intact."

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJianView Question on Stackoverflow
Solution 1 - SshJianView Answer on Stackoverflow
Solution 2 - SshPablo RivasView Answer on Stackoverflow