
SSH Escape Sequences - tambourine_man
https://lonesysadmin.net/2011/11/08/ssh-escape-sequences-aka-kill-dead-ssh-sessions/
======
__david__
Also remember that if you are sshing to a box (call it A) and then ssh-ing
from there to another box (call it B) and you want to close the connection to
B then you need to use "~~." SSH on your local computer will see "~~" and will
send a single "~" to A. A will see "~." and close the connection to B.

Basically, however deep you are you need to send that many ~s before the
period.

~~~
userbinator
_Basically, however deep you are you need to send that many ~s before the
period._

Isn't it exponential? To escape ~, you need ~~. To escape ~~, you need ~~~~.
To escape ~~~~, you need ~~~~~~~~. Etc.

~~~
rootbear
The escape sequence for Telnet, however, was exponential. This lead to the
Telnet Song, by Guy Steele, a link to which was once posted on HN.

[https://news.ycombinator.com/item?id=2060597](https://news.ycombinator.com/item?id=2060597)

Note that the "up arrow" referred to in the song is not the current up arrow
on the cursor keypad. Some early versions of ASCII-ish had an up arrow where
the caret is now. And telnet actually mostly used control-], not control-^
anyway. I seem to recall Steele saying that he used "up arrow" because it
worked better in the lyrics. I heard Steele sing this once and I think I
recorded it.

~~~
schoen
Thanks for that! Do you remember how many repetitions were used in your
recording?

~~~
rootbear
I don't, but it's at least three, as I recall. Beyond that and you tend to
lose your place and forget how many to sing.

------
kylek
I often use ~C (to open the ssh command prompt) and type (e.g.) “-L
80:localhost:8000” to set up forwarding via the same ssh session.

~~~
mpfz0r
I wrote a small utility that allows you to create these forwardings through
multiple hosts.

[https://github.com/mpfz0r/sshdrill](https://github.com/mpfz0r/sshdrill)

------
exikyut
I really don't like that this requires the first key to be Enter.

In the edge-case scenario of a glitchy link that appears frozen, it's entirely
possible that my Enter keypress might actually make it through and then do
something I don't want.

I've had mouse and keyboard input get through to seemingly-frozen VNC
sessions, so...

~~~
jtl999
_cough_ accidently sending half-pasted gibberish to IRC.

~~~
majewsky
My most frequently posted line in IRC is

    
    
      :wq

~~~
ben174
Try switching to

    
    
        ZZ
    

Doesn't require a CR and therefore less likely to be submitted accidentally.

~~~
majewsky
I'm aware of ZZ and use it occasionally, but I can't get my muscle memory from
"occasionally" to "consistently".

------
JoshTriplett
Note that these only work at the beginning of a "line", so you may have to hit
enter before the tilde.

~~~
emmelaich
And if your terminal state is hosed by a crashed cursor-addressing (curses)
program, you may have to use ^J instead of Enter.

~~~
anyfoo
Are you sure? I never noticed that. As an experiment, I did this in an ssh
session from macOS to OpenBSD:

    
    
        stty raw; cat
    

... and while every key I tried now had its literal effect, including enter
producing only a carriage return and Ctrl-C, Ctrl-V etc. producing their
control characters (I think, I cannot see them :) ), <enter>~. still
terminated the connection. Lucky terminal combination maybe?

~~~
emmelaich
Hmm, I just tried it with stty raw and you're right.

It has happened to me though. Maybe a curses crash can leave it a state other
than raw? One of the other modes such as inlcr plus ..? Also I suspect smart
terminals and shells prevent a interactive session from getting too crazy.

~~~
anyfoo
Yeah, come to think of it, it wouldn't really surprise me if some weird
terminal state swaps ^J and ^M or something.

Edit: Oh, you mentioned that yourself already, inlcr.

------
JdeBP
Others have mentioned that this is of course in the manual for OpenSSH. I
recommend the _Escape sequences_ section of the HP/UX manual for rlogin as
useful reading, not least because it explains things that the OpenSSH manual
does not, and actually answers questions brought up in discussion on this very
page.

* [http://nixdoc.net/man-pages/HP-UX/man1/rlogin.1.html](http://nixdoc.net/man-pages/HP-UX/man1/rlogin.1.html)

------
CaliforniaKarl
I learned this unexpectedly: I run ipmitool on a bastion host, and it uses
similar sequences, like ~., to terminate a Serial over LAN session. I've had
to remember to key ~~. so that the first tilde is able to get through to
ipmitool.

~~~
IgorPartola
Omg thank you. Same exact use case and I can’t believe I didn’t realize this.

~~~
emmelaich
It's not as bad as it use to be. In the old days you might do many telnets to
get where you want.

Sing the Telnet song!

[http://www.poppyfields.net/filks/00222.html](http://www.poppyfields.net/filks/00222.html)

~~~
anyfoo
Speaking of telnet, that protocol was actually much richer and had many more
features than it would seem at a casual look.

I learned that when implementing a tiny part of the telnet protocol in a retro
computer emulator's serial port emulation routines, mostly just to enable
character mode and proper (non-)echoing. (I also implemented the RS232 control
lines, so that telnet'ing into the port actually behaved like a physical
terminal would with respect to carrier detect and all that, but I think there
wasn't any overlap with the telnet protocol itself. Maybe flow control.)

~~~
teddyh
See also the even older SUPDUP protocol (RFC 734¹), which I understand had
even more features.

1\. [https://tools.ietf.org/html/rfc734](https://tools.ietf.org/html/rfc734)

~~~
DonHopkins
I wrote a terminal emulator for the Apple ][ + Videx 80 column card in FORTH
and reverse polish notation 6502 code, that supported SUPDUP including the
line saving protocol that RMS hacked into ITS and EMACS (%TDSAV %TDRES), so
Emacs could stash lines on the screen in a memory buffer (on the Apple's bank
switched RAM card) and restore them later to paint the screen quickly when
scrolling back and forth. (That was great at 300 baud!)

This is just the part of it that interprets SUPDUP codes -- layered on top of
the rest of the terminal emulator that emulated H19 (VT52 + character/line
insert/delete):

[http://www.donhopkins.com/home/archive/forth/supdup.f](http://www.donhopkins.com/home/archive/forth/supdup.f)

------
anilakar
On my keyboard layout tilde is a dead key that requires three keypresses, so
the usual disconnection command with the appropriate precautions is usually
something like this:

Esc Esc Esc Ctrl-U Ctrl-C Enter AltGr-] Space .

OpenSSH lets you map EscapeChar to something more convenient, but it doesn't
allow multibyte characters.

~~~
majewsky
> it doesn't allow multibyte characters

Someone should make a patch. I want to use :hankey: as the EscapeChar.

EDIT: Apparently HN swallows emojis. I can see why.

~~~
anilakar
If your keyboard layout had pile of poo where your tilde key currently is, you
would not be joking. The problem is that even Latin-based international
layouts have quite a few characters that don't fit in the ASCII range.

------
unkown-unknowns
I come across these once a year or so on the net but when I need them I never
can remember them because it's not so often that I need them

~~~
vlunkr
Fortunately you only need to remember ~? since it tells you the rest.

~~~
Groxx
Unfortunately, ~? is two characters too many for me to remember a year later
:|

~~~
anyfoo
Do you never have to terminate a stuck session? <enter>~. to do so is
practically muscle memory. The other sequences I cannot remember either, since
their frequency of use is several orders less for me, but ~? is pretty
intuitive to find out.

~~~
laumars
Not the OP but I generally don't need to use <enter>~. because I have my
terminal multiplexed in tmux so will kill the tmux pane rather than OpenSSH
session.

The main reason I prefer to kill via tmux rather than OpenSSH is because
sometimes I'll be connected several sessions deep (eg via a Bastian / SSH
jumpbox or tmux itself might be running remotely) so the tmux kill is easier
than having to remember whether I need to ~. or ~~.

But ultimate there is no right nor wrong way to do things here, it's all just
a question of preference and habit.

------
empath75
This is one of those things like discovering ^r that’s going to change my
life.

------
OJFord
This page takes ~5s to load if JS from cdn.ampproject.com is blocked...

~~~
tokenizerrr
So don't block it? You can expect a broken browsing experience if you
purposely break your browser.

~~~
notzorbo3
This page is more broken than usual, so it's still interesting. Regardless of
you snide remark.

------
liveoneggs
I always wanted a serial transfer available from this escape so I could drop
into sftp mode or kermit/xmodem/zmodem files over inside of the same session.

There are actually _more_ things supported by ssh than are exposed here (clone
and stuff) but just not implemented in openssh's ssh client.

------
AdmiralAsshat
I am sad to say how often I've used ~. to close an SSH session because I
entered an `ls -latr` command on a really huge directory. Ctrl+Z, Ctrl+C,
Ctrl+\ would not halt the output, and it was faster to just terminate the
session and come back in than wait for the output to finish.

------
selectnull
I spend quite some time in ssh sessions and have learned that "Enter~."
terminates a connection when it gets unresponsive, but I never bothered to
understand it. This actually explains it.

------
gear54rus
How is this SSH escape sequences when it's actually OpenSSH escape sequences?

They look neat though, better than having to restart the session to forward
ports. Too bad it doesn't work in PuTTY.

~~~
reacweb
They work in mobaXterm.

~~~
namibj
Which AFAIK uses a cygwin build of OpenSSH, the only non-cygwin builds are the
main GUI, maybe some helper tools, and the X11 Server. The commandline is a
cygwin busybox.

------
snvzz
It's in the manpage.

~~~
majewsky
To be fair, who of us reads every manpage for every command that they use?
(And I mean _reading_ , not just skimming.)

I mean, it's a good practice to read manpages every now and then, just to
learn new stuff (in the same way that there are always new vim commands to
learn). But we encounter so many comands with so many options that knowing all
of the options is just implausible.

~~~
snvzz
This topic has a whole section in the manpage, though. It's not easy to miss
even if just skimming, because of that.

------
jwilk
Non-AMP version:

[https://lonesysadmin.net/2011/11/08/ssh-escape-sequences-
aka...](https://lonesysadmin.net/2011/11/08/ssh-escape-sequences-aka-kill-
dead-ssh-sessions/)

~~~
hnarn
What's wrong with the AMP version?

~~~
wjoe
Aside from the common dislike for AMP centralising everything on Google's
servers, I also dislike AMP links for the same reason I dislike mobile links
to sites when I'm using a desktop PC.

The AMP versions usually lose formatting and links, and are designed to be
read on a small mobile screen. I'd rather have the full version when I have a
big screen available.

~~~
tokenizerrr
Did you even click the link? It's not hosted on Google's server and it looks
great. Much less clutter than the non-AMP version, and loads faster as well of
course.

