Skip to content
MtrXTools

kex_exchange_identification: Connection closed by remote host

The SSH server closed the connection before the handshake

Seen in OpenSSH client, git over SSH (GitHub, GitLab)

The TCP connection opened, but the server (or something in between) closed it before sending its SSH version banner.

Common causes

  • A firewall, proxy or VPN blocks or intercepts SSH on port 22.
  • sshd on the server is refusing new connections: MaxStartups reached, fail2ban, or hosts.deny rules.
  • Something other than sshd is listening on the port, or sshd is crashing on start-up.

How to fix it

  1. For GitHub, try SSH over port 443: ssh -T -p 443 git@ssh.github.com.
  2. Run ssh -v to see where it stops, and test from another network to rule out a local firewall.
  3. On your own server, check the sshd log (journalctl -u ssh or -u sshd) and any fail2ban bans.

Got a different message? Paste it into the error explainer.

Related errors

All 31 Dev tools & SSH errors