失去连接且 ssh 开启时终端挂起

失去连接且 ssh 开启时终端挂起

当我在 gnome 终端选项卡中通过互联网 ssh 到服务器时,如果我失去了互联网连接,终端选项卡将挂起并且不接受任何输入。为什么会挂?

有没有办法激活终端选项卡,即让它继续运行本地 shell 进程?

关闭终端选项卡是唯一的方法吗?

答案1

ClientAliveIntervalSSH 连接在参数及其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_configssh_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 会话已断开连接。

相关内容