Fix SSH Exchange Identification Connection Closed by Remote Host
In this article
If you see "ssh exchange identification: Connection closed by remote host," the SSH server closed the connection before finishing the key exchange, usually due to a server-side limit, a firewall rule, or a client configuration problem. The fix is to check the server's SSH logs, adjust client connection settings, and verify network and firewall rules—detailed steps below.
What Causes This Error
This error appears when the SSH client connects to port 22 but the server terminates the session during the initial handshake. Common causes include:
- Server connection limits: The server has reached its maximum number of unauthenticated connections (MaxStartups) or total connections (MaxSessions).
- Firewall or IDS: A firewall, load balancer, or intrusion detection system is dropping or resetting the connection.
- TCP wrappers or hosts.deny: The server's TCP wrappers configuration is rejecting the client's IP.
- SSH configuration errors: The server's sshd_config has a syntax error or an invalid option, causing sshd to fail to start or to close connections.
- Client-side issues: The client is sending a protocol version string that the server doesn't understand, or the client's SSH version is too old.
- Network issues: MTU problems, packet fragmentation, or a proxy that interferes with the SSH handshake.
How SSH Connection Establishment Works
SSH uses a three-phase handshake. First, the client opens a TCP connection to port 22. Then, both sides exchange identification strings (e.g., "SSH-2.0-OpenSSH_8.9"). Finally, they perform the key exchange and negotiate encryption. The error "exchange identification" means the server closed the connection during the identification phase, before any encryption is established. This is different from a timeout (where the client gives up waiting) or an authentication failure (which happens after encryption starts).
Step-by-Step Fixes
- Check server logs: On the server, look at /var/log/auth.log (Debian/Ubuntu) or /var/log/secure (RHEL/CentOS). Search for lines mentioning "sshd" and the client's IP. Common log entries include "maximum number of unauthenticated connections" or "Connection closed by authenticating user."
- Verify sshd is running: Run
systemctl status sshd(orservice ssh status) on the server. If it's not running, start it withsystemctl start sshd. Also check for syntax errors in /etc/ssh/sshd_config withsshd -t. - Increase connection limits: If logs show MaxStartups exceeded, edit /etc/ssh/sshd_config and increase the value, e.g.,
MaxStartups 30:50:100. Then restart sshd. - Check firewall rules: On the server, ensure port 22 is open. Use
sudo iptables -L -norsudo ufw status. On the client, check if a local firewall or VPN is blocking outbound SSH. Temporarily disable them to test. - Test with verbose output: Run
ssh -vvv user@hostto see detailed handshake messages. Look for lines like "Connection closed by remote host" and any preceding error. - Try a different SSH client: If you're using a GUI client, try the command line. Or try connecting from a different machine to isolate the issue.
- Check TCP wrappers: If /etc/hosts.deny exists, look for lines like
sshd: ALL. Add an allow rule in /etc/hosts.allow for your IP. - Restart sshd: After any config change, restart the service with
sudo systemctl restart sshd.
Common Misconceptions
Many users think this error means the server is down or the SSH key is wrong. That's rarely the case. The error occurs before any authentication, so it's not about passwords or keys. Also, some think that increasing MaxStartups is the only fix, but often it's a firewall or a misconfigured client that's the culprit. Another misconception is that this error is the same as "Connection timed out"—they're different. A timeout means the client couldn't reach the server at all; this error means the server responded but then closed the connection.
Prevention and Best Practices
- Monitor SSH logs: Regularly check /var/log/auth.log for unusual connection attempts or errors.
- Use a connection multiplexer: Tools like
ControlMasterin OpenSSH reuse a single TCP connection, reducing the number of new handshakes. - Set appropriate MaxStartups: For a busy server, increase the default (10:30:100) to something like 30:50:100 to handle spikes.
- Use fail2ban: This tool automatically bans IPs that show malicious behavior, but it can also cause this error if it misidentifies a legitimate client. Whitelist trusted IPs.
- Keep SSH updated: Use the latest version of OpenSSH to avoid protocol bugs that might cause premature disconnects.
When to Use Alternative Solutions
If you cannot fix the issue by adjusting the server, consider these alternatives:
- Change the SSH port: If a firewall is blocking port 22, try a different port like 2222. Edit /etc/ssh/sshd_config and change
Port 22toPort 2222, then restart sshd and connect withssh -p 2222. - Use a VPN or SSH over HTTPS: If the network blocks SSH entirely, tunnel SSH through a VPN or use a tool like sslh to multiplex SSH and HTTPS on port 443.
- Use a different protocol: If SSH is not essential, consider using a web-based terminal like a serial console provided by your cloud provider.
Frequently Asked Questions
Why does this happen only sometimes?
Intermittent occurrences often point to connection limits being reached during peak times, or a firewall that rate-limits connections. Check the server's MaxStartups setting and the firewall logs for dropped packets.
Does this error mean my server is hacked?
Not necessarily. It's usually a configuration issue or a temporary network problem. However, if you see this error alongside many failed login attempts, it could indicate a brute-force attack. Check the auth logs for failed password attempts.
Can I fix this by changing my SSH client settings?
Yes, sometimes. Try reducing the connection timeout with ConnectTimeout 30 or using a different cipher with -c aes128-ctr. But if the server is closing the connection, the client settings won't help unless the issue is a protocol mismatch.
How do I check if my server's sshd is working correctly?
Run sshd -t to test the configuration. Then run systemctl status sshd to see if it's active. You can also try connecting locally with ssh localhost on the server to see if the handshake works.
What is MaxStartups and how do I set it?
MaxStartups is an sshd_config parameter that limits concurrent unauthenticated connections. The format is start:rate:full. For example, 10:30:100 means allow 10 connections, then drop 30% of new ones until reaching 100, then drop all. Increase the first number to allow more simultaneous connections.
Is this error the same as "Connection reset by peer"?
No. "Connection reset by peer" means the server sent a TCP RST packet, which often indicates a firewall or a server crash. "Connection closed by remote host" means the server sent a FIN packet, which is a normal close but after a protocol error. Both require checking logs, but the causes can differ.