8. Network Tools and the Art of Troubleshooting
The classic command-line toolkit for diagnosing broken networks
Something is broken. The app can't reach the database, the API times out, the page won't load. Somewhere between your machine and the server, a packet is going missing — but where, and why?
When a plumber gets a call about no water, they don't rip open the walls first. They work a ladder of questions: Is the water even on at the street? Is it reaching the house? Is the pressure right? Is this particular tap the problem? Each answer rules out a whole layer and points at the next.
Network troubleshooting works exactly the same way. A network connection is built from layers stacked on top of each other, and each classic Unix tool answers one question at one layer. Learn the ladder, learn which tool answers which rung, and "the network is down" stops being a mystery and becomes a checklist.
The Diagnostic Ladder
Here is the mental model that ties every tool in this article together. When a connection fails, you climb the ladder from the bottom, and the first rung that fails is your culprit.
The rest of this article walks up the ladder one rung at a time, and for each tool we'll look at how it actually works — the mechanism, not just the flags. Almost every one of these tools is a thin wrapper around a clever trick, and once you see the trick the output stops looking like noise.
Rung 1 — ping: Is the Host Even There?
ping is the first thing anyone runs, and for good reason: it answers the
most basic question with the least machinery. It sends a tiny message and
waits for it to bounce back.
ping example.com
PING example.com (93.184.216.34): 56 data bytes
64 bytes from 93.184.216.34: icmp_seq=0 ttl=56 time=11.3 ms
64 bytes from 93.184.216.34: icmp_seq=1 ttl=56 time=10.9 ms
64 bytes from 93.184.216.34: icmp_seq=2 ttl=56 time=11.1 ms
The mechanism: ICMP echo
ping doesn't use TCP or UDP. It speaks
ICMP,
the internet's built-in diagnostics channel. It sends an ICMP Echo Request
(type 8) and the target is expected to send back an ICMP Echo Reply
(type 0) with the same payload copied verbatim.
Two numbers in the output do most of the work:
time=11.3 ms— the RTT.pingstamps the send time into the packet's payload; when the reply comes back it subtracts to get the round trip. High or wildly varying RTT means congestion or a flaky link.ttl=56— the TTL of the reply as it arrived. Common starting values are 64 (Linux/macOS) and 128 (Windows), so a TTL of 56 suggests the reply crossed about 64 − 56 = 8 routers. (Hold onto this idea — the next tool weaponizes it.)
Reading the results
Healthy
time=11.3 ms
time=10.9 ms
time=11.1 ms
0% packet loss
Low, stable RTT and no loss. The host is up and the path is clean.
Suspicious
Request timeout for icmp_seq 3
time=340 ms
Request timeout for icmp_seq 5
40% packet loss
High latency and dropped replies — congestion, a saturated link, or a host that's barely coping.
A subtlety worth knowing
Silence from ping does not mean the host is
down. Many firewalls drop ICMP entirely for security reasons, so a server
can be happily serving HTTPS while ignoring every ping. ping
proves reachability when it succeeds; when it fails, you climb to the next
rung rather than concluding anything.
Rung 2 — traceroute: Where Does the Path Break?
ping gives you a single yes/no for the whole path. When it fails, you want
to know which hop along the way is the problem. traceroute reveals the
entire route, router by router — and it does so with a genuinely clever abuse
of the TTL field.
The mechanism: expiring TTL on purpose
Recall that every router decrements a packet's TTL by one, and drops the
packet when the TTL hits zero — at which point it sends an ICMP Time
Exceeded (type 11) message back to the source. That "you ran out of TTL"
error message is normally a nuisance. traceroute treats it as a feature.
The trick: send packets with deliberately tiny TTLs and read the error messages to map the path.
- Send a probe with TTL = 1. The very first router decrements it to 0, drops it, and mails back a Time Exceeded. Its return address is hop 1.
- Send a probe with TTL = 2. It survives router 1 (TTL becomes 1), dies at router 2, which reveals itself as hop 2.
- Keep incrementing the TTL. Each value peels back one more hop.
- Eventually the TTL is large enough to reach the destination. The
destination doesn't send Time Exceeded — instead it signals "arrived" (an
ICMP Port Unreachable in the classic UDP scheme, since the probe targets an
unused high port).
traceroutesees that and stops.
traceroute example.com
traceroute to example.com (93.184.216.34), 30 hops max, 60 byte packets
1 router.local (192.168.1.1) 1.2 ms 1.1 ms 1.3 ms
2 10.0.0.1 (10.0.0.1) 8.4 ms 8.1 ms 8.9 ms
3 * * *
4 core2.isp.net (203.0.113.9) 12.1 ms 11.8 ms 12.4 ms
5 example.com (93.184.216.34) 11.9 ms 11.7 ms 12.0 ms
Each line is one hop. The three numbers are the RTTs of the three probes
traceroute sends per TTL by default. A line of * * * means no reply came
back before the timeout — often a router configured not to send ICMP errors,
which is common and usually harmless. Persistent stars from a hop onward to
the destination are the interesting case: that's where the path genuinely
dies.
Classic scheme vs. variants
The original Unix traceroute sends UDP probes to high,
unused destination ports (starting at 33434), so the destination answers
with "port unreachable." Modern traceroute can also send ICMP
echoes (-I) or TCP SYNs to a real port like 443
(-T) — handy when firewalls block UDP but allow HTTPS. Windows
tracert uses ICMP by default. The TTL trick is identical in
every variant; only the probe packet changes.
mtr: traceroute that never stops
traceroute is a snapshot. mtr ("My TraceRoute") runs traceroute in a loop
and builds a live table, so intermittent loss at a specific hop becomes
obvious:
mtr example.com
Host Loss% Snt Last Avg Best Wrst
1. router.local 0.0% 100 1.1 1.2 1.0 3.4
2. 10.0.0.1 0.0% 100 8.4 8.3 7.9 12.1
3. core2.isp.net 17.0% 100 40.2 38.9 11.1 90.3
4. example.com 0.0% 100 11.9 11.8 11.5 13.0
Here hop 3 shows 17% loss but hop 4 is clean — a strong hint that hop 3 is merely rate-limiting its own ICMP replies rather than actually dropping your traffic. If the loss appeared at hop 3 and stayed at every hop after it, that would point to real packet loss on the path.
Send probes with increasing TTL
Rung 3 — dig: Does the Name Resolve?
If ping example.com fails but ping 93.184.216.34 works, the problem isn't
the network at all — it's name resolution. Before any connection can
happen, the human-friendly name has to be turned into an IP address, and that
translation is the job of
DNS.
The mechanism: recursive resolution
DNS is a distributed, hierarchical database. No single server knows every
name; instead the answer is assembled by walking down a tree. Your machine
(the stub resolver) asks a recursive resolver (your ISP's, or something
like 1.1.1.1), and that server does the legwork:
The root servers know who runs .com; the .com servers know who is
authoritative for example.com; the authoritative server knows the actual
address. The recursive resolver caches answers along the way (respecting each
record's TTL),
so the second lookup of the same name is nearly instant.
dig: the DNS multitool
dig (Domain Information Groper) is the tool that shows you exactly what DNS
returns, in full detail:
dig example.com
;; QUESTION SECTION:
;example.com. IN A
;; ANSWER SECTION:
example.com. 3600 IN A 93.184.216.34
;; Query time: 12 msec
;; SERVER: 1.1.1.1#53(1.1.1.1)
The ANSWER SECTION is what you're after. Read it left to right: the name,
the cache TTL (3600 seconds), the class (IN = Internet), the record type
(A), and the value.
Different record types answer different questions:
| Type | Answers | Example value |
|---|---|---|
A |
IPv4 address | 93.184.216.34 |
AAAA |
IPv6 address | 2606:2800:220:1:... |
CNAME |
"this name is an alias for…" | example.com. |
MX |
mail servers | 10 mail.example.com. |
NS |
authoritative name servers | a.iana-servers.net. |
TXT |
free-form text (SPF, verification) | "v=spf1 ..." |
SOA |
zone's authority metadata | serial, refresh, TTLs |
Ask for a specific type by naming it:
dig example.com MX # mail servers
dig example.com AAAA # IPv6 address
dig example.com NS # who is authoritative
Two flags that answer most questions
dig +short example.com strips everything except the answer
value — perfect for scripts.
dig +trace example.com makes dig perform the
recursion itself, querying the root, then the TLD, then the authoritative
server, printing each step. It's the single best way to see the
resolution chain in the diagram above, and to find which level is
misconfigured.
host and nslookup are simpler cousins. host example.com gives a
one-line human answer; nslookup is the interactive tool that ships even on
Windows. For real debugging, dig wins because it hides nothing.
Rung 4 — ss and lsof: Is the Port Open and Listening?
The name resolves and the host is reachable, but the connection is still refused. Now the question narrows to a port: is a program actually listening on the port you're trying to reach, and are connections being established? This rung has two viewpoints — the local view (what's happening on this machine) and the remote view (can I reach that port over there).
The local view: ss
ss ("socket statistics") lists the machine's sockets. It replaced the older
netstat and is dramatically faster because it reads kernel socket data
directly instead of parsing /proc line by line. The flag combination worth
memorizing is ss -tlnp:
ss -tlnp
State Local Address:Port Process
LISTEN 0.0.0.0:22 sshd
LISTEN 127.0.0.1:5432 postgres
LISTEN 0.0.0.0:443 nginx
Decoding the flags:
-t— TCP sockets (add-ufor UDP)-l— only listening sockets-n— numeric; don't resolve port 22 → "ssh" or IPs to names (faster and clearer)-p— show the owning process (needs privilege)
That Local Address column hides a common bug. 0.0.0.0:443 means "listening
on all interfaces" — reachable from the network. But 127.0.0.1:5432 means
Postgres is bound to localhost only: nothing outside this machine can ever
connect, no matter how open the firewall is. "Connection refused from another
host" very often turns out to be a service bound to 127.0.0.1.
To see established connections instead of listeners, drop -l:
ss -tnp # established TCP connections + owning process
ss -tan # all TCP sockets in every state
The socket State column walks you through the TCP lifecycle —
LISTEN, SYN-SENT, ESTAB, TIME-WAIT, CLOSE-WAIT. A pile of sockets
stuck in CLOSE-WAIT, for instance, is a classic sign of an application that
isn't closing its connections.
lsof: everything is a file, including sockets
lsof ("list open files") takes Unix's "everything is a file" philosophy
seriously — an open network socket is just another open file descriptor. The
-i flag filters to network files:
lsof -i :443 # who is using port 443 (either end)
lsof -i -P -n # all network connections, numeric
lsof -i [email protected] # connections to a specific host
Where ss is oriented around sockets, lsof is oriented around processes
and file descriptors, so it shines when the question is "which process has
this port open?" (-P keeps port numbers numeric, -n skips DNS — both make
it faster and avoid surprises.)
The remote view: nc (and telnet)
ss and lsof tell you what's happening here. To test whether a port is
reachable over there, you need to actually try to open a connection.
nc (netcat, "the TCP/IP Swiss army knife") does exactly one useful thing for
troubleshooting: it attempts a TCP connection and tells you if it worked.
nc -zv example.com 443
Connection to example.com port 443 [tcp/https] succeeded!
-z— zero-I/O mode: just probe, don't send data-v— verbose, so you see the success/failure message
The mechanism: the TCP three-way handshake
When nc reports "succeeded," what it actually confirmed is that a TCP
three-way handshake completed. Every TCP connection begins with this
three-message dance:
The client sends a SYN (synchronize) with an initial sequence number; the server replies SYN-ACK, acknowledging the client's SYN and offering its own; the client sends a final ACK. Three packets, and both sides now agree the connection is open. This is why the failure modes are so diagnostic:
Connection refused
The SYN reached the host, but nothing is listening on that port, so the kernel replied with a TCP RST (reset). The host is up and the network is fine — the service is down or on a different port.
Connection timed out
The SYN got no answer at all. The packet was silently dropped — typically a firewall in the middle, or the host is down. Refused vs. timed out is the single most useful distinction on this rung.
telnet host port does the same handshake test and predates nc on many
systems (telnet example.com 443). For "is the port open," either works; nc
is the modern choice and scriptable.
Rung 5 — tcpdump: What Is Actually on the Wire?
You've climbed every rung and everything looks fine — the port's open, DNS
resolves, the handshake completes — yet the application still misbehaves. Time
for the tool of last resort: capture the raw packets and see the truth.
tcpdump prints a live, decoded view of the traffic flowing past a network
interface.
Because a busy interface sees thousands of packets a second, the essential
skill isn't running tcpdump — it's filtering it down to the handful of
packets you care about.
The mechanism: BPF, filtering in the kernel
tcpdump's filters are compiled to
BPF
— a miniature bytecode program that runs inside the kernel. When you write
tcp port 443, libpcap compiles it into BPF instructions, and the kernel
runs that program against every packet before copying anything up to
tcpdump. Packets that don't match are dropped in the kernel and never cost a
copy to userspace.
This design is why tcpdump can keep up on a saturated 10-gigabit link: the
expensive work of deciding is done cheaply, in the kernel, on every packet,
and only survivors are handed up.
Writing filters
The filter language (BPF syntax) reads almost like English and combines
primitives with and, or, and not:
tcpdump -i eth0 'tcp port 443' # HTTPS traffic
tcpdump -i eth0 'host 10.0.0.5' # to or from that host
tcpdump -i eth0 'tcp port 443 and host 10.0.0.5'
tcpdump -i eth0 'src 10.0.0.5 and dst port 22' # SSH from a host
tcpdump -i eth0 'icmp' # just pings
tcpdump -i eth0 'port 53' # DNS
The primitives fall into a few families:
| Primitive | Matches |
|---|---|
host H / src H / dst H |
either / source / destination address |
port P / src port P / dst port P |
either / source / destination port |
net 10.0.0.0/24 |
an address range |
tcp / udp / icmp |
a protocol |
tcp[tcpflags] & tcp-syn != 0 |
packets with the SYN flag set |
Combine primitives with and, or, and not. (The interactive explorer
below understands a practical subset of this grammar — addresses, ports,
protocols, net ranges, and boolean logic — but not the byte-offset form on
the last row.)
A couple of flags make the output readable:
tcpdump -i eth0 -n 'tcp port 443' # -n: don't resolve IPs/ports to names
tcpdump -i eth0 -nn 'port 443' # -nn: also skip port-name lookups
tcpdump -i eth0 -c 20 'icmp' # stop after 20 packets
tcpdump -i eth0 -A 'tcp port 80' # -A: print payload as ASCII
tcpdump -i eth0 -w capture.pcap # write raw packets to a file
Reading the output
A single line packs a lot in. Here's a SYN packet:
14:02:11.317 IP 10.0.0.9.51522 > 93.184.216.34.443: Flags [S], seq 12345, win 64240
Read it as: at that timestamp, an IP packet went from 10.0.0.9 port
51522 to (>) 93.184.216.34 port 443, with TCP Flags [S]
(a SYN — the opening of the handshake we saw on rung 4). You'll also see [S.]
for SYN-ACK, [.] for a bare ACK, [P.] for data (push), [F.] for FIN, and
[R.] for a reset. Watching those flags fly by, you can literally see the
handshake succeed or the reset that killed it.
The GUI cousin
Wireshark (and its terminal sibling tshark) reads the exact
same capture files and speaks a superset of the same filter ideas. A common
workflow is tcpdump -w capture.pcap on a headless server, then
open capture.pcap in Wireshark on your laptop for its rich
protocol decoding. tcpdump captures; Wireshark analyzes.
Write a filter
Putting It Together: Working the Ladder
Real troubleshooting is just walking the rungs until one fails. Suppose
"the app can't reach the database at db.internal:5432." Here's the ladder in
action:
- Resolve the name.
dig +short db.internal— no answer? DNS is your bug; stop here. An answer? Note the IP and continue. - Reach the host.
ping <that-ip>— replies mean the host is up and routable. (Remember ICMP may be filtered, so don't over-trust a failure.) - Trace the path if reachability is flaky:
mtr <that-ip>shows exactly which hop drops traffic. - Probe the port.
nc -zv <that-ip> 5432. "Succeeded" — the service is up and reachable, look at the app itself. "Refused" — the host is up but nothing's listening (or it's bound to127.0.0.1; confirm withss -tlnpon the db host). "Timed out" — a firewall is silently dropping you. - Watch the wire when nothing else explains it:
tcpdump -ni any 'host <that-ip> and port 5432'on both ends shows whether your SYN even arrives and whether a reply comes back.
Each tool eliminates a layer. By the time you reach tcpdump you usually
already know the answer — because the first four rungs told you which one
layer to point the camera at.
Summary
ping— rung 1, reachability and latency. Sends ICMP Echo Requests; read the RTT and remember silence can just mean ICMP is filtered.traceroute/mtr— rung 2, the path. Abuse the TTL field: probes with increasing TTL die one hop further along, and each router reveals itself with an ICMP Time Exceeded.mtrruns it continuously to expose intermittent loss.dig/host/nslookup— rung 3, DNS. Watch recursive resolution walk root → TLD → authoritative.dig +shortfor scripts,dig +traceto see the whole chain.ss/lsof— rung 4, local sockets.ss -tlnplists listeners and their processes; a127.0.0.1bind is a classic "connection refused" cause.lsof -ianswers "which process owns this port."nc/telnet— rung 4, remote reachability. A successful TCP three-way handshake means the port is open; refused (RST) vs. timed out (dropped) is the most diagnostic distinction you'll make all day.tcpdump— rung 5, the raw truth. BPF filters run in the kernel so only the packets you care about reach userspace. Learn the filter language and the TCP flag codes, and hand the.pcapto Wireshark when you need deeper decoding.
Learn the ladder, and "the network is down" becomes a five-question checklist with a tool for each question.