TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Closing a stale SSH connection

188 点作者 granddave大约 2 年前

19 条评论

tjoff大约 2 年前
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 未加载
maratc大约 2 年前
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 未加载
capableweb大约 2 年前
Lots of words to just say &quot;Enter then ~.&quot;
评论 #35509314 未加载
评论 #35508359 未加载
dingosity大约 2 年前
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 未加载
blueflow大约 2 年前
Primary source is the SSH manpage, Section &quot;ESCAPE CHARACTERS&quot;. Read it from your terminal with the `man ssh` command.
abdusco大约 2 年前
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 未加载
scsibug大约 2 年前
Another good tip; when randomly generating passwords (especially for other users), filter out anything starting with a tilde to prevent strange behavior.
评论 #35508412 未加载
pstrateman大约 2 年前
Being able to change port forwarding without reconnecting is really useful too.
hartator大约 2 年前
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 未加载
motge大约 2 年前
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)
yubiox大约 2 年前
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 未加载
davesmylie大约 2 年前
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 未加载
LinuxBender大约 2 年前
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 未加载
pabs3大约 2 年前
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.
bullen大约 2 年前
I would love the opposite, how to keep a SSH connection alive when sleeping the computer? XD
评论 #35510442 未加载
评论 #35510377 未加载
riffic大约 2 年前
tilde dot is my jam.
est大约 2 年前
that typeface makes the tilde prefix (~) almost looks like a hyphen (-)
hsjqllzlfkf大约 2 年前
Why is this better than CTRL-C?
评论 #35505887 未加载
评论 #35505880 未加载
评论 #35507781 未加载
评论 #35505861 未加载
c0l0大约 2 年前
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 未加载