ssh "kex_exchange_identification: Connection closed by remote host": who closed it
Fix ssh "kex_exchange_identification: Connection closed by remote host": check what listens on the port, sshd's log, MaxStartups, PerSourcePenalties and blocklists.
by OhSudo ··
Checked on real software: OpenSSH · November 2026How we check
Not sure this is your cause?Find it step by step: Can't connect over SSH →
$ ssh admin@203.0.113.10kex_exchange_identification: Connection closed by remote hostConnection closed by 203.0.113.10 port 22The TCP connection worked, and then the other side closed it before saying anything. An SSH server starts by sending a short version line (SSH-2.0-OpenSSH_9.6…), and this never arrived. So the problem isn’t your key or your password. Either it isn’t an SSH server that answered, or the server chose to drop you right away.
A variant says the same thing more abruptly:
kex_exchange_identification: read: Connection reset by peer- Your sshconnects to port 22(works)TCP
- Server sideshould send SSH-2.0-OpenSSH…closes it(fails)
- Connection closedbefore any version line(breaks here)
TL;DR
# Does anything answer with an SSH banner?nc -w 5 203.0.113.10 22 # an SSH server prints SSH-2.0-…
# On the server: why sshd dropped the connectionsudo journalctl -u ssh -n 50 # "sshd" on RHEL-family systems1. Is it really sshd on that port?
nc -w 5 203.0.113.10 22A real SSH server answers at once with a line like SSH-2.0-OpenSSH_9.6p1 Ubuntu-3ubuntu13. If nothing comes and the connection closes, something else is on that port:
- A Docker port with nothing behind it.
-p 2222:22on a container where sshd isn’t running: Docker accepts the connection, can’t reach anything in the container, and resets it, which gives theread: Connection reset by peervariant. Checkdocker psand the container’s logs. - A load balancer or a proxy in front of the server with no healthy backend.
- A port forward on a router or in the cloud pointing to the wrong machine or port.
On the server, sudo ss -tlnp | grep ':22 ' shows which program listens.
2. sshd dropped you: MaxStartups
sshd limits connections that haven’t logged in yet. The default, MaxStartups 10:30:100, starts dropping new ones at random once 10 are waiting, and drops all of them at 100. On a server exposed to the internet, password-guessing bots fill those slots all day. The server’s log says so. OpenSSH 9.8 and later write a line per dropped connection:
drop connection #10 from [198.51.100.7]:51234 on [203.0.113.10]:22 MaxstartupsOlder versions, such as 9.6 on Ubuntu 24.04, write error: beginning MaxStartups throttling, and then drop connection #10 … past MaxStartups.
Run ssh again and it often works, as it’s random. To fix it for good, keep the bots away. Allow port 22 only from your addresses in the firewall, or use a VPN. You can also raise the limit in /etc/ssh/sshd_config:
MaxStartups 30:30:100Check the file with sudo sshd -t before restarting sshd, so a typo doesn’t lock you out.
3. sshd dropped you: penalties and blocklists
- OpenSSH 9.8 and later have
PerSourcePenalties: an address that fails to log in many times, or makes sshd crash, is refused for a while, right at the start, with this same message. Automation that keeps retrying with a wrong key can block itself, or everyone behind the same NAT. The log’sdrop connectionline then ends with the reason, such aspenalty: failed authentication, andPerSourcePenaltyExemptListinsshd_configexempts trusted addresses. - fail2ban and similar tools ban addresses after failed logins.
sudo fail2ban-client status sshdlists them, andsudo fail2ban-client set sshd unbanip 198.51.100.7removes one. DenyUsers,AllowUsersandMatchblocks don’t cause this, as they act after the version exchange.
4. sshd itself can’t work
If every connection fails this way, including from the server’s own console with ssh localhost, look at sshd:
sudo sshd -t # config errors, and unreadable host keyssudo systemctl status sshHost keys with the wrong permissions or owner, a full disk, or a broken config can make sshd close each connection. On a server you can’t reach any more, use the cloud provider’s serial console.
5. Something in between
VPNs, corporate proxies, and firewalls that inspect traffic sometimes reset SSH connections. Run ssh -vvv to see how far it gets. If Connection established is the last useful line, the drop happened before the server sent anything. Try another network, such as a phone’s hotspot, to tell your network from the server.
Shell, we have a problem!
Similar, but different: Connection refused means nothing listens on the port, and Connection timed out means no answer at all, usually a firewall. Too many authentication failures comes after the version exchange, when your keys are tried. Permission denied (publickey) means you reached sshd, and it rejected your key. The SSH Config Generator helps set up ports and keys per host.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.