linux服务器自动断开,解决centos7服务器用ssh连接老是自动断开问题

最近自己买了个服务器,用ssh连接时一段时间不操作就要重新连接,让人很是麻烦。现在终于解决了,步骤很简单。

登陆服务器后输入 lsb_release -a 可以查看系统版本,此命令适用于所 有的linux,包括Redhat、SuSE、Debian等发行版,但是在debian下要安装lsb。如:

[root@chen /]# lsb_release -a

LSB Version: :core-4.1-amd64:core-4.1-noarch

Distributor ID: CentOS

Description: CentOS Linux release 7.3.1611 (Core)

Release: 7.3.1611

Codename: Core

此方法适用于centos7的版本。用文本编译器打开ssh服务里面的一个配置文件。代码如下:

[root@chen /]# vi /etc/ssh/sshd_config

# $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $

# This is the sshd server system-wide configuration file. See

# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with

# OpenSSH is to specify options with their default value where

此时已经进入配置文件里面了,输入 i 进入编辑模式,找到如下代码:

#PermitUserEnvironment no#Compression delayed

#ClientAliveInterval 0

#ClientAliveCountMax3

#ShowPatchLevel no

#PidFile /var/run/sshd.pid

#MaxStartups 10:30:100

#PermitTunnel no

#ChrootDirectory none

#VersionAddendum none

#ClientAliveInterval 0

#ClientAliveCountMax3

去掉注释并改为

ClientAliveInterval 60

ClientAliveCountMax 3

ClientAliveInterval指定了服务器端向客户端请求消息 的时间间隔, 默认是0, 不发送.而ClientAliveInterval 60表示每分钟发送一次, 然后客户端响应, 这样就保持长连接了.ClientAliveCountMax, 使用默认值3即可.ClientAliveCountMax表示服务器发出请求后客户端没有响应的次数达到一定值, 就自动断开. 正常情况下, 客户端不会不响应。  最后一定要记得重启ssh服务,否则是不会生效的,重启代码如下:

[root@chen /]# systemctl restart sshd.service

[root@chen /]#

然后就OK啦。另外悄悄告诉你判断ssh服务有没有启动的命令是:

[root@chen /]# systemctl status sshd.service

?sshd.service - OpenSSH server daemon

Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)

Active: active (running) since Thu 2017-09-21 16:57:34 CST; 2min 35s ago

Docs: man:sshd(8)

man:sshd_config(5)

Main PID: 9640 (sshd)

CGroup: /system.slice/sshd.service

忖9640 /usr/sbin/sshd -D

看到running就是启动了,如果没有启动的话输入

systemctl start sshd.service

就启动完成了。设置开机自启动的话则输入:

systemctl enable sshd.service

本文由 CentOS中文站 - 专注Linux技术 作者:centos 发表,其版权均为 CentOS中文站 - 专注Linux技术 所有,文章内容系作者个人观点,不代表 CentOS中文站 - 专注Linux技术 对观点赞同或支持。如需转载,请注明文章来源。