TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Closing a stale SSH connection

188 pointsby granddaveabout 2 years ago

19 comments

tjoffabout 2 years ago
One caveat here is the note:<p>&gt; <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&#x27;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&#x27;t too bad either.
评论 #35510434 未加载
评论 #35507464 未加载
评论 #35507720 未加载
maratcabout 2 years ago
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>(&quot;control up-arrow Q&quot; song playing in the background.)
评论 #35506002 未加载
评论 #35507011 未加载
评论 #35505850 未加载
capablewebabout 2 years ago
Lots of words to just say &quot;Enter then ~.&quot;
评论 #35509314 未加载
评论 #35508359 未加载
dingosityabout 2 years ago
I feel old. I remember when we moved from telnet and rlogin to ssh and this was in the man page. I&#x27;m also happy to learn kids are still using terminals and ssh.
评论 #35507204 未加载
评论 #35507765 未加载
评论 #35506346 未加载
blueflowabout 2 years ago
Primary source is the SSH manpage, Section &quot;ESCAPE CHARACTERS&quot;. Read it from your terminal with the `man ssh` command.
abduscoabout 2 years ago
One of the most useful SSH tricks I&#x27;ve ever learned. I wonder if there&#x27;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:&#x2F;&#x2F;mosh.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mosh.org&#x2F;</a>
评论 #35504934 未加载
评论 #35505876 未加载
scsibugabout 2 years ago
Another good tip; when randomly generating passwords (especially for other users), filter out anything starting with a tilde to prevent strange behavior.
评论 #35508412 未加载
pstratemanabout 2 years ago
Being able to change port forwarding without reconnecting is really useful too.
hartatorabout 2 years ago
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.
评论 #35505159 未加载
评论 #35510197 未加载
评论 #35506568 未加载
评论 #35505092 未加载
motgeabout 2 years ago
Also very helpful: There is ~C which opens a &quot;command&quot; mode where you can add local or remote forwardings after you opened a SSH session (e.g. -L8080:localhost:8080)
yubioxabout 2 years ago
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.
评论 #35513494 未加载
davesmylieabout 2 years ago
Pretty sure the answer is no, cause I&#x27;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.
评论 #35507503 未加载
评论 #35507216 未加载
LinuxBenderabout 2 years ago
Adding for completeness sake, if using Alpine Linux the ssh escape sequence menu and output will not display correctly if using &#x2F;bin&#x2F;ash <i>from BusyBox</i> as your login shell. One work-around is to type &#x27;cat [Return]&#x27; and then use the escape sequences, or to <i>carefully</i> change your shell to &#x2F;bin&#x2F;bash
评论 #35504971 未加载
评论 #35505032 未加载
pabs3about 2 years ago
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.
bullenabout 2 years ago
I would love the opposite, how to keep a SSH connection alive when sleeping the computer? XD
评论 #35510442 未加载
评论 #35510377 未加载
rifficabout 2 years ago
tilde dot is my jam.
estabout 2 years ago
that typeface makes the tilde prefix (~) almost looks like a hyphen (-)
hsjqllzlfkfabout 2 years ago
Why is this better than CTRL-C?
评论 #35505887 未加载
评论 #35505880 未加载
评论 #35507781 未加载
评论 #35505861 未加载
c0l0about 2 years ago
If you enjoyed this article, you may also find other useful tips in a piece I submitted to HN a while ago on &quot;advanced SSH usage&quot;: <a href="https:&#x2F;&#x2F;johannes.truschnigg.info&#x2F;writing&#x2F;2022-07_advanced_ssh_usage&#x2F;" rel="nofollow">https:&#x2F;&#x2F;johannes.truschnigg.info&#x2F;writing&#x2F;2022-07_advanced_ss...</a>
评论 #35507032 未加载
评论 #35505561 未加载