InfraRunBook
    Back to articles

    SSH Connection Refused vs Connection Timed Out on Linux: Causes and Fixes

    Linux
    Published: Nov 30, 2026
    Updated: Nov 30, 2026

    Learn how to tell an SSH 'Connection refused' from a 'Connection timed out' on Linux and fix each root cause, from a stopped sshd to firewall drops, wrong ports, and routing problems.

    SSH Connection Refused vs Connection Timed Out on Linux: Causes and Fixes

    Symptoms

    You type a perfectly ordinary command and get one of two answers back. They look similar. They are not. They point at completely different parts of the stack, and mixing them up will cost you an hour.

    $ ssh infrarunbook-admin@10.10.1.20
    ssh: connect to host 10.10.1.20 port 22: Connection refused
    
    $ ssh infrarunbook-admin@10.10.1.20
    ssh: connect to host 10.10.1.20 port 22: Connection timed out

    Here's the distinction I drill into every junior engineer. Connection refused means a TCP SYN reached a host and that host (or something in front of it) sent back a RST. Somebody answered, and the answer was no. Connection timed out means the SYN went out and nothing ever came back. The packet was dropped, never routed, or the host is gone. With verbose output you can watch it happen:

    $ ssh -v infrarunbook-admin@10.10.1.20
    OpenSSH_9.6p1, OpenSSL 3.0.13
    debug1: Connecting to 10.10.1.20 [10.10.1.20] port 22.
    debug1: connect to address 10.10.1.20 port 22: Connection timed out
    ssh: connect to host 10.10.1.20 port 22: Connection timed out

    Note what you will not see in either case: a banner, a key exchange, or a password prompt. If you get as far as

    kex_exchange_identification
    or
    Permission denied (publickey)
    , you're past the network layer and this article isn't your problem. We're dealing with failures that happen before SSH proper begins.

    A quick first test that works from any client, without involving the SSH binary at all:

    $ nc -zv -w 5 10.10.1.20 22
    nc: connect to 10.10.1.20 port 22 (tcp) failed: Connection refused
    
    $ nc -zv -w 5 10.10.1.20 22
    nc: connect to 10.10.1.20 port 22 (tcp) timed out: Operation now in progress

    Now let's walk through the causes. I've ordered them roughly by how often I see them, split by which symptom they produce.


    Cause 1: sshd Isn't Running (Refused)

    Why it happens. The most boring cause is also the most common. The daemon crashed, was never enabled after a fresh install, or failed to start after a config edit. The kernel has no listener on port 22, so it answers every SYN with a RST. Instant refusal.

    How to identify it. You need console access, a hypervisor shell, or another path in. On the server:

    infrarunbook-admin@sw-infrarunbook-01:~$ systemctl status sshd
    × ssh.service - OpenBSD Secure Shell server
         Loaded: loaded (/lib/systemd/system/ssh.service; enabled)
         Active: failed (Result: exit-code) since Mon 2026-11-30 09:12:44 UTC; 3min ago
        Process: 1482 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=255)
    
    sshd[1482]: /etc/ssh/sshd_config line 41: Bad configuration option: PermitRootLogn

    That typo is a classic. I have seen this happen right after someone edited the config and restarted the service without validating it first.

    How to fix it. Validate the config, fix it, then start the service:

    $ sudo sshd -t
    /etc/ssh/sshd_config line 41: Bad configuration option: PermitRootLogn
    $ sudo sed -i 's/PermitRootLogn/PermitRootLogin/' /etc/ssh/sshd_config
    $ sudo sshd -t && echo OK
    OK
    $ sudo systemctl enable --now sshd
    $ ss -tlnp | grep :22
    LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1733,fd=3))

    Cause 2: sshd Is Listening on a Different Port (Refused)

    Why it happens. Plenty of hardening guides say to move SSH off port 22. Someone does it, nobody documents it, and six months later you're staring at a refusal on 22 while the daemon is perfectly healthy on 2222.

    How to identify it. On the server, check what's actually bound:

    $ sudo ss -tlnp | grep sshd
    LISTEN 0 128 0.0.0.0:2222 0.0.0.0:* users:(("sshd",pid=911,fd=3))
    
    $ sudo sshd -T | grep -E '^(port|listenaddress)'
    port 2222
    listenaddress 0.0.0.0:2222

    Also check

    ListenAddress
    . If it's bound only to
    127.0.0.1
    or to a management IP like 10.10.1.5, connections to any other address get refused even though sshd is up.

    How to fix it. Either connect with the right port (

    ssh -p 2222 infrarunbook-admin@10.10.1.20
    ) or put the intended values in
    /etc/ssh/sshd_config
    and reload. Put a
    Host
    block in
    ~/.ssh/config
    so nobody has to remember:

    Host sw-infrarunbook-01
        HostName 10.10.1.20
        Port 2222
        User infrarunbook-admin

    Cause 3: SELinux Blocking a Non-Standard Port (Refused)

    Why it happens. This one bites people on RHEL-family systems. You change

    Port 22
    to
    Port 2222
    , restart sshd, and it fails to bind because SELinux only allows
    sshd_t
    to bind to ports labelled
    ssh_port_t
    . The daemon dies, so clients get refused.

    How to identify it.

    $ sudo journalctl -u sshd -n 5
    sshd[2210]: error: Bind to port 2222 on 0.0.0.0 failed: Permission denied.
    sshd[2210]: fatal: Cannot bind any address.
    
    $ sudo ausearch -m avc -ts recent | grep 2222
    type=AVC msg=audit(1764493021.114:402): avc: denied { name_bind } for pid=2210 comm="sshd" src=2222 scontext=system_u:system_r:sshd_t:s0 tcontext=system_u:object_r:unreserved_port_t:s0 tclass=tcp_socket permissive=0

    How to fix it. Don't disable SELinux. Label the port:

    $ sudo semanage port -a -t ssh_port_t -p tcp 2222
    $ sudo semanage port -l | grep ssh_port_t
    ssh_port_t                     tcp      2222, 22
    $ sudo systemctl restart sshd

    If

    semanage
    is missing, install
    policycoreutils-python-utils
    .


    Cause 4: Firewall Rejecting the Connection (Refused)

    Why it happens. A firewall can produce a refusal too. If the rule action is

    REJECT
    rather than
    DROP
    , the firewall sends back an ICMP port-unreachable or a TCP RST on purpose. From the client it looks identical to a dead daemon, which makes it a nasty trap. I have lost time chasing sshd configs on hosts where sshd was fine and an iptables REJECT was the culprit.

    How to identify it. If

    ss -tlnp
    shows sshd listening locally but remote clients get refused, suspect the firewall:

    $ sudo iptables -L INPUT -n -v --line-numbers
    Chain INPUT (policy ACCEPT)
    num  pkts bytes target  prot opt in  out source     destination
    1     120  7200 REJECT  tcp  --  *   *   0.0.0.0/0  0.0.0.0/0  tcp dpt:22 reject-with tcp-reset
    
    $ sudo nft list ruleset | grep -n 'dport 22'
    18:  tcp dport 22 reject with tcp reset

    A tcpdump on the server settles any doubt. If you see the SYN arrive and a RST leave while sshd is listening, it's the firewall:

    $ sudo tcpdump -ni eth0 'tcp port 22 and host 10.10.2.15'
    10:41:02.118 IP 10.10.2.15.51234 > 10.10.1.20.22: Flags [S], seq 1829
    10:41:02.118 IP 10.10.1.20.22 > 10.10.2.15.51234: Flags [R.], seq 0, ack 1830

    How to fix it. Insert an accept rule ahead of the reject, scoped to your management network:

    $ sudo iptables -I INPUT 1 -p tcp -s 10.10.2.0/24 --dport 22 -j ACCEPT
    
    # nftables equivalent
    $ sudo nft insert rule inet filter input ip saddr 10.10.2.0/24 tcp dport 22 accept

    With firewalld, use

    firewall-cmd --permanent --add-service=ssh && firewall-cmd --reload
    . Remember that runtime rules vanish on reboot unless you persist them.


    Cause 5: Firewall Silently Dropping Packets (Timed Out)

    Why it happens. This is the number one cause of timeouts. A DROP rule, a default-deny policy with no SSH allow, a cloud security group, or an upstream network ACL discards the SYN without telling anyone. The client retransmits for roughly two minutes and gives up.

    How to identify it. Run tcpdump on the server while the client tries. If nothing arrives, the block is upstream. If SYNs arrive and no SYN-ACK leaves, it's the host firewall:

    $ sudo tcpdump -ni eth0 'tcp port 22 and host 10.10.2.15'
    10:44:10.002 IP 10.10.2.15.51300 > 10.10.1.20.22: Flags [S], seq 4411
    10:44:11.004 IP 10.10.2.15.51300 > 10.10.1.20.22: Flags [S], seq 4411
    10:44:13.008 IP 10.10.2.15.51300 > 10.10.1.20.22: Flags [S], seq 4411
    
    $ sudo iptables -S INPUT
    -P INPUT DROP
    -A INPUT -i lo -j ACCEPT
    -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

    Look at those repeated SYNs with growing gaps. That's the classic retransmission pattern, and the default policy is DROP with no port 22 rule.

    How to fix it. Add the allow rule and persist it:

    $ sudo iptables -A INPUT -p tcp -s 10.10.2.0/24 --dport 22 -m conntrack --ctstate NEW -j ACCEPT
    $ sudo iptables-save | sudo tee /etc/iptables/rules.v4

    If the SYNs never show up on the server at all, stop poking at the host. Check the cloud security group, the upstream firewall, and any hypervisor-level filtering.


    Cause 6: Routing, Wrong IP, or Host Down (Timed Out)

    Why it happens. Sometimes there's simply no path. The server has the wrong default gateway, the client is on a VLAN with no route to 10.10.1.0/24, the IP was reassigned, or the machine is powered off. ARP goes unanswered, packets vanish, and you get a timeout.

    How to identify it. Work from the client outward:

    $ ip route get 10.10.1.20
    10.10.1.20 via 10.10.2.1 dev eth0 src 10.10.2.15
    
    $ ping -c 3 10.10.1.20
    PING 10.10.1.20 (10.10.1.20) 56(84) bytes of data.
    From 10.10.2.1 icmp_seq=1 Destination Host Unreachable
    
    $ ip neigh show 10.10.1.20
    10.10.1.20 dev eth0 FAILED

    A

    FAILED
    neighbour entry on the same L2 segment means nobody is answering ARP. Either the host is down or you have the wrong address. Remember that ICMP may be blocked while SSH works, so a failed ping alone proves little. Use
    traceroute -T -p 22 10.10.1.20
    to follow the TCP path and see where it dies.

    How to fix it. Correct the route or address, then verify:

    $ sudo ip route add 10.10.1.0/24 via 10.10.2.1 dev eth0
    $ ip route get 10.10.1.20
    10.10.1.20 via 10.10.2.1 dev eth0 src 10.10.2.15

    Check the return path too. Asymmetric routing, where replies leave via a different interface, will get dropped by

    rp_filter
    and looks exactly like a timeout.
    sysctl net.ipv4.conf.all.rp_filter
    will tell you if strict mode is on.


    Cause 7: Rate Limiting and Ban Tools (Timed Out or Refused)

    Why it happens. fail2ban, sshguard, or a hand-written

    recent
    module rule will block your own IP after a few failed attempts. Depending on the configured action, you'll see a timeout (DROP) or a refusal (REJECT). The maddening part is that it works from other machines, so you assume the server is fine.

    How to identify it. If one client fails and another connects, check for bans:

    $ sudo fail2ban-client status sshd
    Status for the jail: sshd
    |- Filter
    |  |- Currently failed: 2
    |  `- Total failed:     47
    `- Actions
       |- Currently banned: 1
       `- Banned IP list:   10.10.2.15
    
    $ sudo iptables -L f2b-sshd -n
    REJECT  all  --  10.10.2.15  0.0.0.0/0  reject-with icmp-port-unreachable

    Also watch for sshd's own

    MaxStartups
    limit. Under a brute-force flood, sshd begins dropping new unauthenticated connections, and legitimate clients get reset.

    How to fix it. Unban the address, then fix whatever caused the failures, usually a script with a stale key:

    $ sudo fail2ban-client set sshd unbanip 10.10.2.15
    1
    
    # permanently whitelist your management range in /etc/fail2ban/jail.local
    [DEFAULT]
    ignoreip = 127.0.0.1/8 10.10.2.0/24

    A Fast Decision Flow

    When I'm paged for this, I run through the same short sequence. Refused? Go straight to the server and check

    ss -tlnp
    . Nothing listening means causes 1, 2 or 3. Listening but still refused remotely means a REJECT rule or a ban. Timed out? Run tcpdump on the server. No SYN arriving means the problem is upstream or in routing. SYN arriving with no reply means a host firewall DROP. That one test eliminates half the guesswork.


    Prevention

    Most of these outages are self-inflicted, so most of the prevention is discipline rather than tooling.

    First, never restart sshd without validating. Make

    sshd -t
    a reflex, and keep your existing session open while you test a second one. A bad config applied to a running daemon won't kill your current connection, but a restart on a broken file will leave you locked out.

    Second, document and standardise ports. If you move SSH off 22, record it in your inventory, add the SELinux label in the same change, and ship a matching

    ~/.ssh/config
    block through configuration management. Treat the firewall rule and the port as one change.

    Third, prefer explicit allow rules for management subnets and persist them. A rule that exists only in runtime memory is a reboot away from becoming a timeout. Decide deliberately between DROP and REJECT; REJECT on internal networks gives faster, clearer failures, while DROP suits internet-facing interfaces.

    Fourth, whitelist your admin ranges in fail2ban and monitor the jail. An alert on bans from internal addresses catches runaway automation before it locks you out.

    Finally, keep out-of-band access. A serial console, hypervisor console, or IPMI session is the only thing that saves you when sshd is the thing that's broken. I've never regretted setting it up, and I've often regretted skipping it.

    Frequently Asked Questions

    What is the difference between SSH connection refused and connection timed out?

    Connection refused means the target host or a firewall actively replied with a TCP reset, so something answered and rejected the connection. Connection timed out means no reply came back at all, which points to a dropped packet, a routing problem, or a host that's down.

    Why do I get connection refused when sshd is running?

    The daemon may be listening on a different port or address than you're targeting, or a firewall REJECT rule or fail2ban ban may be sending a reset on its behalf. Compare the output of ss -tlnp with your firewall rules and ban lists.

    How can I tell if a firewall is dropping SSH traffic?

    Run tcpdump on the server filtered to port 22 while the client connects. If SYN packets arrive repeatedly with no SYN-ACK going out, a host firewall is dropping them. If nothing arrives, the block is upstream.

    Why does SSH fail after changing the port on RHEL or Rocky Linux?

    SELinux only lets sshd bind to ports labelled ssh_port_t. Add the new port with semanage port -a -t ssh_port_t -p tcp PORT and restart sshd.

    How do I safely change sshd_config without locking myself out?

    Run sshd -t to validate the file, keep your current session open, reload the service, and confirm you can open a second session before closing the first.

    Related Articles