SSH kex_exchange_identification: Connection closed
This error comes after the connection opened and before any login. Something accepted the TCP connection, then the SSH handshake did not finish: the two sides never completed the exchange of identification and keys. Nothing got as far as a host key warning, a key offer or a password prompt, so those are not the problem yet.
This page describes standard OpenSSH behaviour. The commands are the usual ones, and wording differs between versions, distributions and platforms. We did not run them against a particular network or server for this page.
What the message means
kex_exchange_identification: Connection closed by remote host
The same moment is also reported as
kex_exchange_identification: read: Connection reset by peer and,
in older OpenSSH versions, with the older prefix
ssh_exchange_identification. A shorter line such as
Connection closed by 203.0.113.10 port 22 can appear in newer
versions, but it also appears at other points, including after login attempts.
It points to this page only when a
kex_exchange_identification line comes with it. On its own, after
a session or a login, it is the ordinary close covered in
SSH "Connection closed by remote host" and "Broken pipe".
At the start of an SSH connection each side sends one line saying that it speaks SSH and which version, and then they agree on keys. Closed or reset means the other end, or something in the path that answers for it, ended the connection during that step. It tells you the connection was made. It does not tell you what ended it.
That tells it apart from the other early errors.
Could not resolve hostname
means ssh never had an address.
A timeout or a refused connection
means no TCP connection was made. Permission denied, covered in
SSH "Permission denied (publickey)", means the handshake finished and login failed, and a
changed host key warning also
needs a finished key exchange. If instead both sides answered and then
reported Unable to negotiate or
no matching ... found, they had no algorithm in common, which is
a different problem: see
SSH "Unable to negotiate". This
page is only about a handshake that ended early.
1. Confirm it is this error and nothing earlier
Run the command again with -v:
ssh -v user@server.example.com
The log shows the address and port ssh is using, usually followed by a line saying the connection was established, and then the error. If it stops before the connection is established, you have a resolve, timeout or refused problem and the guides above are the place to go. The ssh -v output reader checks a pasted log for well-known messages, including this one, and links the matching guide. It runs in your browser and sends nothing.
Also check that the name, address and port are the ones you meant. A
Host entry in your SSH config can change them, and can add a
proxy, without any of it showing in the command you typed:
ssh -G myserver | grep -i -E '^(hostname|port|proxycommand|proxyjump) '
The ssh -G output reader shows the same values, and any proxy, from a pasted copy of the full output. It runs in your browser and sends nothing.
2. See what is actually speaking on that port
Any program can accept a TCP connection on a port. If the one listening on the port you used is not an SSH server, or is a different service you did not expect, it can close the connection or answer with something that is not an SSH identification. A device or router can also have its own management service on port 22 or on the port your forward points to.
A plain TCP client shows what comes back without ssh in the way. With netcat (flags differ a little between versions):
nc -w 5 server.example.com 22
An SSH server sends its identification line first, which starts with
SSH-2.0- followed by the software name. Press Ctrl-C to leave if
it waits. Three results are common:
-
An
SSH-2.0-line appears. Something there speaks SSH. Go on to the next steps. - Something else appears, such as text or an HTTP reply. Then the address and port lead to a different service. Check the port number, the forward on the router and the address the name points to.
- Nothing appears and the connection closes. Either the service closes at once, or something in the path does. Continue with the next step.
If nc waits a long time and ssh reports
Connection timed out during banner exchange, that is a related
stall at the same step: TCP opened, but no identification arrived in time. It
is not the same as an unreachable host, so the dropped-packet checks in the
timeout guide are not the
first place to look. The causes below fit it too, and so does a server that is
very busy or a link that is dropping the traffic once the connection is open.
3. Look for something in the path that closes the handshake
A connection can be accepted by a device in front of the real server. Some proxies, firewalls, load balancers and security appliances complete the TCP connection themselves and then close it if they do not allow the traffic. Signs that this is the case:
- It works from another network and not from this one. Try the same command from a phone hotspot. If it works there, something on your side of the path closes it. See SSH blocked on a work network.
-
Your config sends the connection through a proxy. A
ProxyJumporProxyCommandin thessh -Goutput above means the close can come from the jump host or the proxy, not from the final machine. Try the first hop on its own. - An allow-list or ban covers your address. Some set-ups accept the connection and then close it for addresses that are not on a list, or that were blocked after repeated failed logins. A change of address on your side, or a new address for the server, can start this.
- A change was made on the path recently. A new firewall rule, a changed forward, or a service moved to another machine are the usual suspects.
These are things to rule in or out, not a list of what is wrong. The error does not say which one closed the connection.
4. Check whether sshd accepted the connection and dropped it
If the path is clear and an SSH server answers, the server itself can still close a new connection. You need a console, someone on site or a session you already have open for this.
-
Test from the device itself.
ssh -v user@127.0.0.1on the machine takes the network out. If it also closes during the handshake, the cause is sshd or the machine. If it gets to a login, the cause is between that machine and you. -
Read the sshd log. On many systems
sudo journalctl -u sshshows it, and the service issshdon some distributions. Look at the lines from the moment of your attempt. A refusal logged there usually says why. -
Too many connections at once. sshd has a setting,
MaxStartups, that limits how many connections can wait to log in, and it can drop new ones beyond that. A burst of scripts, or automated traffic on a public port, can fill it.sudo sshd -Tprints the value in use. -
A bad configuration or a stressed machine.
sudo sshd -tchecks the configuration file. A machine that is out of memory or process slots can also fail to start a new session.
If this started right after you changed a firewall or sshd, see locked out of SSH after a firewall change for what gets you back in and how to make the next change safely.
If the machine is a RescueShell device
A path that starts from the device can help when the internet cannot reach it. The agent on the device keeps an outbound HTTPS connection to our relay, so a public SSH port, a forward, or a proxy and filter on the way in are not part of the path to a shell. That needs the agent installed and running first.
It does not repair a service that closes a local handshake. The session still ends at the device's own SSH server, so an sshd that accepts and then drops new connections, a limit that is full, or a machine too stressed to start a session affects it as well. The lockout guide lists which lockouts a second path survives. If a shell does not open once the agent is installed, see the shell does not open.
Try it on one device
The first device is free to try with no card, for 30 days from when it connects, not from when you sign up. Start a trial.