SSH会话连接超时问题

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/114008.html原文链接:https://javaforall.net

(0)
上一篇 2021年10月7日 下午1:00
下一篇 2021年10月7日 下午2:00


相关推荐

  • 背包九讲问题——超详细

    背包九讲问题——超详细Acwing背包题库一.01背包问题问题描述有N件物品和一个容量是VV的背包。每件物品只能使用一次。第i件物品的体积是vi,价值是wi。求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大。输出最大价值。输入格式第一行两个整数,N,V,用空格隔开,分别表示物品数量和背包容积。接下来有N行,每行两个整数vi,wi,用空格隔开,分别表示第i件物品的体积和价值。输出格式输出一个整数,表示最大价值。数据范围0<N,V≤100

    2022年6月28日
    30
  • 用vb.net实现写字板程序报告(二)

    用vb.net实现写字板程序报告(二)所有源代码均在这里下载:http://www.up2e.com/resource.php 用vb.net实现写字板程序报告(二)–byzigz(LuHai)luluhai@eastday.com 3)           状态栏的隐藏就是在“查看”菜单中有个check按钮,当checked=true时点击它状态栏就隐藏,反之就取消隐藏。PrivateSubmSt

    2022年7月16日
    21
  • pytorch之torch.nn.Conv2d()函数详解

    pytorch之torch.nn.Conv2d()函数详解文章目录一 官方文档介绍二 torch nn Conv2d 函数详解参数详解参数 dilation 扩张卷积 也叫空洞卷积 参数 groups 分组卷积三 代码实例一 官方文档介绍官网 nn Conv2d 对由多个输入平面组成的输入信号进行二维卷积二 torch nn Conv2d 函数详解参数详解 torch nn Conv2d in channels out channels kernel size stride 1 padding 0 dilation 1 groups

    2026年3月20日
    2
  • qtreewidget基本使用_qtreewidget列宽自适应

    qtreewidget基本使用_qtreewidget列宽自适应1、voidQTreeWidget::setHeaderLabels(constQStringList&labels)设置表头,QStringList有几项,表头就有多少列2、voidQHeaderView::setSortIndicatorShown(boolshow)Thispropertyholdswhetherthesortindicatoris

    2022年10月1日
    4
  • Latex输入矩阵的几种方式

    Latex输入矩阵的几种方式直接用 matrix pmatrix bmatrix Bmatrix vmatrix 或者 Vmatrix 环境 begin gathered begin matrix 0 amp 1 1 amp 0 end matrix quad begin pmatrix 0 amp i i amp 0 end pmatrix quad beg

    2026年3月17日
    2
  • SpringBoot 微信第三方登陆详解

    SpringBoot 微信第三方登陆详解第一步这篇文章只讲三方登陆主要实现步骤 不讲理论这是详细理论拿到微信拉取二维码的接口地址建一个网页设一个跳转 a 标签接口地址 https open weixin com connect qrconnect appid APPID amp redirect uri REDIRECT URI amp response type code amp scope SCOPE amp state STATE wechat redirect 开发平台创建完成后 获取到 appid 和 appsecre

    2026年3月19日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号