我经常从学校 ssh 到我家里的盒子,但通常当我换课并且我的电脑挂起时,管道会被打破。但是, ssh 只是锁定 - Ctrl+ c,Ctrl+z和Ctrl+d没有效果。
必须重新启动我的终端很烦人,更烦人的是必须关闭并重新创建一个新的屏幕窗口。
所以我的问题是,是否有一种简单的方法可以让 ssh 正常终止(即,当管道“正常”失败时,它将退出并显示有关管道损坏的消息)?或者我必须弄清楚PID是什么并手动杀死它?
gee*_*aur 599
普通密钥通过ssh会话转发,因此这些都不起作用。相反,使用转义序列。随后,为了杀死当前会话命中Enter,~,.。
(请记住,在~设置为作曲字符的国际键盘中,您必须按两次:Enter, ~, ~, .)
更多这些转义序列可以用Enter, ~,列出?:
Supported escape sequences:
~. - terminate connection (and any multiplexed sessions)
~B - send a BREAK to the remote system
~C - open a command line
~R - request rekey
~V/v - decrease/increase verbosity (LogLevel)
~^Z - suspend ssh
~# - list forwarded connections
~& - background ssh (when waiting for connections to terminate)
~? - this message
~~ - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)
Run Code Online (Sandbox Code Playgroud)
您可以通过点击 关闭转义序列列表Enter。
请注意,因为点击~~导致ssh发送~而不是拦截它,所以您可以通过点击N次来解决N 个嵌套ssh连接。(这仅适用于直接跟在s 之后的 s 。)也就是说,终止5 层深的会话并保持其他 4 层完整。~ ~EnterEnter~~~~~.ssh
uli*_*tko 61
您可能还想为 SSH设置应用程序级保持活动,以防止它在连接问题上冻结。我的~/.ssh/config包含这个:
Host *
ServerAliveInterval 15
# ServerAliveCountMax 3
Run Code Online (Sandbox Code Playgroud)
这使得 ssh 客户端每 15 秒发送一次应用程序级保持活动。每当其中三个连续失败(默认为ServerAliveCountMax)时,客户端将连接视为挂起并关闭它。
与另一个选项相反TCPKeepAlive,这是在加密通道中检查的,并且是不可欺骗的。
值得注意的是,那些保持连接也有助于,嗯,保持长时间空闲的连接有效,即防止您将半关闭的tcp 会话挂起数小时而不受影响。
如果您经常遇到此问题,我强烈建议您打开此功能,但您也应该了解它可能带来的轻微安全风险。一个已知明文攻击,如果攻击者知道了间隔和空闲连接的内容可能会变得更加容易。这可能是默认情况下未启用它的原因。
sco*_*ttl 46
如 geekosaur 的回答所述,转义序列~.将终止连接。
可以通过键入~?以下内容显示转义序列的完整列表及其功能:
Supported escape sequences:
~. - terminate connection (and any multiplexed sessions)
~B - send a BREAK to the remote system
~C - open a command line
~R - Request rekey (SSH protocol 2 only)
~^Z - suspend ssh
~# - list forwarded connections
~& - background ssh (when waiting for connections to terminate)
~? - this message
~~ - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)
Run Code Online (Sandbox Code Playgroud)