![失去连接且 ssh 开启时终端挂起](https://linux22.com/image/63155/%E5%A4%B1%E5%8E%BB%E8%BF%9E%E6%8E%A5%E4%B8%94%20ssh%20%E5%BC%80%E5%90%AF%E6%97%B6%E7%BB%88%E7%AB%AF%E6%8C%82%E8%B5%B7.png)
当我在 gnome 终端选项卡中通过互联网 ssh 到服务器时,如果我失去了互联网连接,终端选项卡将挂起并且不接受任何输入。为什么会挂?
有没有办法激活终端选项卡,即让它继续运行本地 shell 进程?
关闭终端选项卡是唯一的方法吗?
答案1
ClientAliveInterval
SSH 连接在参数及其ClientAliveCountMax
客户端等效项设置的指定时间段后自动断开。如果这些超时时间相当长,您将遇到冻结的外壳。但是,如果您使用,OpenSSH
则不必等待超时,并且可以使用以下命令强制关闭连接转义字符:
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.
(...)
当连接冻结时,按~(这意味着Shift+`键一起),松开并按.。或者,如果您的连接不稳定或需要始终连接到远程服务器,您可以使用自动SSH自动恢复失去的连接,非常方便。
编辑:
但是,如果ClientAliveInterval
和均ServerAliveInterval
显式设置为 0 或未显式设置,然后根据sshd_config
和ssh_config
联机帮助页默认设置为 0,则超时设置在以下文件中设置(来自http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/usingkeepalive.html):
# cat /proc/sys/net/ipv4/tcp_keepalive_time
7200
# cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75
# cat /proc/sys/net/ipv4/tcp_keepalive_probes
9
The first two parameters are expressed in seconds, and the last is
the pure number. This means that the keepalive routines wait for
two hours (7200 secs) before sending the first keepalive probe,
and then resend it every 75 seconds. If no ACK response is
received for nine consecutive times, the connection is marked as
broken.
您只需使用即可修改这 3 个文件,echo
并根据这些值自行查看冻结的 SSH 会话已断开连接。