断开连接并打开ssh时终端挂起


18

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

有什么方法可以激活终端选项卡,即使其继续运行本地Shell进程吗?

关闭终端标签是唯一的方法吗?

Answers:


20

在由ClientAliveIntervalClientAliveCountMax参数以及它们的客户端等效设置所设置的指定时间后,SSH连接会自动断开。如果这些超时时间很高,您将遇到冻结的外壳。但是,如果您使用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+ `键),将其释放并按.。另外,如果您使用不稳定的连接,或者需要始终连接到远程服务器,则可以使用autossh自动更新丢失的连接,这非常方便。

编辑

但是,如果将ClientAliveIntervalServerAliveInterval都显式设置为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会话是否断开连接。


6
我想补充一点,ssh只能在开始时或在Enter之后识别其转义字符。当ssh连接卡住时,Enter通常但并非总是您之前按下的最后一个字符。因此,最好习惯于Enter ~ .终止连接的序列。
egmont 2015年

谢谢。我仍然不清楚如何/proc/sys/net/ipv4/tcp_keepalive*文件,ClientAliveIntervalServerAliveInterval 一起工作?它们是否都具有相同的设置,即保持ssh连接有效?以前的文件不仅用于ssh连接,还用于其他TCP连接吗?
所有

另一个问题:为什么“如果这些超时时间很高,您将遇到冻结的外壳。” “超时时间很高”是什么意思?如果在指定的时间内没有活动,外壳程序是否会始终冻结?还是在某些缺乏活动的情况下,外壳未冻结但会自动退出?
2015年

@Tim:1. /proc/sys/net/ipv4/tcp_keepalive*不仅是因为,ssh(d)而且正如我链接到的文档中所述:Remember that keepalive support, even if configured in the kernel, is not the default behavior in Linux. Programs must request keepalive control for their sockets using the setsockopt interface. There are relatively few programs implementing keepalive, but you can easily add keepalive support for most of them following the instructions explained later in this document.
Arkadiusz Drabczyk 2015年

@Tim:2.很高意味着例如2个小时-您将必须等待2个小时才能断开会话。进行测试-与远程主机连接,杀死远程主机sshd或拔下电缆,然后检查冻结的 ssh会话何时会自行退出。通过frozen shell我的意思是外壳,是无效的,不带任何按键,并且不显示任何信息。
2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.