Part 6 · Real networks: infrastructure, security and operations

Network troubleshooting: a systematic method with tcpdump and curl

lesson 17 / 197 min1 labpublished updated
Course contents
On this page
  1. The method: bottom up, or split the path
  2. The toolbox, layer by layer
  3. tcpdump: the ground truth
  4. curl for everything HTTP
  5. Common scenarios

You can track down almost any network problem with one fixed method and about ten tools. Rule one: don’t guess, measure. Rule two: split the problem in half, and find where along the path things work and where they stop working.

The method: bottom up, or split the path

  1. Pin down the problem: what fails, from where, to where, since when, always or only sometimes? What is the exact error message? (“It doesn’t work” is not data.)
  2. Go layer by layer, from the bottom: Is the link up? Are the IP address and route correct? Does DNS answer correctly? Is the port open? Does the TLS handshake complete? What does HTTP say?
  3. Or split the path: Does it work from the same host to the destination? From another host on the same network? From the Internet? Straight to the IP instead of the name? To the backend instead of the load balancer?
  4. Change only one variable at a time.
  5. Once you’ve found it, understand the cause and write it down.

The toolbox, layer by layer

LayerQuestionTools
LinkIs the interface up? Any hardware errors?ip -br link, ethtool eth0, ethtool -S
NeighborIs the gateway reachable?ip neigh, arping
IP and routingAre the address and the route right?ip -br addr, ip route get <dst>, ping, traceroute, mtr, tracepath (MTU)
DNSDoes the name resolve correctly? Through which resolver?dig, dig +trace, getent hosts, resolvectl
TransportIs the port listening? Is it reachable?ss -tulpn, nc -vz host 443, nmap -p
TLSAre the handshake and the certificate right?openssl s_client -connect host:443 -servername host
HTTPWhat’s the response, and how long does each phase take?curl -v, curl -w, curl --resolve
All layersWhat is really on the wire?tcpdump
Firewall and NATWhich rule is being applied?nft list ruleset, iptables -S -t nat, conntrack -L
ThroughputWhat’s the real capacity of the path?iperf3 -c server, ss -ti

tcpdump: the ground truth

sudo tcpdump -i any -nn host 10.0.0.5                        # all traffic to/from one host
sudo tcpdump -i eth0 -nn 'tcp port 443 and host api.example.com'
sudo tcpdump -i any -nn 'tcp[tcpflags] & (tcp-syn|tcp-rst) != 0'   # only SYNs and RSTs
sudo tcpdump -i any -nn -A 'tcp port 80'                     # print payloads as ASCII
sudo tcpdump -i any -nn udp port 53                          # DNS queries and answers
sudo tcpdump -i eth0 -w capture.pcap -s 0 -C 100            # save to rotating files for later

Everything you need is visible with tcpdump itself and its BPF filters, without any GUI tool:

sudo tcpdump -i any -nn 'tcp[tcpflags] & tcp-rst != 0'           # only RSTs (who is resetting?)
sudo tcpdump -i any -nn 'tcp[tcpflags] & (tcp-syn) != 0'         # only SYNs (connection attempts)
sudo tcpdump -i any -nn 'udp port 53'                           # DNS queries and answers
sudo tcpdump -i any -nn -A 'tcp port 80 and tcp[((tcp[12]&0xf0)>>2):4]=0x47455420'   # HTTP GET requests
sudo tcpdump -i any -nn 'tcp port 443 and tcp[((tcp[12]&0xf0)>>2)]=0x16'   # TLS handshake records

To see a complete stream (the equivalent of “Follow TCP Stream”), tcpdump -A -nn 'host X and port Y' plus -s0 prints all of the text content. To go through a capture later, read the same capture.pcap file back with tcpdump -r capture.pcap; again, no GUI tool needed. (TLS content is encrypted anyway, and no tool can read it without the keys.)

curl for everything HTTP

curl -sv https://api.example.com/health                       # handshake, headers, redirects
curl -so /dev/null -w 'dns:%{time_namelookup} tcp:%{time_connect} tls:%{time_appconnect} ttfb:%{time_starttransfer} total:%{time_total} code:%{http_code}\n' https://api.example.com/
curl --resolve api.example.com:443:10.0.3.7 https://api.example.com/   # bypass DNS, hit one backend
curl -H 'Host: shop.example.com' http://10.0.3.7/                      # test virtual hosting directly
curl -4 https://example.com/      # force IPv4 (and -6 for IPv6)
curl -x http://proxy:3128 https://example.com                          # through a proxy

Common scenarios

SymptomLikely causesWhat to run
“Could not resolve host”DNS: wrong resolver, wrong name, search domain, private DNSdig name, dig @8.8.8.8 name, cat /etc/resolv.conf
Connection refusedNothing listens on that port, or only on 127.0.0.1ss -tulpn on the server
Connection timed outFirewall or security group, routing, host downtraceroute -T -p 443, tcpdump on both ends
Works with the IP, not the nameDNS, virtual host or SNIcurl --resolve
Small pages load, big ones hangMTU black hole (often behind a VPN)ping -M do -s 1472, tracepath
Slow, but no errorsPacket loss, bufferbloat, slow DNS, too many handshakesmtr, curl -w, ss -ti (retrans)
Drops now and thenNAT or LB idle timeout, one bad IP among several in DNS, port exhaustionLB logs, keepalive, ss -s
TLS errorSystem clock, incomplete chain, wrong name, unknown CA, SNIopenssl s_client, date
502 or 504 from the load balancerUnhealthy backend, wrong port, timeout too shortLB health checks and logs, curl straight to the backend
Fails only from a container or podInternal DNS, NetworkPolicy, bound to localhost, overlay MTUTest from inside the same namespace (nsenter or kubectl debug)