SSH会话连接超时问题

目前大多数ssh服务是运行在Linux系统上的sshd服务。当访问终端在windows上时,各终端软件,如,putty,SecureCRT等,大多支持设置向服务器端自动发送消息,来防止终端定期超时。其实,服务器端也支持类似的设置,从服务器的角度防止链接超时。并且,当终端在Ubuntu 等Linux系统上时,客户端也可进行类似设置。
下面我们就介绍三种防止超时被踢出的方法,后两种情况的设置方法以及通过设置shell变量来达到此目的的方法:

1、 配置服务器

#vi /etc/ssh/sshd_config

1)找到 ClientAliveInterval参数,如果没有就自己加一行
数值是秒,比如你设置为120 ,则是2分钟
ClientAliveInterval 120

2)ClientAliveCountMax
指如果发现客户端没有响应,则判断一次超时,这个参数设置允许超时的次数。如3 、5等自定义
 
修改两项参数后如下:
----------------------------
ClientAliveInterval 120
ClientAliveCountMax 3   ### 0 不允许超时次数
 
修改/etc/ssh/sshd_config文件,将 ClientAliveInterval 0和ClientAliveCountMax 3的注释符号去掉,将ClientAliveInterval对应的0改成60,没有就自己输入。
ClientAliveInterval指定了服务器端向客户端请求消息 的时间间隔, 默认是0, 不发送.而ClientAliveInterval 60表示每分钟发送一次, 然后客户端响应, 这样就保持长连接了.ClientAliveCountMax, 使用默认值3即可.ClientAliveCountMax表示服务器发出请求后客户端没有响应的次数达到一定值, 就自动断开. 正常情况下, 客户端不会不响应.
重新加载sshd服务。退出客户端,再次登陆即可验证。
 
3)重启sshd service
sudo /etc/init.d/ssh restart


2、 配置客户端

#vi  /etc/ssh/ssh_config
然后找到里面的
ServerAliveInterval
参数,如果没有你同样自己加一个就好了
参数意义相同,都是秒数,比如5分钟等

ServerAliveInterval 300

下面是man sshd_config获取的描述信息

 ClientAliveInterval
             Sets a timeout interval in seconds after which if no data has
             been received from the client, sshd 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 version 2
             only.

 ClientAliveCountMax
             Sets the number of client alive messages (see above) which may
             be sent without sshd receiving any messages back from the
             client.  If this threshold is reached while client alive mes-
             sages are being sent, sshd will disconnect the client, terminat-
             ing the session.  It is important to note that the use of client
             alive messages is very different from TCPKeepAlive (below).  The
             client alive messages are sent through the encrypted channel and
             therefore will not be spoofable.  The TCP keepalive option
             enabled by TCPKeepAlive is spoofable.  The client alive mecha-
             nism is valuable when the client or server depend on knowing
             when a connection has become inactive.

             The default value is 3.  If ClientAliveInterval (above) is set
             to 15, and ClientAliveCountMax is left at the default, unrespon-
             sive ssh clients will be disconnected after approximately 45
             seconds.

3、echo export TMOUT=1000000 >> /root/.bash_profile; source .bash_profile
       在Linux 终端的shell环境中通过设置环境变量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

### 可能的原因 SSH连接超时可能由多种因素引起,包括网络配置错误、防火墙设置不当以及SSH服务本身的配置问题。对于MacOS下的SSH连接服务器超时的情况,通常可以从以下几个方面着手排查和解决问题[^1]。 ### 检查本地网络环境 确保本地设备能够正常访问互联网,并尝试通过其他方式(如浏览器)确认可以到达目标服务器所在的地址或域名。如果发现只有特定的服务不可达,则可能是路由选择或者ISP层面的问题所致[^2]。 ### 验证SSH端口可达性 使用`telnet`或其他工具检测能否成功建立到目的主机指定端口号(默认为22)上的TCP连接: ```bash telnet example.com 22 ``` 若上述命令返回“Connection refused”,则表明远端未开启该端口;如果是“connection timeout”,那么很可能是由于中间存在阻止流量的安全策略或是路径不通造成的阻塞现象。 ### 修改SSH客户端配置文件 编辑位于用户目录下的`.ssh/config`文件,在其中加入如下参数以优化握手过程并提高稳定性: ```plaintext Host * ServerAliveInterval 60 TCPKeepAlive yes ConnectTimeout 30 ``` 这些选项的作用分别是定期发送心跳包保持会话活跃状态、启用底层协议级别的持久化机制防止意外断开,还有就是设定了最大等待响应的时间长度,从而减少不必要的延迟影响用户体验[^3]。 ### 排除DNS解析相关障碍 有时即使IP层面上通信无碍也会因为名称解析失败而导致最终无法完成认证流程。因此建议先利用ping指令验证是否可以直接定位到对方的真实物理位置,再进一步考虑调整本机hosts记录临时绕过公共查询渠道带来的不确定性干扰源。 ### 测试不同加密算法兼容情况 部分老旧版本的操作系统可能会遇到新式密钥交换方法不被支持的情形,这时不妨试着降级采用更广泛的RSA而非ECDSA作为公私钥对生成依据之一来进行匹配尝试: ```bash ssh -o HostKeyAlgorithms=+ssh-rsa user@example.com ``` 此做法可以在一定程度上规避掉因双方协商过程中产生的分歧所引发的一系列连锁反应,进而顺利建立起安全隧道实现数据交互的目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值