解决 SSH 报错 kex_exchange_identification connection closed 修复
排查 SSH 建立连接时抛出 kex_exchange_identification: read: Connection reset by peer 或 connection closed,分析 hosts.deny 与 MaxStartups。
问题现象
SSH 客户端刚开始连接,还未要求输入密码或公钥即闪退:
kex_exchange_identification: read: Connection reset by peer
Connection reset by 203.0.113.10 port 22
触发根因
- Fail2ban 或防火墙因误触暴力破解策略将客户端 IP 封禁;
- TCP Wrappers 限制:客户端 IP 被
/etc/hosts.deny规则匹配阻断; - 并发连接数超出
MaxStartups上限:突发脚本批量登录导致未认证连接被直接丢弃。
解决步骤
检查并解除 fail2ban 封禁
在控制台查看当前受限 IP 并解封:
sudo fail2ban-client status sshd
sudo fail2ban-client set sshd unbanip 203.0.113.50
提升 sshd 的 MaxStartups 并发容限
在 /etc/ssh/sshd_config 中扩充:
MaxStartups 100:30:200
重载服务:
sudo systemctl reload sshd
验证确认
再次通过客户端登录测试:
握手协议协商顺利进行,正常进入用户终端。


