Debugging the Network
Which layer failed, and a checklist to work down.
Work down the layers. The first answer that surprises you is the bug.
The method
Four questions, in order. Each one rules out everything below it:
- Does the name resolve to an address?
- Is the address reachable?
- Is the port open?
- Is the application answering correctly?
Does it resolve
Before any packet goes anywhere, the name becomes an address:
getent hosts example.com | head -1 | awk '{print $2}'A name that does not resolve never reaches the network at all:
getent hosts nosuchhost.invalid >/dev/null 2>&1 \
&& echo resolved || echo "does not resolve"A stale record looks exactly like a dead server, and getent is the fastest way to rule it
out.
Is it reachable
ping tests IP, below any port:
ping -c 1 -W 2 1.1.1.1 | sed -n 2p | grep -oE 'ttl=[0-9]+'Is the port open
This is the question that usually matters, and nc -z answers it in one word:
nc -z -w 1 127.0.0.1 9000 2>/dev/null && echo open || echo closednc -z -w 1 127.0.0.1 9999 2>/dev/null && echo open || echo closedIf it answers closed instantly the port is shut; if it hangs until the timeout, nothing
is answering — chapter 3.
tcpdump filters
Filters keep a capture readable. They combine with and, or and not:
cap "-i lo -n -c 2 host 127.0.0.1 and port 9000" \
"echo hi | nc -w 1 127.0.0.1 9000"| Filter | Catches |
|---|---|
host 1.2.3.4 |
both directions to one machine |
port 443 |
either source or destination |
src host 1.2.3.4 |
one direction only |
tcp / udp / icmp |
one protocol |
tcp[tcpflags] & tcp-syn != 0 |
connection attempts only |
That last one is how you find what is trying to connect and failing:
cap "-i lo -n -c 2 tcp[tcpflags] & tcp-syn != 0 and port 9000" \
"echo hi | nc -w 1 127.0.0.1 9000"Without -n, tcpdump resolves every address to a name — slow, and it hides the numbers
you are looking for.
Reading a capture
A whole connection, and what each line told you:
cap "-i lo -n -c 8 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000"| Line | Says |
|---|---|
| 1–3 | the handshake completed — the service is up |
| 4 | three bytes sent |
| 5 | they were acknowledged — the server read them |
| 6–8 | a clean close, not a crash |
Dropped versus rejected
The same firewall policy produces opposite symptoms, and this is the distinction nothing else will tell you:
| Firewall | Packets back | You see |
|---|---|---|
| Rejects | [R.] immediately |
connection refused |
| Drops | none | connection timed out |
nc -z -w 1 127.0.0.1 9999 2>/dev/null && echo open || echo closedThat was instant, so something answered. A capture is the only way to be sure — a timeout
with repeated [S] and no reply is a drop.
The checklist
Eight checks, and the first three are not the application:
| # | Check | Command | Chapter |
|---|---|---|---|
| 1 | Does the name resolve? | getent hosts name |
8 |
| 2 | Is the host reachable? | ping -c 1 host |
6 |
| 3 | How far away is it? | traceroute host |
6 |
| 4 | Is the port open? | nc -z -w 1 host port |
3 |
| 5 | Is anything listening locally? | ss -tln |
2 |
| 6 | Bound to the right address? | ss -tln, check 0.0.0.0 |
2 |
| 7 | Does the handshake complete? | cap "-c 3 port N" |
3 |
| 8 | Is data acknowledged? | cap "-c 8 port N" |
4 |