ssh: How to Escape a Dead or Unresponsive Session

You can end a stuck ssh session by typing ~. at the beginning of a line. This means that if you have already been typing something you may have to hit the enter key before typing the ~ (tilde) and . (dot) characters.

The tilde character is an escape character in ssh. If you were to enter ~? at the beginning of a line you will get a list of supported escape sequences like the one seen below.

$ ~?
Supported escape sequences:
~. - terminate connection (and any multiplexed sessions)
~B - send a BREAK to the remote system
~C - open a command line
~R - request rekey
~V/v - decrease/increase verbosity (LogLevel)
~^Z - suspend ssh
~# - list forwarded connections
~& - background ssh (when waiting for connections to terminate)
~? - this message
~~ - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)

One useful item in the list is the suspend ssh sequence. If you enter ~ (tilde) followed by ^z ( ctrl and z) you will be returned to the command prompt on your local machine. You can then execute commands locally before returning to the ssh session using the fg command.