The Exchange identification step in SSH is failing because the client and server can’t agree on which cryptographic algorithms (like key exchange methods, ciphers, and MACs) they both support. This breakdown happens before any actual authentication or data transfer, leaving you with a Connection closed error.
Here’s what’s likely happening and how to fix it:
1. Mismatched KexAlgorithms:
The client and server don’t have any common key exchange algorithms. This is the most frequent culprit.
- Diagnosis: On the client, run
ssh -vvv user@host. Look for a line likedebug2: kex_parse_kexinit: no common kex algorithm. - Fix: Edit the server’s
sshd_config(usually/etc/ssh/sshd_config). Find or add theKexAlgorithmsline. Ensure it includes algorithms known to be supported by both your client and server. A good starting point for compatibility might be:
Restart the SSH service:KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1sudo systemctl restart sshd. - Why it works: This explicitly tells the server which key exchange methods it can use, forcing it to negotiate one from this list that the client also supports.
2. Mismatched Ciphers:
Similar to KexAlgorithms, the client and server don’t share any common encryption algorithms for the actual data stream.
- Diagnosis:
ssh -vvv user@host. Look fordebug2: kex_parse_kexinit: no common cipher. - Fix: In
sshd_config, edit or add theCiphersline. Include widely supported ciphers:
RestartCiphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbcsshd. - Why it works: This ensures the server offers encryption algorithms that the client understands, allowing the connection to proceed securely.
3. Mismatched MACs (Message Authentication Codes):
The client and server can’t agree on algorithms to verify the integrity of data packets.
- Diagnosis:
ssh -vvv user@host. Look fordebug2: kex_parse_kexinit: no common mac. - Fix: In
sshd_config, edit or add theMACsline:
RestartMACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,umac-128@openssh.comsshd. - Why it works: This provides a common method for both ends to ensure data hasn’t been tampered with during transit.
4. Outdated SSH Client or Server: An older version of SSH on either end might not support newer, more secure algorithms that the other end is trying to use.
- Diagnosis: Check the SSH versions:
ssh -Von the client, andsshd -Vorssh -Von the server. Compare these to current stable releases. - Fix: Update your SSH client and server software to the latest stable versions. For example, on Debian/Ubuntu:
sudo apt update && sudo apt upgrade openssh-client openssh-server. On CentOS/RHEL:sudo yum update openssh-clients openssh-server. - Why it works: Newer versions inherently support a broader and more secure range of cryptographic primitives, increasing the chances of a successful negotiation.
5. Firewall or Network Interruption Blocking SSH Traffic: Less common for this specific error, but a firewall might be interfering with the initial handshake packets, causing them to be dropped and the connection to close prematurely.
- Diagnosis: Use
tcpdumpon the client or server to capture traffic on port 22.sudo tcpdump -i eth0 port 22 -nn(replaceeth0with your interface). Look for SYN packets going out but no SYN-ACK coming back, or unexpected RST packets. - Fix: Ensure port 22 (or your custom SSH port) is open in any firewalls (e.g.,
sudo ufw allow sshorsudo firewall-cmd --permanent --add-service=ssh && sudo firewall-cmd --reload). Check cloud provider security groups or network ACLs. - Why it works: This guarantees that the initial SSH negotiation packets can actually reach their destination and receive responses.
6. UsePrivilegeSeparation Setting:
In rare cases, a misconfiguration related to privilege separation in sshd_config can cause issues during the early stages of the connection.
- Diagnosis: Check
sshd_configforUsePrivilegeSeparation. - Fix: Try setting
UsePrivilegeSeparation yesinsshd_config. If it was alreadyyesand causing issues, tryUsePrivilegeSeparation notemporarily to diagnose, butyesis the recommended secure setting. Restartsshd. - Why it works: Privilege separation drops root privileges early in the process. If the mechanism for this is broken or incompatible with the system setup, it can halt the connection before it even starts negotiating.
7. Client-Side ssh_config Overrides:
Your local ~/.ssh/config file might have settings that conflict with the server’s capabilities.
- Diagnosis: Run
ssh -vvv -F /dev/null user@hostto bypass your local config. If this works, your~/.ssh/configis the problem. - Fix: Examine your
~/.ssh/configfor explicitKexAlgorithms,Ciphers, orMACsdirectives that might be too restrictive or outdated. Remove or adjust them to allow negotiation. - Why it works: This removes any client-specific, potentially problematic, algorithm preferences, allowing the client to discover what the server supports.
Once you’ve resolved the Exchange identification error, you’ll likely encounter the next step in the SSH process, which is usually authentication (Authentication that can take another 1000 words).