How to fix "ssh: connect to host port 22: Connection timed out"

Tested: we reproduced this error and confirmed the fix on Ubuntu 24.04 with OpenSSH 9.6p1 and iptables, November 29, 2026.

Connection timed out means your connection request got no answer at all. Something between you and the server dropped it: most often a firewall or cloud security group that doesn’t allow port 22 from your address, a wrong IP address, or a server that’s off or still booting. Check the address first, then the firewall:

nc -zv -w 5 server.example.com 22

We reproduced it with OpenSSH 9.6 by adding a firewall rule that silently drops packets to a port where a server was listening. SSH printed:

ssh: connect to host 127.0.0.3 port 2340: Connection timed out

and nc printed timed out: Operation now in progress. Changing the same rule from dropping packets to rejecting them gave Connection refused instead, which is why a timeout points at something on the path rather than the SSH server itself.

Find your cause

Check How What it means
Is the address right? Compare with your cloud console or ping the host A wrong or old IP times out
Is the server running? Cloud console, or ask whoever runs it A stopped instance times out
Does the cloud firewall allow you? AWS security group, Azure NSG, GCP firewall, DigitalOcean firewall Inbound TCP 22 must be open to your IP
Does the server’s own firewall allow you? sudo ufw status or sudo iptables -L -n on the server A DROP rule times out
Is your network blocking SSH? Try from another network or a phone hotspot Some office and public Wi-Fi block outgoing port 22
Is SSH on another port? Check the server’s Port setting Connecting to a closed, filtered port times out

Fix: open port 22 in the cloud firewall

On AWS, Azure, Google Cloud and DigitalOcean, the instance’s firewall or security group needs an inbound rule for TCP port 22 from your IP address (or the address range you connect from). AWS’s own SSH troubleshooting guide starts with the security group and network ACLs for the same reason. If your home IP changes, update the rule or connect through a VPN or bastion host with a fixed address.

Fix: the server’s own firewall

With access to the server (cloud console, recovery mode), allow SSH:

sudo ufw allow OpenSSH
sudo ufw status

Fix: wait less when it fails

By default SSH waits as long as the operating system allows before giving up. Set a shorter limit so scripts fail fast. In our test -o ConnectTimeout=8 gave up after 8 seconds:

ssh -o ConnectTimeout=8 user@server

Sessions that freeze or drop when idle

A different problem with similar words: you’re connected, leave it idle and it freezes or ends with client_loop: send disconnect: Broken pipe. A router or firewall dropped the idle connection. Keep it alive from the client:

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 3

That sends a keepalive every 60 seconds and gives up after three missed replies. It goes in ~/.ssh/config; see our SSH config file guide.

Confirm it worked

nc -zv server 22 reports the port open, and ssh -v gets past Connecting to ... port 22 to Connection established. If you now see Connection refused or No route to host, the path is open and the problem is on the server: see Connection refused or No route to host.

FAQ

What's the difference between timed out and connection refused?

Refused means the server answered and said nothing is listening on that port. Timed out means no answer came back at all, so something on the way (a firewall, a wrong address, a powered-off server) swallowed the connection. See Connection refused for the other case.

How do I change how long SSH waits?

Use ConnectTimeout, in seconds: ssh -o ConnectTimeout=10 user@host, or ConnectTimeout 10 in ~/.ssh/config. Without it, SSH waits as long as the operating system does, which can be more than a minute.

My session freezes and drops after a while idle. Is that the same error?

No. That's a connection that was working and got cut by a router or firewall that forgets idle connections. ServerAliveInterval 60 in ~/.ssh/config sends a small keepalive every minute so it stays open.