The server closed the connection during SSH authentication or data transfer. This typically happens due to network issues, SSH configuration problems, or server-side restrictions like rate limiting or IP blocking.
This error occurs when a Git remote server abruptly terminates the SSH connection during the key exchange phase or while transferring data. The message "Connection closed by remote host" indicates the server actively closed the connection rather than a network timeout. Common variations include "fatal: The remote end hung up unexpectedly" during push/pull operations, or "ssh_exchange_identification: Connection closed by remote host" during the initial SSH handshake. The error typically appears when using SSH to communicate with remote Git repositories (GitHub, GitLab, Bitbucket, or self-hosted servers). It can occur during clone, fetch, push, or pull operations. The root cause usually falls into one of several categories: network connectivity issues, SSH authentication problems, server-side security restrictions (IP allowlists, rate limiting), or resource exhaustion on the remote server.
First, confirm the connection problem is SSH-related and not specific to Git:
# Test GitHub connection
ssh -T [email protected]
# Test GitLab connection
ssh -T [email protected]
# Test Bitbucket connection
ssh -T [email protected]If this fails with the same error, the issue is with SSH connectivity, not Git specifically.
Add keep-alive settings to your SSH config to maintain the connection:
# Edit your SSH config file
nano ~/.ssh/config
# Add these lines
Host *
ServerAliveInterval 60
ServerAliveCountMax 30This sends a keep-alive packet every 60 seconds for up to 30 rounds (30 minutes total), preventing idle connection timeouts.
For immediate testing without editing config:
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=30 -T [email protected]If your network blocks SSH port 22, try using SSH over HTTPS port 443:
# Test if port 443 works
ssh -T -p 443 [email protected]
# If successful, configure it permanently
nano ~/.ssh/config
# Add this section
Host github.com
Hostname ssh.github.com
Port 443
User gitThis works for GitHub and some other providers that offer SSH on port 443.
Ensure your SSH keys are properly configured and have correct permissions:
# Check SSH directory permissions (should be 700)
ls -la ~/.ssh
chmod 700 ~/.ssh
# Check private key permissions (should be 600)
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
# List your SSH keys
ls -la ~/.ssh/*.pub
# Display your public key to verify it matches what is on the server
cat ~/.ssh/id_rsa.pubVerify the public key is added to your account on GitHub/GitLab/Bitbucket.
If you have multiple network interfaces active (both WiFi and Ethernet), disconnect one:
# On Linux, list network interfaces
ip link show
# Temporarily disable WiFi if using Ethernet
sudo ip link set wlan0 down
# Or disable Ethernet if using WiFi
sudo ip link set eth0 downOn Windows/Mac, simply disable one network adapter in your network settings. SSH keys may be tied to specific MAC addresses, causing conflicts with multiple interfaces.
VPNs can interfere with SSH connections. Try temporarily disabling your VPN:
# Test connection without VPN
ssh -T [email protected]
# If it works, configure VPN split tunneling to exclude Git traffic
# Or switch to HTTPS for Git operations
git remote set-url origin https://github.com/username/repo.gitFor large repositories, increase the HTTP post buffer to prevent connection drops:
# Increase buffer to 500MB
git config --global http.postBuffer 524288000
# For slow connections, adjust timeout settings
git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999This helps when the error occurs during push/pull of large files.
If DNS changes caused host key mismatches, clear the problematic entry:
# Remove specific host from known_hosts
ssh-keygen -R github.com
ssh-keygen -R gitlab.com
# Or backup and clear entire known_hosts
mv ~/.ssh/known_hosts ~/.ssh/known_hosts.backup
touch ~/.ssh/known_hostsThe next connection will prompt you to accept the new host key.
As a workaround, use HTTPS instead of SSH for Git operations:
# Check current remote URL
git remote -v
# Change from SSH to HTTPS
git remote set-url origin https://github.com/username/repo.git
# For authentication, use a personal access token instead of password
git config --global credential.helper storeHTTPS is more firewall-friendly but requires token-based authentication.
Server-side troubleshooting (if you control the Git server):
Check server logs for connection rejection reasons:
# View SSH daemon logs
sudo tail -f /var/log/auth.log # Debian/Ubuntu
sudo tail -f /var/log/secure # RHEL/CentOSVerify sshd configuration allows connections:
sudo nano /etc/ssh/sshd_config
# Check MaxStartups, MaxAuthTries, AllowUsers, AllowGroups
sudo systemctl restart sshdMonitor server resources:
# Check memory and CPU
top
free -h
# Check network connections
ss -tuln | grep :22Rate limiting considerations:
GitHub and other providers implement rate limiting. If you are making many requests rapidly (especially in CI/CD), you may be temporarily blocked. Solutions:
- Add delays between Git operations
- Use repository caching in CI/CD pipelines
- Contact provider support for allowlist exceptions
Corporate network environments:
In enterprise networks, security appliances may perform deep packet inspection or terminate SSL/SSH connections. Contact your network administrator to:
- Allowlist Git provider IP ranges
- Configure proxy exceptions for Git traffic
- Enable SSH on port 443 through firewall rules
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git
fatal: missing blob object 'abc1234' in blobless clone
How to fix 'missing blob object in blobless clone' in Git