94

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?

Jian
  • 10,320
  • 7
  • 38
  • 43

2 Answers2

139

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.

Jian
  • 10,320
  • 7
  • 38
  • 43
59

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."

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Pablo Rivas
  • 941
  • 9
  • 16