One caveat here is the note:<p>> <i>(Note that escapes are only recognized immediately after newline.)</i><p>This means that it is easy to pick up a habit to smash the enter-button a few times before doing this dance, and as noted on a nordic layout it can be a bit tricky and since you might seldom do it you might do it a few times.<p>Problem can be that sometimes the connection is only broken one way, what you are typing goes to the server but the responses don't. So you might end up wreaking some kind of havoc on the remote server when you just want to kill the session. Maybe you had a half-written command. Maybe you had just done an up-arrow to get to the previous command, maybe you redid that up-arrow one or more times before you realized that the connection was broken. If you press enter now you will re-run one of your previous commands. Could be quite scary.<p>To save you from some of that, you could do a ctrl+c which will clear your current line, before pressing enter. But whether that is a good idea depends on the context...<p>The most apparent issue this has been for me is if on the remote you have IRC or something and you type a bunch of garbage to whatever channel you are on. No biggie, but the old restart the terminal isn't too bad either.
Pro tip: if you used ssh to get into host A and then another ssh to get from host A into host B, to break the A-B connection you need to issue `~~.`<p>("control up-arrow Q" song playing in the background.)
I feel old. I remember when we moved from telnet and rlogin to ssh and this was in the man page. I'm also happy to learn kids are still using terminals and ssh.
One of the most useful SSH tricks I've ever learned. I wonder if there's a way to detect a stale session and force reconnect when I turn on the computer? Like mosh[0], but with SSH.<p>[0]: <a href="https://mosh.org/" rel="nofollow">https://mosh.org/</a>
Another good tip; when randomly generating passwords (especially for other users), filter out anything starting with a tilde to prevent strange behavior.
I wish there is an easy way to have the reverse: a “sticky” ssh session.<p>It’s so annoying that the connection is lost when going to sleep or network issues. And the solutions to fix this are not really worth the effort.
Also very helpful: There is ~C which opens a "command" mode where you can add local or remote forwardings after you opened a SSH session (e.g. -L8080:localhost:8080)
Probably too late to post here but the timing of this article was interesting. I use the ~. on a daily basis because something in the firewall at work was causing my idle ssh connections to hang. But just yesterday morning right before reading this article I added ServerAliveInterval 30 in the client ssh config to see if it would help and guess what, the connections were not stuck today.
Pretty sure the answer is no, cause I've been searching for a way to do this for a while, but is there any way to trigger these escapes from script?<p>Use case is bouncing through to an RDS that only allows access from specific EC2 instances. The RDS endpoint in question is highly specific to the EC2 - not a big deal to hit ~L and create the forward manually, but doing this automatically would be great.
Adding for completeness sake, if using Alpine Linux the ssh escape sequence menu and output will not display correctly if using /bin/ash <i>from BusyBox</i> as your login shell. One work-around is to type 'cat [Return]' and then use the escape sequences, or to <i>carefully</i> change your shell to /bin/bash
Due to my use of the ControlMaster feature, I usually just do `ssh -O exit foo` in another terminal to close an existing connection. Either that or just close the terminal and open a new one.
If you enjoyed this article, you may also find other useful tips in a piece I submitted to HN a while ago on "advanced SSH usage": <a href="https://johannes.truschnigg.info/writing/2022-07_advanced_ssh_usage/" rel="nofollow">https://johannes.truschnigg.info/writing/2022-07_advanced_ss...</a>