SkillPilotAI

Lab: can't connect to the server

When you cannot connect to a server, the error message is your biggest clue: Connection refused, Connection timed out and No route to host each point to a different place (the service, a firewall, or routing). Read the error first, then follow a short checklist: listen address, host firewall, route both ways, NAT mapping.

276.1 Why is it used?

  • Story: Ravi's app server runs on 192.168.76.20 port 8080; his laptop is 192.168.76.10.
  • The connection fails every time, with a different error message each time.
  • Each error means a problem in a different place, so reading it correctly saves a lot of time.

276.2 How it works: three errors, three places

  • Connection refused: the SYN reaches the server but nothing listens on 8080, so the kernel sends back an RST; a firewall reject rule does the same.
  • Connection timed out: a firewall on the way silently drops the SYN, no reply ever comes; the client gives up after a few seconds.
  • No route to host / Network unreachable: a routing problem, e.g. a wrong route via a gateway that does not exist.
ErrorWhat happened on the wireWhere to look
Connection refusedSYN reached the host, RST came backService not running, wrong listen address, reject rule
Connection timed outSYN sent, no reply at allFirewall drop or missing route
No route to host / Network unreachableNo usable route, or ARP for the next hop failedRoute table, gateway, ip neigh
Error 3: a more specific wrong route
  1. 1.Laptop route table has 192.168.76.0/24 (direct)

  2. 2.Someone adds 192.168.76.0/25 via 192.168.76.99

  3. 3./25 is more specific, so traffic to .20 picks gateway .99

  4. 4.ARP for 192.168.76.99 gets no reply (device missing)

  5. 5.Laptop reports No route to host

Longest prefix match: the most specific route always wins

276.3 The 4-step checklist

  1. Service listening? On which address? 127.0.0.1 = local only, 0.0.0.0 = all interfaces.
  2. Port open in the host firewall? (nftables, iptables, Windows Firewall)
  3. Route both ways? The server must also know the way back for the reply.
  4. NAT / port forward mapping: does the public port point to the correct internal port?
ToolPlatformWhat it tells you
Test-NetConnection -PortWindowsDid the TCP port connect?
netstat -anoWindowsListening ports + PID
nc -zvLinuxPort test
ss -lnt / netstat -lntLinuxListening sockets and their address
curl, ip routeBothApp test, route check
tcpdumpLinuxWhat actually arrives on the wire
Port forwarding: public 80 -> internal 8080
  1. 1.Ravi (outside) sends a request to the router on public port 80

  2. 2.Rule forwards it to internal port 8081 by mistake

  3. 3.Nothing listens on 8081: the server returns an RST (refused)

  4. 4.Mapping corrected to 8080

  5. 5.Same request reaches the app and returns 200 OK

276.4 Practical 1: port test on Windows

PowerShell
Test-NetConnection 127.0.0.1 -Port 1 `  -InformationLevel QuietTest-NetConnection 8.8.8.8 -Port 53 `  -InformationLevel Quiet
Output
$ Test-NetConnection 127.0.0.1 -Port 1 -InformationLevel QuietWARNING: TCP connect to (127.0.0.1 : 1) failedFalse$ Test-NetConnection 8.8.8.8 -Port 53 -InformationLevel QuietTrue
  • No service on port 1 -> warning + False; DNS port 53 on 8.8.8.8 -> True.

276.5 Practical 2: the Docker lab baseline

Terminal
nc -zv -w 3 192.168.76.20 8080curl -s http://192.168.76.20:8080/
Output
$ nc -zv -w 3 192.168.76.20 8080Connection to 192.168.76.20 8080 port [tcp/http-alt] succeeded!$ curl -s http://192.168.76.20:8080/Hello from app (ravi lab)
  • Isolated lab: client 192.168.76.10, app server runs busybox httpd on 192.168.76.20:8080.

276.6 Fault 1: service stopped -> refused

Terminal
# fault 1: httpd stopped (on app)nc -zv -w 3 192.168.76.20 8080curl -sS http://192.168.76.20:8080/tcpdump -nn -i eth0 -c 2 'tcp port 8080' &nc -z -w 3 192.168.76.20 8080netstat -lnt                 # on app# fix: start httpd again (on app)httpd -f -p 8080 -h /wwwnc -zv -w 3 192.168.76.20 8080
Output
$ nc -zv -w 3 192.168.76.20 8080nc: connect to 192.168.76.20 port 8080 (tcp) failed: Connection refused$ curl -sS http://192.168.76.20:8080/curl: (7) Failed to connect to 192.168.76.20 port 8080 after 0 ms: Couldn't connect to server$ tcpdump -nn -i eth0 -c 2 'tcp port 8080' &$ nc -z -w 3 192.168.76.20 80802 packets captured22:16:35.702815 IP 192.168.76.10.36528 > 192.168.76.20.8080: Flags [S], seq 4115075124, win 64240, options [mss 1460,sackOK,TS val 3101391648 ecr 0,nop,wscale 10], length 022:16:35.702927 IP 192.168.76.20.8080 > 192.168.76.10.36528: Flags [R.], seq 0, ack 4115075125, win 0, length 0[app]$ netstat -lntProto Recv-Q Send-Q Local Address           Foreign Address         Statetcp        0      0 127.0.0.11:35731        0.0.0.0:*               LISTEN[app]$ netstat -lnttcp        0      0 127.0.0.11:35731        0.0.0.0:*               LISTENtcp        0      0 :::8080                 :::*                    LISTEN$ nc -zv -w 3 192.168.76.20 8080Connection to 192.168.76.20 8080 port [tcp/http-alt] succeeded!
  • tcpdump proves it: SYN [S] out, RST [R.] back, so the host is reachable; netstat has no LISTEN on 8080.

276.7 Fault 2: listening on 127.0.0.1 only

Terminal
# fault 2 (on app): local onlyhttpd -f -p 127.0.0.1:8080 -h /wwwnc -zv -w 3 192.168.76.20 8080wget -qO- http://127.0.0.1:8080/   # on appnetstat -lnt                       # on app# fix (on app): all addresseshttpd -f -p 8080 -h /wwwnetstat -lntnc -zv -w 3 192.168.76.20 8080
Output
$ nc -zv -w 3 192.168.76.20 8080nc: connect to 192.168.76.20 port 8080 (tcp) failed: Connection refused[app]$ wget -qO- http://127.0.0.1:8080/Hello from app (ravi lab)[app]$ netstat -lntProto Recv-Q Send-Q Local Address           Foreign Address         Statetcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTENtcp        0      0 127.0.0.11:35731        0.0.0.0:*               LISTEN[app]$ netstat -lntProto Recv-Q Send-Q Local Address           Foreign Address         Statetcp        0      0 127.0.0.11:35731        0.0.0.0:*               LISTENtcp        0      0 :::8080                 :::*                    LISTEN$ nc -zv -w 3 192.168.76.20 8080Connection to 192.168.76.20 8080 port [tcp/http-alt] succeeded!
  • The most classic trap: the service runs and works inside the server, but is bound to 127.0.0.1, so outside connections are refused.
  • :::8080 is the IPv6-style "all addresses" (like 0.0.0.0).

276.8 Fault 3: firewall drop -> timeout

Terminal
# fault 3 (on app): drop tcp 8080nft add table ip n276nft add chain ip n276 input \  '{ type filter hook input priority 0; }'nft add rule ip n276 input \  tcp dport 8080 counter dropnc -zv -w 3 192.168.76.20 8080curl -sS --connect-timeout 3 \  http://192.168.76.20:8080/nft list chain ip n276 input   # on appping -c 2 -W 1 192.168.76.20# fix (on app)nft delete table ip n276nc -zv -w 3 192.168.76.20 8080
Output
[app]$ nft list chain ip n276 inputtable ip n276 {	chain input {		type filter hook input priority filter; policy accept;		tcp dport 8080 counter packets 0 bytes 0 drop	}}$ nc -zv -w 3 192.168.76.20 8080nc: connect to 192.168.76.20 port 8080 (tcp) timed out: Operation in progress$ curl -sS --connect-timeout 3 http://192.168.76.20:8080/curl: (28) Failed to connect to 192.168.76.20 port 8080 after 3001 ms: Timeout was reached[app]$ nft list chain ip n276 input	chain input {		type filter hook input priority filter; policy accept;		tcp dport 8080 counter packets 6 bytes 360 drop	}$ ping -c 2 -W 1 192.168.76.2064 bytes from 192.168.76.20: icmp_seq=1 ttl=64 time=0.100 ms64 bytes from 192.168.76.20: icmp_seq=2 ttl=64 time=0.078 ms2 packets transmitted, 2 received, 0% packet loss, time 1027ms[app]$ nft delete table ip n276$ nc -zv -w 3 192.168.76.20 8080Connection to 192.168.76.20 8080 port [tcp/http-alt] succeeded!
  • Now it is timed out, not refused; the counter went 0 -> 6 packets (the SYN and its retries).
  • Ping still works because the rule matches only tcp port 8080.

276.9 Fault 4: wrong route -> unreachable

Terminal
# fault 4 (on client): wrong routeip route add 192.168.76.0/25 \  via 192.168.76.99ip routeip route get 192.168.76.20nc -zv -w 6 192.168.76.20 8080ip neigh show 192.168.76.99# fixip route del 192.168.76.0/25ip route get 192.168.76.20nc -zv -w 3 192.168.76.20 8080
Output
$ ip route192.168.76.0/25 via 192.168.76.99 dev eth0192.168.76.0/24 dev eth0 proto kernel scope link src 192.168.76.10$ ip route get 192.168.76.20192.168.76.20 via 192.168.76.99 dev eth0 src 192.168.76.10 uid 0$ nc -zv -w 6 192.168.76.20 8080nc: connect to 192.168.76.20 port 8080 (tcp) failed: Host is unreachable$ ip neigh show 192.168.76.99192.168.76.99 dev eth0 FAILED$ ip route del 192.168.76.0/25$ ip route get 192.168.76.20192.168.76.20 dev eth0 src 192.168.76.10 uid 0$ nc -zv -w 3 192.168.76.20 8080Connection to 192.168.76.20 8080 port [tcp/http-alt] succeeded!
  • Host is unreachable is the Linux wording of No route to host; ip neigh FAILED shows ARP for .99 got no reply.

276.10 Fault 5: port forward 80 -> wrong port

Terminal
# fault 5 (on app): 80 -> 8081 (wrong)nft add table ip n276natnft add chain ip n276nat prerouting \  '{ type nat hook prerouting priority -100; }'nft add rule ip n276nat prerouting \  tcp dport 80 counter dnat to 192.168.76.20:8081nc -zv -w 3 192.168.76.20 80netstat -lnt                   # on app# fix (on app): flush, 80 -> 8080nft add rule ip n276nat prerouting \  tcp dport 80 counter dnat to 192.168.76.20:8080nc -zv -w 3 192.168.76.20 80curl -s http://192.168.76.20/nft list chain ip n276nat prerouting
Output
[app]$ nft list chain ip n276nat preroutingtable ip n276nat {	chain prerouting {		type nat hook prerouting priority dstnat; policy accept;		tcp dport 80 counter packets 0 bytes 0 dnat to 192.168.76.20:8081	}}$ nc -zv -w 3 192.168.76.20 80nc: connect to 192.168.76.20 port 80 (tcp) failed: Connection refused[app]$ netstat -lnttcp        0      0 127.0.0.11:35731        0.0.0.0:*               LISTENtcp        0      0 :::8080                 :::*                    LISTEN[app]$ nft list chain ip n276nat prerouting	chain prerouting {		type nat hook prerouting priority dstnat; policy accept;		tcp dport 80 counter packets 0 bytes 0 dnat to 192.168.76.20:8080	}$ nc -zv -w 3 192.168.76.20 80Connection to 192.168.76.20 80 port [tcp/http] succeeded!$ curl -s http://192.168.76.20/Hello from app (ravi lab)[app]$ nft list chain ip n276nat prerouting	chain prerouting {		tcp dport 80 counter packets 2 bytes 120 dnat to 192.168.76.20:8080	}
  • Refused on port 80 while 8080 is listening: the service is fine, the mapping is wrong.
  • After the fix the dnat counter shows 2 packets going to 8080.

276.11 Common mistakes

  • Myth: if it does not connect, the firewall is blocking it. A firewall drop gives a timeout; refused means the packet reached the host and nothing listens there.
  • Refused outside but fine inside the server: check the listen address first (127.0.0.1 vs 0.0.0.0).
  • Changing firewall rules before reading the error message carefully.
  • Forgetting the return route: the server must know the way back for the reply.
  • Checking only the public port in port forwarding, not the internal port it maps to.

276.12 Interview tips

  • Refused = SYN reached, RST came back: nothing listening or a reject rule.
  • Timed out = no reply at all: firewall drop or missing route.
  • No route to host / Network unreachable = routing (check ip route get and ip neigh).
  • Then walk the checklist: listen address, host firewall, route both ways, NAT mapping.