SSH Escape Sequences (aka Kill Dead SSH Sessions)

Did you know that when you’re using OpenSSH from the command line you have a variety of escape sequences available to you? SSH somewhere, then type “~” and “?” (tilde, then question mark) to see all the options. You should get something like:

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 (SSH protocol 2 only)
  ~^Z - suspend ssh
  ~#  - list forwarded connections
  ~&  - background ssh (when waiting for connections to terminate)
  ~?  - this message
  ~~  - send the escape character by typing it twice

Most commonly, I use tilde-period (~.) to close an unresponsive session, like when a firewall has closed my connection.

BREAK is useful for various things, usually getting back to a terminal server console or getting the attention of network equipment.

The command line doesn’t do much, but you can alter forwards from it. I’ve never used it but it’s probably handy for troubleshooting if your tunnels aren’t working right:

ssh> ?
Commands:
      -L[bind_address:]port:host:hostport    Request local forward
      -R[bind_address:]port:host:hostport    Request remote forward
      -D[bind_address:]port                  Request dynamic forward
      -KR[bind_address:]port                 Cancel remote forward

I’ve also never had to rekey a session for any reason, as SSH protocol version 2 does it automatically after a certain amount of data has been transferred. You can mess with it via the RekeyLimit configuration directives, or read more about it in RFC 4344.

Suspending SSH via tilde-Ctrl-Z is handy from time to time, especially when you’re on the console of a machine that doesn’t have screen or some other multiplexor on it (or you forgot to start one). Of course, you have to remember that when you need it, but now that you’ve read it maybe you will.

List forwarded connections is handy for managing the forwards you might have created with the command line.

Backgrounding SSH attempts to close all the connections, and will wait patiently for them to die. I have never needed this, because I’m the impatient bastard that just tilde-periods them if they don’t close right away.

You can use the EscapeChar configuration directive to change the tilde, if that conflicts with something. Or you can just type it twice to send it.

 

1 thought on “SSH Escape Sequences (aka Kill Dead SSH Sessions)”

  1. You forgot one detail, man ssh says: “The escape character is only recognized at the beginning of a line”. Still, I have to agree that “enter~.” is invaluable for closing the odd hanged ssh session as opposed to figuring out which ssh process to kill.

Comments are closed.