This error occurs when the SSH server abruptly closes the connection during the protocol handshake, before authentication is completed. It typically indicates server-side configuration issues, resource constraints, or firewall restrictions rather than a networking connectivity problem.
When SSH displays "Connection closed by remote host," the remote server has established the TCP connection but then terminated it during the SSH protocol exchange (before username/password authentication even begins). This differs from "Connection refused" or "Connection timed out"—those errors occur at the network layer, while this error happens after successful TCP handshake but during SSH negotiation. The server is actively rejecting the connection, often due to configuration restrictions, resource limits, or security policies on the server side.
Before troubleshooting SSH specifically, confirm the server is reachable:
ping -c 4 remote.example.com
telnet remote.example.com 22If ping works but telnet fails to even reach port 22, the problem is likely a firewall blocking SSH entirely. If telnet connects and shows SSH banner, SSH daemon is running and the issue is within SSH negotiation.
You can also try from a different client machine (different IP):
ssh -v [email protected]The -v flag shows verbose output of where exactly the connection closes.
The most common cause is /etc/hosts.deny blocking SSH. If you have direct server access (console, panel), check:
sudo cat /etc/hosts.deny
sudo cat /etc/hosts.allowIf /etc/hosts.deny contains:
sshd: ALLThis blocks all SSH connections. Remove this line and restart SSH:
sudo systemctl restart sshdIf your IP is specifically blocked, remove that line:
# Remove the blocking line, then:
sudo systemctl restart sshdTo allow SSH from all IPs, add to /etc/hosts.allow:
sshd: ALLIf multiple SSH connections are failing simultaneously (or you're attempting rapid reconnects), the server's MaxStartups limit may be exceeded. Check the current setting:
sudo grep MaxStartups /etc/ssh/sshd_configDefault is usually 10:100. This means max 10 unauthenticated connections, dropping all new ones at 100% capacity. To increase:
sudo nano /etc/ssh/sshd_configChange or add:
MaxStartups 200:30:400This allows 200 concurrent connections before any are dropped. Save and restart:
sudo systemctl restart sshdFirewall rules might drop SSH packets after the TCP connection. Check your active firewall:
# For iptables
sudo iptables -L -n | grep ssh
# For ufw
sudo ufw status
# For firewalld
sudo firewall-cmd --list-allEnsure SSH (port 22) is not in any DROP or REJECT rules. For ufw:
sudo ufw allow 22/tcp
sudo systemctl restart ufwFor iptables, insert a rule allowing SSH:
sudo iptables -I INPUT -p tcp --dport 22 -j ACCEPT
sudo systemctl restart iptables # or save rulesFor firewalld:
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reloadCorrupted SSH host keys can cause the server to reject all connections. Check if keys exist and are valid:
sudo ls -la /etc/ssh/ssh_host_*If files are missing or corrupted, regenerate them:
sudo ssh-keygen -AThen restart the SSH daemon:
sudo systemctl restart sshdThis regenerates all standard host key types (RSA, ECDSA, ED25519). The server will now show a new fingerprint to clients.
If the server is out of memory or disk space, SSH daemon may fail to handle new connections. Check:
df -h / # Disk space
free -h # Memory
top -n 1 # Process loadIf memory is exhausted, kill non-critical processes or reboot. If disk is full, remove old logs/files.
Then restart the SSH daemon:
sudo systemctl restart sshdIf sshd is running but misbehaving, try force-restarting:
sudo systemctl kill sshd
sudo systemctl start sshdIf your server uses SELinux or AppArmor, policies might block SSH. Check status:
# SELinux
getenforce
# AppArmor
sudo aa-status | grep sshdIf SELinux is in Enforcing mode, temporarily set to Permissive to test:
sudo setenforce 0
# Try SSH again
sudo setenforce 1If AppArmor is blocking sshd, check logs:
sudo tail -f /var/log/syslog | grep apparmorFor permanent fixes, consult your distribution's SELinux/AppArmor documentation, or disable enforcement (not recommended in production).
When troubleshooting, run SSH with verbose output to see exactly where the connection closes:
ssh -vvv [email protected]Look for the last line before "Connection closed by remote host"—it will show what phase of negotiation failed (key exchange, algorithm negotiation, etc.).
Common verbose output clues:
- "Received disconnect message (code 3)" = server actively killed the connection
- "Disconnect message from ... [lang] ..." = message from server explaining why
- No further output = server silently closed connection (suggests firewall/DOS protection)
Advanced troubleshooting:
Library Update Issues: After glibc or OpenSSL updates, the SSH daemon might not auto-reload the new libraries. A simple systemctl restart sshd forces the daemon to use the updated libraries. If that fails, try systemctl kill sshd && systemctl start sshd.
Network Path Issues: Rarely, intermediate routers or NAT devices may be dropping SSH packets after the 3-way handshake (especially if SSH negotiation is slow). This can happen on satellite links or very slow networks. Test with ssh -o ConnectTimeout=30 to allow more time for negotiation.
Custom SSH Ports: If SSH runs on a non-standard port (not 22), ensure your connection attempt specifies the correct port:
ssh -p 2222 [email protected]Brute Force Protection: Some servers use tools like fail2ban or DDoS protection that may temporarily ban IPs after multiple failed SSH attempts. Check if your IP is banned:
sudo fail2ban-client status sshdIf banned, wait for the ban to expire or ask an admin to manually unban the IP.
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
How to fix SSH man-in-the-middle attack warning in SSH
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVATE KEY FILE! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
How to fix "WARNING: UNPROTECTED PRIVATE KEY FILE!" in SSH
sign_and_send_pubkey: no mutual signature supported
How to fix "sign_and_send_pubkey: no mutual signature supported" in SSH
Bad owner or permissions on /home/user/.ssh/known_hosts
How to fix "Bad owner or permissions on known_hosts" in SSH
It is required that your private key files are NOT accessible by others.
How to fix "private key files are NOT accessible by others" in SSH