Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

SSH to GitHub connects, then the server closes or resets the connection

Errorkex_exchange_identification: Connection closed by remote host

Short answer

The TCP connection opened but something hung up before SSH finished its handshake. Either a network device between you and the server is cutting port 22 (firewall, VPN, proxy), or your own server's sshd is refusing new unauthenticated connections. Try GitHub on port 443 first; on your own server check MaxStartups.

  • Applies to OpenSSH 9.x and 10.x, Git 2.4x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run one verbose attempt and note how far it gets. Then run the same test on port 443. If 443 works and 22 does not, the fault is on the network path.

    ssh -vT git@github.com
    ssh -T -p 443 git@ssh.github.com
  2. 2Match what you see, then jump to its fix

    1. 1Port 22 fails after connecting while the port 443 test prints the greeting.A firewall, VPN or proxy cuts port 22Fix
    2. 2The failure only happens for hosts matched by a Host block that sets a proxy, or by a Host * block.A ProxyCommand or ProxyJump line sends the connection somewhere elseFix
    3. 3The failure is on a server you run, often when CI or many developers connect at once.Your own git server is dropping unauthenticated connectionsFix

What does the error look like?

  • kex_exchange_identification: Connection closed by remote host
  • Connection reset by 140.82.112.4 port 22
  • fatal: Could not read from remote repository.

Cause 1 of 3

A firewall, VPN or proxy cuts port 22

1Check

GitHub documents port 443 for exactly this case, because most firewalls allow it. Turning the VPN off for one test shows whether it is the culprit.

ssh -T -p 443 git@ssh.github.com

2Fix

Route github.com to ssh.github.com on port 443 in your ssh config.

# ~/.ssh/config
Host github.com
    Hostname ssh.github.com
    Port 443
    User git

3Verify

The normal command works without extra flags.

ssh -T git@github.com

Confirmed by docs.github.com

Did this fix it?

Cause 2 of 3

A ProxyCommand or ProxyJump line sends the connection somewhere else

1Check

ssh uses the first value it finds for each setting, and a ProxyCommand or ProxyJump in a general block can apply to GitHub too.

grep -n -i -E "^host|proxycommand|proxyjump" ~/.ssh/config /etc/ssh/ssh_config

2Fix

Turn the proxy off for the GitHub block. The value none disables it, and host-specific blocks must come before general ones.

Host github.com
    ProxyCommand none
    ProxyJump none

Host *
    # general defaults go last

3Verify

Connect again and read the verbose output.

ssh -vT git@github.com

Confirmed by man.openbsd.org

Did this fix it?

Cause 3 of 3

Your own git server is dropping unauthenticated connections

1Check

sshd drops new unauthenticated connections once MaxStartups is reached (default 10:30:100), and newer versions also refuse addresses that look like attacks (PerSourcePenalties) or cap per-source connections (PerSourceMaxStartups).

sudo grep -i -E "maxstartups|persource|logingracetime" /etc/ssh/sshd_config

2Fix

Raise MaxStartups to fit your real peak, set a per-source limit if you use one, then reload sshd.

# /etc/ssh/sshd_config
MaxStartups 30:30:100

3Verify

Repeat the failing operation from several clients at once and watch the server log.

ssh -vT git@your-git-server.example.com

Confirmed by man.openbsd.org

Did this fix it?

How do I stop it coming back?

  • Keep the port 443 block in ~/.ssh/config for networks that filter SSH.
  • Limit parallel git jobs in CI so they do not open dozens of connections at the same moment.
  • On your own servers, change sshd limits deliberately and keep them in version control.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.