SSH Slow or Connection Timeout
Resolve slow SSH connections, timeouts and frequent disconnections. Diagnosis and fix in minutes.
Symptoms
- SSH takes 10-30 seconds before showing login prompt
- Connection drops after a few minutes of inactivity
ssh: connect to host ... port 22: Connection timed out
1. SSH slow on startup (30s wait)
Cause: DNS reverse lookup
SSH tries to resolve reverse DNS of your IP. If DNS is slow or not configured, it waits for timeout.
Verify:
time ssh root@server hostname
# If it takes >5s, it's DNSFix: disable DNS lookup on server
nano /etc/ssh/sshd_configUseDNS nosystemctl restart sshdCause: GSSAPI authentication
SSH tries Kerberos/GSSAPI authentication that fails with timeout.
Fix on client side (on your PC):
nano ~/.ssh/configHost *
GSSAPIAuthentication no
GSSAPIDelegateCredentials noOr at connection time:
ssh -o GSSAPIAuthentication=no root@server2. Disconnections on inactivity
Fix on server side
nano /etc/ssh/sshd_config# Send keep-alive every 60 seconds, up to 10 attempts
ClientAliveInterval 60
ClientAliveCountMax 10systemctl restart sshdFix on client side
nano ~/.ssh/configHost *
ServerAliveInterval 60
ServerAliveCountMax 10
TCPKeepAlive yes3. Connection timeout (can't connect)
Cause: port blocked by firewall
# From your PC, test connection
nc -zv SERVER_IP 22
# or
telnet SERVER_IP 22If it doesn't respond, check on server (access from VNC/VirtFusion console):
# Firewall status
ufw status
iptables -L INPUT -n --line-numbers
# Is SSH running?
systemctl status sshd
ss -tlnp | grep :22SSH port changed
If you changed the SSH port:
ssh -p 2222 root@serverCause: fail2ban banned your IP
fail2ban-client status sshd
fail2ban-client set sshd unbanip YOUR_IP4. Host key verification failed
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!Server was reinstalled or SSH key changed. Remove the old key:
# From your PC
ssh-keygen -R SERVER_IP
# or remove the specific line from:
nano ~/.ssh/known_hosts5. Permission denied (publickey)
Verify permissions of public key on server
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
ls -la ~/.ssh/Verify PubkeyAuthentication is enabled
grep PubkeyAuthentication /etc/ssh/sshd_config
# Must be: PubkeyAuthentication yesDebug connection
# From your PC, show detailed debug
ssh -vvv root@server 2>&1 | head -606. Optimize SSH for fast connections
Add to ~/.ssh/config on your PC:
Host my-vps
HostName SERVER_IP
User root
IdentityFile ~/.ssh/id_ed25519
# Reuse existing connections (much faster for multiple sessions)
ControlMaster auto
ControlPath ~/.ssh/cm-%r@%h:%p
ControlPersist 10m
# Compression (useful with slow connection)
Compression yes
# No DNS, no GSSAPI
GSSAPIAuthentication no
UseDNS noWith ControlMaster, subsequent connections to same server are instant.
SSH over HTTPS (port 443)
If port 22 is blocked by your network (e.g. corporate network), you can configure SSH on port 443 on the server and connect from any network.