在网络不好的环境下,或者是 SSH 连接的超时时间设置的较短的情况下,经常会遇到 SSH 连接没有响应的情况。我一般的解决方法是直接关闭 SSH 进程所在的上级进程(比如直接关闭当前终端窗口)。今天突然觉得有些麻烦,猜想应该存在相应的解决方案。
所幸经过一番搜索在 StackOverflow 找到了如何直接断开 SSH 连接的方法。
在具有已挂起 SSH 进程的终端下面依次输入 Enter
、~.
,就可以直接断开当前的连接并退出 SSH。
这里的 ~.
就是 SSH 的控制字符。通过 man ssh
,可以找到更多的控制字符的用法。以下摘自 ssh 的 manpage:
ESCAPE CHARACTERS When a pseudo-terminal has been requested, ssh supports a number of functions through the use of an escape character. A single tilde character can be sent as ~~ or by following the tilde by a character other than those described below. The escape character must always follow a newline to be interpreted as special. The escape character can be changed in configuration files using the EscapeChar configuration directive or on the command line by the -e option. The supported escapes (assuming the default ‘~’) are: ~. Disconnect. ~^Z Background ssh. ~# List forwarded connections. ~& Background ssh at logout when waiting for forwarded connection / X11 sessions to terminate. ~? Display a list of escape characters. ~B Send a BREAK to the remote system (only useful if the peer supports it). ~C Open command line. Currently this allows the addition of port forwardings using the -L, -R and -D options (see above). It also allows the cancellation of existing port-forwardings with -KL[bind_address:]port for local, -KR[bind_address:]port for remote and -KD[bind_address:]port for dynamic port-forwardings. !command allows the user to execute a local command if the PermitLocalCommand option is enabled in ssh_config(5). Basic help is available, using the -h option. ~R Request rekeying of the connection (only useful if the peer supports it). ~V Decrease the verbosity (LogLevel) when errors are being written to stderr. ~v Increase the verbosity (LogLevel) when errors are being written to stderr.
这也再次说明了,一个撰写良好的 manual 是对一个命令最完整的教程。
怎么钻到挂起的 SSH 的进程下面?
这个是这样的,就是比如说某个终端窗口里连接到远程服务器的 SSH 因为网络原因失去响应了就可以用控制指令退出 SSH 程序而不用关闭整个终端窗口。