解决 SSH 报错 kex_exchange_identification: read: Connection reset by peer 修复
排查 SSH 握手初期抛出 kex_exchange_identification: read: Connection reset by peer 故障,分析 hosts.deny、并发连接过载与防火墙阻断。
问题现象
使用 SSH 登录远程 Linux 主机时,客户端在打印版本握手阶段瞬间报错断连:
$ ssh root@203.0.113.50
kex_exchange_identification: read: Connection reset by peer
Connection reset by 203.0.113.50 port 22
执行 ssh -vvv root@203.0.113.50 查看详细调试输出:
debug1: Local version string SSH-2.0-OpenSSH_9.0
kex_exchange_identification: read: Connection reset by peer
客户端甚至还没来得及接收到服务端的 SSH 版本字符串(Banner),TCP 连接就被对端直接重置(RST)。
触发根因
kex_exchange_identification 代表 SSH 密钥交换(Key Exchange)前的协议标识交换阶段:
- TCP Wrappers 或 hosts.deny 拦截:服务端配置了
/etc/hosts.deny包含了sshd: ALL,且/etc/hosts.allow未包含客户端来源 IP,sshd 建立 TCP 握手后由底层库强制断开; - 并发未认证连接超过
MaxStartups上限:在大规模自动化集群中,大量并发 SSH 会话瞬间涌入,触及了服务端的MaxStartups阈值,sshd 随机丢弃新连接; - 安全软件或防火墙拦截(Fail2ban / CSF / 云 WAF):短时间内高频失败连接导致客户端公网 IP 被源站防火墙动态封锁;
- 服务端口并非真正的 SSH 服务:端口转发错误,连接到了普通的 HTTP 服务或测试服务。
解决步骤
1. 检查服务端 /etc/hosts.allow 与 /etc/hosts.deny
登录服务端排查 TCP Wrappers 策略:
cat /etc/hosts.deny
若包含 sshd: ALL,在 /etc/hosts.allow 中加入你的白名单网段:
sshd: 198.51.100.0/24
2. 检查并调大 sshd 的 MaxStartups 高并发连接参数
编辑服务端 /etc/ssh/sshd_config:
# 格式为 start:rate:full
# 允许同时处理多达 100 个未完成握手的连接,达到 100 后开始以 30% 概率拒绝,最大 200
MaxStartups 100:30:200
重启 SSH 服务:
sudo systemctl restart sshd
3. 检查 fail2ban 与本地 iptables 拦截
查看 fail2ban 的 sshd 封锁清单:
sudo fail2ban-client status sshd
若客户端 IP 处于 Banned IP list,执行手动解封:
sudo fail2ban-client set sshd unbanip 198.51.100.22
4. 验证端口服务真实性
使用 nc 直接读取 22 端口返回的明文 Banner:
nc -v -w 3 203.0.113.50 22
正常的返回应当形如 SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.1。若返回 HTTP 报文或乱码,说明端口映射错误。
验证确认
从客户端重新发起 SSH 连接:
ssh -v root@203.0.113.50
客户端成功接收到远端 SSH-2.0 Banner 并顺利协商密钥,握手成功完成。


