李从儒分享 http://blog.sciencenet.cn/u/Licr

博文

Linux反空闲设置

已有 4919 次阅读 2015-12-15 13:46 |个人分类:Linux操作|系统分类:科研笔记

用客户端工具,例如securecrt连接linux服务器,有的会出现过一段时间没有任何操作,客户端与服务器就断开了连接。造成这个的原因,主要是因为客户端与服务器之间存在路由器,防火墙以及为了本身的安全性,在超过特定的时间后就会把空闲连接断开。或者是服务器端设置了断开空闲连接。那么解决的方法有两种,一是从服务器着手,一是在客户端工具上下手。


根据操作系统不同,分为在服务器端Linux系统上直接进行的修改命令行参数,以及可以在用户windows系统上进行的设置CRT参数。


我使用的是直接修改命令行参数的方式,过程如下:

找到/ect/ssh/ssh_config文件,使用sudo vim ssh_config命令修改,在末尾补充

ServerAliveInterval 60

这表明每60s向服务器(大型机)发送一次命令。


在网站上也看到其他人分享的经验,包括对CRT参数的修改,诸如在http://blog.csdn.net/wodepuwei/article/details/14000411 看到的如下经验:

方式一: 修改命令行参数
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、修改/etc/ssh/sshd_config文件,将 ClientAliveInterval 0和ClientAliveCountMax 3的注释符号去掉,将ClientAliveInterval对应的0改成60,ClientAliveInterval指定了服务器端向客户端请求消息 的时间间隔, 默认是0, 不发送.而ClientAliveInterval 60表示每分钟发送一次, 然后客户端响应, 这样就保持长连接了.ClientAliveCountMax, 使用默认值3即可.ClientAliveCountMax表示服务器发出请求后客户端没有响应的次数达到一定值, 就自动断开. 正常情况下, 客户端不会不响应.

最后记得执行/etc/init.d/sshd restart 哦,否则刚才的修改是不会生效的。

或:

修改/etc/ssh/sshd_config配置文件 ClientAliveInterval 300(默认为0),参数的是意思是每5分钟,服务器向客户端发一个消息,用于保持连接,使用service sshd reload 让其修改后生效。如果发现还是有问题,可以试着把300设置小一点,例如60。

想参考更多,请输入man sshd_config了解更多信息

方式二:设置CRT参数(反空闲和屏幕日志记录功能)

1.反空闲

   使用SecureCRT连接公网,经常会出现断掉的情况,一般采用top,或者ping某个ip的方式保证ssh连接处于激活状态,其实SecureCRT自身的反空闲功能也可以达到此目的,具体配置为:

选项------会话选项-----终端-----反空闲,可以设置发送字符串为n,每10秒发送一次,这样就可以达到每10秒发送一个回车,保证ssh连接处于激活状态。

2.屏幕日志记录功能

   屏幕日志记录,可以记录自己执行过的所有命令和输出,便于以后追踪查找问题。

具体配置为:

选项-----全局选项-----默认会话-----编辑默认设置,弹出会话选项框,选择日志文件,可以配置日志文件名、选项、自定义日志数据,具体格式见日志文件名和自定义日志数据替换部分。

securecrt的设置方法:会话选项 –> 终端 –> 反空闲–>选中发送协议 NO-OP(p) 每 60 秒

putty的设置方法:putty -> Connection -> Seconds between keepalives ( 0 to turn off ), 默认为0, 改为300


内容补充:

ServerAliveInterval: number of seconds that the client will wait before sending a null packet to the server (to keep the connection alive).

ClientAliveInterval: number of seconds that the server will wait before sending a null packet to the client (to keep the connection alive).

Setting a value of 0 (the default) will disable these features so your connection could drop if it is idle for too long.

ServerAliveInterval seems to be the most common strategy to keep a connection alive. To prevent the broken pipe problem, here is the ssh config I use in my .ssh/config file:

Host myhostshortcut HostName myhost.com User barthelemy ServerAliveInterval 60

ClientAliveCountMax on the server side might also help. This is the limit of how long a client are allowed to stay unresponsive before being disconnected. The default value is 3, as in three ClientAliveInterval.






https://blog.sciencenet.cn/blog-2458445-943543.html

上一篇:Perl基础操作
下一篇:不错的学习网站(不断增添中)
收藏 IP: 124.16.129.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-10-20 04:30

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部