8 ms·
SSH Escape Sequences
- unkown-unknowns 9y agoI 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 9y agoFortunately you only need to remember ~? since it tells you the rest.
- Groxx 9y agoUnfortunately, ~? is two characters too many for me to remember a year later :|
- anyfoo 9y agoDo 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 9y agoNot 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.
- majewsky 9y agoSince I didn't know about <Enter>~. until a few days ago, I usually `killall ssh` or the specific PID.
- Groxx 9y agoTypically I just wait for timeout, or open a new session (remote tmux session makes this quite painless). Plenty of other things to do, usually, waiting a couple minutes every week or three doesn't bug me enough to google.
- gumby 9y agoIf you need them but don’t remember them just use man ssh in another terminal.
- deleted 9y ago[deleted]
- CaliforniaKarl 9y agoI 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 9y agoOmg thank you. Same exact use case and I can’t believe I didn’t realize this.
- emmelaich 9y agoIt'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 9y agoSpeaking 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 9y agoSee 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 9y agoI 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
- snvzz 9y agoIt's in the manpage.
- majewsky 9y agoTo 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 9y agoThis topic has a whole section in the manpage, though. It's not easy to miss even if just skimming, because of that.
- JoshTriplett 9y agoNote that these only work at the beginning of a "line", so you may have to hit enter before the tilde.
- anyfoo 9y agoYeah. I can understand where they are coming from, at least with tilde as the escape character it would be highly inconvenient if it happened all the time, but I still a bit unhappy about having to press enter before being able to terminate a stuck session. That's usually not something I want to actually send, should the session not be as dead as I thought it was.
- emmelaich 9y agoAnd if your terminal state is hosed by a crashed cursor-addressing (curses) program, you may have to use ^J instead of Enter.
- anyfoo 9y agoAre 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 9y agoHmm, 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 9y agoYeah, 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.
- kylek 9y agoI 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.
- wiredfool 9y agoThis is useful to know, especially if it works with agent forwarding. I default to agent forwarding off, but invariably forget to enable it the few times I need it.
- mpfz0r 9y agoI 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 9y agoI 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...
- anyfoo 9y agoYep, as I wrote below another comment, I share the same sentiment. To mitigate the risk somewhat, I tend to press Ctrl-C a few times before hitting enter to initiate the escape sequence (there are some edge cases where you need more than one Ctrl-C to interrupt). Still not satisfied about it, as there are plenty of situations where that doesn't help. Being in emacs, for example, it could make things worse...
- derefr 9y agoHow about typing "Ctrl+C Ctrl+C e x i t Enter", and then adding the "~ ." if that doesn't work?
- peterwwillis 9y agoAlways use Ctrl+C Enter ~. Enter. Just think of it as an ssh Konami code. If you've sshed into another box first, use ~~. to only reset the embedded session.
- phillipwills 9y agoI've posted random ~. in irc because I did it in the wrong order...
- jtl999 9y agocough accidently sending half-pasted gibberish to IRC.
- mjevans 9y agoMy solution is to use a stateless in-application (where I expect to be) command to switch to a different window (In this case, the server chatter window) where pasting the line is an error since there's no command. It usually works... I agree that it would be nice if /any/ terminal escape sequence followed by ~ would initiate the escape.
- gear54rus 9y agoHow 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.
- phillipwills 9y agoPretty sure you can edit a lot of these session settings in putty while it is running. It's in the menu, I think (been a while since I used putty).
- reacweb 9y agoThey work in mobaXterm.
- namibj 9y agoWhich 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.
- cat199 9y agobecause for just about everyone, ssh is openssh for better or worse. true though.
- vesinisa 9y agoI checked, and it is indeed an OpenSSH feature. According to the release notes, it was added in v2.2.0, but you can actually tell just by the perverse choice of ~ as an escape character! In many European keyboard layouts, including the Finnish layout used by original SSH's author, tilde (~) is a compose key and a chore to type. It is usually expressed by typing AltGr+¨ followed by Spacebar: https://en.wikipedia.org/wiki/Tilde#Keyboards https://en.wikipedia.org/wiki/Tilde#Keyboards English is one of the few keyboard layouts where a person can express ~ with one, rather than two, handstrokes. For others, terminating an SSH session with the escape character usually requires blindly typing this four-part sequence: Enter, AltGr+¨, Spacebar, Dot. I know you can change the escape character with ssh_config(5), but no one who used a non-English layout would have ever chosen ~ as the default to start with. I've always felt like it had to be an OpenSSH-specific feature, because I knew the original author of SSH (before the open-source codebase became OpenSSH) used the same KB layout as myself. ;)
- selectnull 9y agoI 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.
- __david__ 9y agoAlso 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.
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]
- userbinator 9y agoBasically, 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.
- barrkel 9y agoYou need to enter ~ after pressing return (or else you have a bad time editing command lines involving home directories), so in practice only the first ~ each SSH instance sees needs escaping.
- schoen 9y agoIt's convenient there (and probably not accidental!) that Unix usernames are usually lowercase, so if you want to run a command like ~otheruser/bin/cat it won't conflict with an ssh escape sequence (although you won't see the ~ appear until you've typed ~o). On the other hand, ~Randomuser/bin/cat would be impossible to type immediately at a new command prompt over an ssh connection (though of course it's possible to run such a command by typing anything such as "x<Backspace>" first).
- kevhito 9y agoNo, not exponential. The escape sequence is: <newline> then '~'. Anything after the '~' is special. In other words, only the first '~' appearing directly after a new line is special. For example, <newline>~~~~ will send <newline>~~~ to the remote side.
- anilakar 9y agoOn 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 9y ago> 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 9y agoIf 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.
- OJFord 9y agoThis page takes ~5s to load if JS from cdn.ampproject.com is blocked...
- tokenizerrr 9y agoSo don't block it? You can expect a broken browsing experience if you purposely break your browser.
- notzorbo3 9y agoThis page is more broken than usual, so it's still interesting. Regardless of you snide remark.
- jwilk 9y agoNon-AMP version: https://lonesysadmin.net/2011/11/08/ssh-escape-sequences-aka-kill-dead-ssh-sessions/ https://lonesysadmin.net/2011/11/08/ssh-escape-sequences-aka...
- hnarn 9y agoWhat's wrong with the AMP version?
- wjoe 9y agoAside 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 9y agoDid 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.
- JustSomeNobody 9y agoSomewhat controversially, AMP is what’s wrong.
- empath75 9y agoThis is one of those things like discovering ^r that’s going to change my life.
- liveoneggs 9y agoI 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 9y agoI 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.
- JdeBP 9y agoOthers 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