问题
今天登陆服务器时候,ssh 后返回 ssh_exchange_identification: Connection closed by remote host 错误,重试了几次,会有一定概率失败。
分析
由于每次不不一定失败,首先想到的 ssh 连接数超过了限制。
首先打开目标服务器的 sshd 配置文件。
$ vim /etc/ssh/sshd_config
1
找到MaxStartup和 MaxSession配置
MaxSessions 10
MaxStartup 10:30:60
1
2
这两个配置含义:
MaxStartup
这个是限制处于联机页面的连接数,默认值10。联机页面就是当你登录ssh时,还没输入密码的页面。
三元组形式10:30:60
10:当连接数达到10时就开始拒绝连接,不过不是全部拒绝,我们继续往下看
30:当连接数到达10时,之后的连接有30的概率被拒绝掉
60:当连接数达到60时,之后的连接就全部拒绝了
一个数字的形式
我们可以直接 写个60,这样言简意赅,连接数达到60之前敞开玩,达到60后就不能玩了。
MaxSessions
每个连接可以并行开启多少个会话(session),默认值是10.
查看当前 sshd 连接数
$ netstat -tnpa | grep 'ESTABLISHED.*sshd'
tcp 0 52 172.17.19.234:22 49.233.92.166:43710 ESTABLISHED 30156/sshd: [accept
tcp 0 0 172.17.19.234:22 159.203.219.38:37027 ESTABLISHED 29896/sshd: root [p
tcp 0 1264 172.17.19.234:22 90.84.189.254:38404 ESTABLISHED 30145/sshd: [accept
tcp 0 0 172.17.19.234:22 111.229.122.177:38808 ESTABLISHED 30134/sshd: root [p
1
2
3
4
5
发现有 10 个左右连接。
解决方案
修改 MaxStartup 参数,将最小连接数设置为 50。保存后退出。
重启 sshd 服务:systemctl restart sshd 或者 service sshd reload。
使用 ssh 重新登录。
PS: 如果将最小连接数调整最小 50 后,通过查看 sshd 连接数命令后,还会继续增多,多半是服务器被攻击了,可适当调整 sshd 端口,和增加允许 IP。
- ssh_exchange_identification identification Connection exchange 解决方案ssh_exchange_identification identification connection ssh_exchange_identification quot ssh_exchange_identification host identification kex_exchange_identification identification connection kex_exchange_identification connection exchange finished服务器 socketexception connection finshell解决方案 elasticsearch connection解决方案 错误 connection解决方案connect refused connection available解决方案 方案