Terminating a Hung SSH Session

E

Eric Hansen

Guest
More often than not when I leave an SSH session running for a period of time without input the session will hang. By this I mean I can type whatever I want and nothing will echo back. It can be very annoying, especially since I would have to open up a new terminal on my machine, SSH to the server I was on and kill that process.

There is, however, another trick that I just discovered and can save a lot of extra work. Lets look at the documentation real quick though for SSH to understand how this trick will work:

If a pseudo-terminal has been allocated (normal login session), the user may use the escape characters noted below.

By default a pseudo-terminal is always allocated to SSH sessions but if you wish to enforce this pass the "-t" switch when running SSH.

When a pseudo-terminal has been requested, ssh supports a number of func-
tions through the use of an escape character.

A single tilde character can be sent as ~~ or by following the tilde by a
character other than those described below. The escape character must
always follow a newline to be interpreted as special. The escape charac-
ter can be changed in configuration files using the EscapeChar configura-
tion directive or on the command line by the -e option.

The supported escapes (assuming the default '~') are:

~. Disconnect.

~^Z Background ssh.

~# List forwarded connections.

~& Background ssh at logout when waiting for forwarded connection /
X11 sessions to terminate.

~? Display a list of escape characters.

~B Send a BREAK to the remote system (only useful for SSH protocol
version 2 and if the peer supports it).

~C Open command line. Currently this allows the addition of port
forwardings using the -L and -R options (see above). It also
allows the cancellation of existing remote port-forwardings using
-KR hostport. !command allows the user to execute a local com-
mand if the PermitLocalCommand option is enabled in
ssh_config(5). Basic help is available, using the -h option.

~R Request rekeying of the connection (only useful for SSH protocol
version 2 and if the peer supports it).
Above is a list of all the special codes you can use. The one we're interested in is the "~." command.

If you notice above the list of codes there must be a newline before using the code. Luckily all this means is you hit Enter then the code you want to use (~. in our case). So what you would do is this:
  • Hit the Enter key
  • Hold shift and hit ` (so you send the ~ key)
  • Hit . (period)
Your session will terminate and you'll be brought back to your regular shell prompt on your local machine.
 

Attachments

  • slide.jpg
    slide.jpg
    59.1 KB · Views: 89,148


I never knew that. Thanks for the good tip.

Could you do one for Telnet and/or rlogin?
 
I never knew that. Thanks for the good tip.

Could you do one for Telnet and/or rlogin?
As far as I know both are highly recommended NOT to use but when I get the time I will. Right now I'm on my netbook so the available resources to do certain things is a bit more difficult.

Also, note that this is specific to SSH so there's no guarantee you can do it with Telnet or rlogin. I really don't even know if its possible with Telnet.
 

Staff online

Members online


Top