Understanding the Error
The kex_exchange_identification error indicates that the initial handshake between the SSH client and server failed before authentication could begin. The "Key Exchange" (kex) phase is where both parties agree on encryption algorithms and exchange keys. If this step fails, the connection is dropped immediately. Common causes include an unreachable host, a misconfigured SSH daemon (sshd), or intermediate network devices like firewalls or intrusion prevention systems blocking the traffic.
Step 1: Verify Basic Connectivity
Before diving into SSH-specific configurations, ensure that the underlying network path is functional. A simple ping test can confirm if the host is reachable, but it does not guarantee that port 22 is open.
- Ping the target host: Use
ping <hostname_or_ip>to check for basic IP connectivity. If this fails, the issue is likely network-related rather than SSH-specific. - Test Port Accessibility: Use
telnet <hostname_or_ip> 22ornc -zv <hostname_or_ip> 22to verify that the SSH port is open and accepting connections. If the connection times out, a firewall is likely blocking access.
If port 22 is closed, you must contact your network administrator or adjust firewall rules. For local testing, you might temporarily disable the firewall using sudo ufw disable (on Ubuntu/Debian) or sudo systemctl stop firewalld (on CentOS/RHEL) to isolate the issue.
Step 2: Inspect Server-Side Logs
When the client reports a kex failure, the server usually has more detailed information about why the connection was rejected. Check the SSH daemon logs on the remote machine.
- Locate the log file: On most Linux distributions, SSH logs are found in
/var/log/auth.log(Debian/Ubuntu) or/var/log/secure(RHEL/CentOS). - Monitor live logs: Run
sudo tail -f /var/log/auth.logwhile attempting to connect from the client. Look for entries related tosshd. - Analyze the output: You may see messages like "Connection closed by authenticating user" or "Protocol mismatch." These clues help determine if the server is rejecting the client's offered ciphers or if the service is crashing upon connection.
If the server logs show no activity during your connection attempt, the packet is being dropped somewhere in the network stack, pointing back to firewall or routing issues.
Step 3: Adjust Client Cipher Preferences
One of the most common causes of kex errors is a mismatch in supported cryptographic algorithms. Older clients may try to use deprecated ciphers that the server has disabled, or vice versa. You can force the client to use specific, compatible protocols.
Edit or create the client configuration file at ~/.ssh/config or pass options directly via the command line. To specify older, widely compatible protocols:
ssh -o KexAlgorithms=+diffie-hellman-group14-sha256 user@host
You can also restrict ciphers if necessary:
ssh -o Ciphers=aes256-ctr user@host
Note: While some users suggest bypassing Deep Packet Inspection (DPI) by changing ciphers, this is technically inaccurate. DPI operates at the network layer and identifies traffic patterns regardless of the application-layer cipher suite. Changing ciphers only helps if the server explicitly rejects the default algorithm offer. For genuine DPI evasion, tunneling SSH over HTTPS (port 443) or using tools like stunnel is required, as discussed in Pass pw for remote user in bash line when ssh from linux to windows, which covers advanced SSH scripting contexts.
Step 4: Check for Corrupted Configuration Files
Corrupted files on either the client or server side can cause negotiation failures. On the server, ensure that /etc/ssh/sshd_config is valid. You can test the configuration syntax without restarting the service by running:
sudo sshd -t
If this command returns an error, fix the syntax issues in sshd_config and restart the SSH service using sudo systemctl restart sshd. Similarly, check for corrupted known_hosts files on the client side by renaming ~/.ssh/known_hosts and retrying the connection.
Step 5: Restart the SSH Service
After making any changes to configuration files, always restart the SSH daemon to apply them. Use the appropriate command for your distribution:
- Systemd-based systems:
sudo systemctl restart sshd - Init-based systems:
sudo service ssh restart
Restarting ensures that any updated cipher lists or protocol versions are loaded into memory. If the problem persists after a restart, consider reinstalling the OpenSSH package to restore default configurations, keeping in mind that manual key management steps might be needed afterward, as outlined in Fixing "Permission denied» with OpenSSH: Manually adding authorized_keys.
Frequently Asked Questions
Why does my SSH connection work locally but fail remotely?
This usually indicates a firewall rule or network policy restricting external access to port 22. Ensure that your router or cloud security group allows inbound TCP traffic on port 22 from the specific source IP addresses you are connecting from.
Can I ignore the kex_exchange_identification error by skipping verification?
No, ignoring verification flags like -o StrictHostKeyChecking=no will not resolve a kex error because the failure happens before host key verification. This flag only affects the prompt asking whether to add the host to known_hosts, which occurs after a successful connection.
What should I do if the server logs show "No matching key exchange method found"?
This means the client and server do not share any common key exchange algorithms. Update your SSH client to a newer version that supports modern algorithms, or modify the server's sshd_config to enable older methods like diffie-hellman-group1-sha1 if security policies allow.
Does changing the SSH port solve kex errors?
Changing the port number does not fix cryptographic negotiation failures. However, it can help avoid interference from ISPs or networks that block standard port 22. If you change the port, remember to update your firewall rules and use the -p flag in your SSH command.
How do I troubleshoot kex errors when using SSH jump hosts?
Jump hosts introduce additional network hops, each with its own potential points of failure. Test connectivity to each hop individually using nc -zv. Also, ensure that the ProxyJump directive in your config file is correctly formatted and that all intermediate servers have valid SSH configurations.