ssh自动断开解决办法

 

用putty/SecureCRT连续3分钟左右没有输入, 就自动断开, 然后必须重新登陆, 很麻烦.

在网上查了很多资料, 发现原因有多种, 环境变量TMOUT引起,ClientAliveCountMax和ClientAliveInterval设置问题或者甚至是防火墙的设置问题. 所以可以这么尝试:

1, echo $TMOUT
如果显示空白,表示没有设置, 等于使用默认值0, 一般情况下应该是不超时. 如果大于0, 可以在如/etc/profile之类文件中设置它为0.
Definition: TMOUT: If set to a value greater than zero, the value is interpreted as the number of seconds to wait for input after issuing the primary prompt. Bash terminates after waiting for that number of seconds if input does not arrive.


2. ClientAliveInterval 60
在/etc/ssh/sshd_config中增加ClientAliveInterval 60, ClientAliveInterval指定了服务器端向客户端请求消息的时间间隔, 默认是0, 不发送.而ClientAliveInterval 60表示每分钟发送一次, 然后客户端响应, 这样就保持长连接了.这里比较怪的地方是:不是客户端主动发起保持连接的请求(如FTerm, CTerm等),而是需要服务器先主动.

另外,至于ClientAliveCountMax, 使用默认值3即可.ClientAliveCountMax表示服务器发出请求后客户端没有响应的次数达到一定值, 就自动断开. 正常情况下, 客户端不会不响应.

 

ClientAliveCountMax
Sets the number of client alive messages (see below) which may be
sent without sshd(8) receiving any messages back from the client.
If this threshold is reached while client alive messages are
being sent, sshd will disconnect the client, terminating the ses-
sion. It is important to note that the use of client alive mes-
sages is very different from TCPKeepAlive (below). The client
alive messages are sent through the encrypted channel and there-
fore will not be spoofable. The TCP keepalive option enabled by
TCPKeepAlive is spoofable. The client alive mechanism is valu-
able when the client or server depend on knowing when a connec-
tion has become inactive.

 

The default value is 3. If ClientAliveInterval (see below) is
set to 15, and ClientAliveCountMax is left at the default, unre-
sponsive SSH clients will be disconnected after approximately 45
seconds. This option applies to protocol version 2 only.

 

ClientAliveInterval
Sets a timeout interval in seconds after which if no data has
been received from the client, sshd(8) will send a message
through the encrypted channel to request a response from the
client. The default is 0, indicating that these messages will
not be sent to the client. This option applies to protocol ver-
sion 2 only.

 

3. 启用putty keepalive
putty -> Connection -> Seconds between keepalives ( 0 to turn off ), 默认为0, 改为60. 
  
4.SecureCRT设置反空闲

option->session option->Terminal->Anti-idle->Send protocol NO-OP every__seconds

每隔几秒发送空字符串 保持链接不断开

 

### SSH 远程连接中断的原因分析 SSH 远程连接意外终止通常由多种因素引起,主要包括网络不稳定、服务器端超时设置不当以及客户端未正确处理保持活动状态等问题。以下是具体原因及对应的解决方案: #### 1. **网络波动** 网络环境中的丢包或延迟可能导致 SSH 连接被强制关闭。此类问题可以通过调整 SSH 的心跳机制来缓解。 解决方案之一是在客户端的 `~/.ssh/config` 文件中添加以下参数[^2]: ```plaintext Host * ServerAliveInterval 60 TCPKeepAlive yes ``` 参数解释如下: - `ServerAliveInterval`: 设置每隔多少秒向服务器发送一次心跳信号,默认单位为秒。 - `TCPKeepAlive`: 启用底层 TCP Keepalive 功能以维持连接活跃。 --- #### 2. **服务器端超时配置** 如果服务器上的 `/etc/ssh/sshd_config` 文件设置了过短的超时时间,则可能会导致无操作一段时间后连接自动断开。这通常是由于默认的 `ClientAliveInterval` 和 `ClientAliveCountMax` 参数值较低所致。 可通过编辑该文件并增加以下内容解决问题[^3]: ```plaintext ClientAliveInterval 60 ClientAliveCountMax 3 ``` 上述配置表示每分钟向客户端发送一次探针请求,并允许最多三次失败尝试后再切断连接。 编辑完成后需重新启动 sshd 服务使更改生效: ```bash systemctl restart sshd ``` --- #### 3. **防火墙或 NAT 超时** 当使用 NAT 或者某些企业级防火墙设备时,这些中间件可能也会因为长时间没有数据传输而主动释放闲置连接。此时除了优化 SSH 自身的心跳外还需要联系网络管理员确认其策略是否合理。 --- #### 4. **采用会话管理工具 Screen/Tmux** 对于那些即使采取上述措施仍无法完全杜绝掉线风险的任务场景(比如执行耗时较长的操作),建议利用 screen/tmux 工具创建持久化终端会话。这样即便发生短暂离线也不会影响后台运行程序继续工作. 安装方法 (以 CentOS为例): ```bash yum install tmux -y ``` 创建新 session 并命名: ```bash tmux new -s mysessionname ``` 列出当前存在的 sessions : ```bash tmux ls ``` 断开现有连接但保留 session 中的内容不变: ```bash Ctrl+b d ``` 之后可以从任何地方再次接入刚才退出的那个 session: ```bash tmux attach -t mysessionname ``` --- ### 总结 以上介绍了几种常见的 SSH 远程连接异常结束的现象及其对应处置办法。实际应用过程中可根据具体情况选用合适的技术手段加以应对。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值