解决办法:需要在Server端和Client端互相配置心跳参数,即发空包

- Server - ssh配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# vim /etc/ssh/sshd_config

....

98 #PermitUserEnvironment no
99 #Compression delayed
100 ClientAliveInterval 60
101 ClientAliveCountMax 3
102 UseDNS no
103 #PidFile /var/run/sshd.pid
104 #MaxStartups 10:30:100
105 #PermitTunnel no
106 #ChrootDirectory none
107 #VersionAddendum none
....

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

- 重启sshd服务

1
systemctl restart sshd 或 service sshd restart

- Client - ssh配置 【任选其一】

SSH Client 读取参数顺序 :命令行参数 ===> 用户配置文件 ===> 系统配置文件
● 1. SSH 命令行参数

1
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 root@192.168.1.1

● 2.用户配置文件 ~/.ssh/config

1
2
3
Host *
ServerAliveInterval 60
ServerAliveCountMax 3

● 3.系统配置/etc/ssh/sshd_config 追加以下内容

1
2
3
Host *
ServerAliveInterval 60
ServerAliveCountMax 3

- SSH Debug

1
2
3
4
5
6
7
8
9
10
11
12
☁  ~  ssh -vvv root@192.168.1.1 
OpenSSH_7.9p1 Deepin-deepin1 UOS_Desktop_104*, OpenSSL 1.1.1d 10 Sep 2019
debug1: Reading configuration data /home/mist/.ssh/config
debug1: /home/mist/.ssh/config line 13: Applying options for lt-zss
debug1: /home/mist/.ssh/config line 192: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolve_canonicalize: hostname 192.168.1.1 is address
debug2: ssh_connect_direct
debug1: Connecting to 192.168.1.1 [192.168.1.1] port 22.
debug1: Connection established.
.....