tcpdump Recipes
When logs and ss don't explain a network problem, look at the packets. tcpdump captures traffic on an interface and filters it with BPF expressions; it settles arguments like "the request never reached us" vs "we answered and the client ignored it" in seconds. The protocols themselves are in Computer Networking (TCP Connections for the handshake); here you learn to capture safely on a server and read the result.
Capturing needs root (or CAP_NET_RAW), which the sandbox doesn't have. Reading a saved capture doesn't, so the verified sessions below read a small capture file, written by a Python script in the same session, with the same tcpdump -r you would use on a real .pcap.
Capture recipes (sample sessions)
| Goal | Command |
|---|---|
| traffic to/from one host on one port | sudo tcpdump -i any -nn host 10.0.0.30 and port 5432 |
| only new TCP connections (SYNs) | sudo tcpdump -i eth0 -nn 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn' |
| resets (who is refusing?) | sudo tcpdump -i any -nn 'tcp[tcpflags] & tcp-rst != 0' |
| DNS queries leaving the server | sudo tcpdump -i any -nn udp port 53 |
| ICMP (unreachables, fragmentation needed) | sudo tcpdump -i any -nn icmp |
| HTTP payload in ASCII | sudo tcpdump -i lo -nn -A -s 0 port 8080 |
| save for Wireshark, rotating 10 files of 100 MB | sudo tcpdump -i eth0 -nn -w /var/tmp/cap.pcap -C 100 -W 10 port 443 |
| stop after N packets | -c 1000 |
Options that matter on a production box:
-nn: no DNS or port-name lookups. Without it tcpdump resolves every address it prints, which is slow and creates DNS traffic of its own.- Always filter: capturing everything on a busy interface costs CPU and disk, and drops packets. The
N packets dropped by kernelline at the end tells you whether you lost some. -wfiles contain payloads: passwords in plain-text protocols, session cookies, personal data. Treat them as secrets, delete them after the investigation.-i anycaptures all interfaces (Linux "cooked" headers); pick the real interface when you need Ethernet details.- Encrypted traffic (TLS) shows the handshake and sizes, not the content. That is usually enough: you can see who connected, who reset, who stopped answering.
Reading a capture
The session builds a capture of four short conversations to 10.0.0.20 (an app server) from 10.0.0.5, then reads it:
python3 - <<'EOF'
import struct
def tcp(src, dst, sport, dport, flags, seq, ack, payload=b""):
t = struct.pack("!HHIIBBHHH", sport, dport, seq, ack, 5 << 4, flags, 64240, 0, 0) + payload
ip = struct.pack("!BBHHHBBH4s4s", 0x45, 0, 20 + len(t), 1, 0x4000, 64, 6, 0, bytes(src), bytes(dst))
return b"\x02\x00\x00\x00\x00\x02" + b"\x02\x00\x00\x00\x00\x01" + b"\x08\x00" + ip + t
C, S = [10, 0, 0, 5], [10, 0, 0, 20]
SYN, SYNACK, ACK, PSHACK, RSTACK = 0x02, 0x12, 0x10, 0x18, 0x14
req = b"GET /health HTTP/1.1\r\nHost: app\r\n\r\n"
pkts = [
(0.000, tcp(C, S, 40001, 443, SYN, 100, 0)), # 1. HTTPS: normal handshake
(0.001, tcp(S, C, 443, 40001, SYNACK, 900, 101)),
(0.001, tcp(C, S, 40001, 443, ACK, 101, 901)),
(1.000, tcp(C, S, 40002, 8080, SYN, 200, 0)), # 2. port 8080: refused
(1.000, tcp(S, C, 8080, 40002, RSTACK, 0, 201)),
(2.000, tcp(C, S, 40003, 5432, SYN, 300, 0)), # 3. PostgreSQL: no answer, retransmits
(3.000, tcp(C, S, 40003, 5432, SYN, 300, 0)),
(5.000, tcp(C, S, 40003, 5432, SYN, 300, 0)),
(6.000, tcp(C, S, 40004, 80, SYN, 400, 0)), # 4. HTTP request with payload
(6.001, tcp(S, C, 80, 40004, SYNACK, 700, 401)),
(6.001, tcp(C, S, 40004, 80, ACK, 401, 701)),
(6.002, tcp(C, S, 40004, 80, PSHACK, 401, 701, req)),
]
with open("incident.pcap", "wb") as f:
f.write(struct.pack("<IHHiIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1))
for ts, data in pkts:
sec, usec = 1767225600 + int(ts), round((ts % 1) * 1e6)
f.write(struct.pack("<IIII", sec, usec, len(data), len(data)) + data)
EOF
echo "=== everything"
tcpdump -nn -r incident.pcap 2>/dev/null
echo "=== only connection attempts (SYN without ACK)"
tcpdump -nn -r incident.pcap 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn' 2>/dev/null | awk '{print $1, $3, $4, $5}'
echo "=== resets"
tcpdump -nn -r incident.pcap 'tcp[tcpflags] & tcp-rst != 0' 2>/dev/null | cut -d' ' -f2-
echo "=== the HTTP request, as text"
tcpdump -nn -A -r incident.pcap 'tcp port 80 and tcp[tcpflags] & tcp-push != 0' 2>/dev/null | grep -oE 'GET [^ ]+ HTTP/1.1|Host: [a-z]+' | uniq=== everything
00:00:00.000000 IP 10.0.0.5.40001 > 10.0.0.20.443: Flags [S], seq 100, win 64240, length 0
00:00:00.001000 IP 10.0.0.20.443 > 10.0.0.5.40001: Flags [S.], seq 900, ack 101, win 64240, length 0
00:00:00.001000 IP 10.0.0.5.40001 > 10.0.0.20.443: Flags [.], ack 1, win 64240, length 0
00:00:01.000000 IP 10.0.0.5.40002 > 10.0.0.20.8080: Flags [S], seq 200, win 64240, length 0
00:00:01.000000 IP 10.0.0.20.8080 > 10.0.0.5.40002: Flags [R.], seq 0, ack 201, win 64240, length 0
00:00:02.000000 IP 10.0.0.5.40003 > 10.0.0.20.5432: Flags [S], seq 300, win 64240, length 0
00:00:03.000000 IP 10.0.0.5.40003 > 10.0.0.20.5432: Flags [S], seq 300, win 64240, length 0
00:00:05.000000 IP 10.0.0.5.40003 > 10.0.0.20.5432: Flags [S], seq 300, win 64240, length 0
00:00:06.000000 IP 10.0.0.5.40004 > 10.0.0.20.80: Flags [S], seq 400, win 64240, length 0
00:00:06.001000 IP 10.0.0.20.80 > 10.0.0.5.40004: Flags [S.], seq 700, ack 401, win 64240, length 0
00:00:06.001000 IP 10.0.0.5.40004 > 10.0.0.20.80: Flags [.], ack 1, win 64240, length 0
00:00:06.002000 IP 10.0.0.5.40004 > 10.0.0.20.80: Flags [P.], seq 1:36, ack 1, win 64240, length 35: HTTP: GET /health HTTP/1.1
=== only connection attempts (SYN without ACK)
00:00:00.000000 10.0.0.5.40001 > 10.0.0.20.443:
00:00:01.000000 10.0.0.5.40002 > 10.0.0.20.8080:
00:00:02.000000 10.0.0.5.40003 > 10.0.0.20.5432:
00:00:03.000000 10.0.0.5.40003 > 10.0.0.20.5432:
00:00:05.000000 10.0.0.5.40003 > 10.0.0.20.5432:
00:00:06.000000 10.0.0.5.40004 > 10.0.0.20.80:
=== resets
IP 10.0.0.20.8080 > 10.0.0.5.40002: Flags [R.], seq 0, ack 201, win 64240, length 0
=== the HTTP request, as text
GET /health HTTP/1.1
Host: app
Reading the flags: [S] SYN, [S.] SYN-ACK (the . is ACK), [.] a pure ACK, [P.] data pushed, [R.] reset. The four stories in the capture are the four you will see in real incidents:
- Port 443:
S,S.,.— a healthy three-way handshake. Sequence numbers after the first packets are shown relative (ack 1) unless you add-S. - Port 8080: the server answers the SYN with
R.within microseconds. The host is up, nothing listens (or a REJECT rule). The client sees "Connection refused". - Port 5432: SYN, then the same SYN again after 1 s and 2 s more: the client's kernel retransmitting with exponential back-off because nothing came back. A firewall drops the packets somewhere (security group, nftables, the database host's own firewall). The client sees a timeout. If you capture on the database host and don't see these SYNs at all, the drop happens before it.
- Port 80: handshake, then the request itself, readable with
-Abecause it's plain HTTP.
Capturing on both ends
The decisive technique is capturing at both ends at the same time: if the client's capture shows SYNs leaving and the server's capture shows none arriving, the problem is between them, and you can stop debugging the application. If the server shows SYNs arriving and SYN-ACKs leaving, but the client never sees them, look at the return path (asymmetric routing, a stateful firewall that only saw one direction).
# Sample session: on the database host, while the app retries
$ sudo tcpdump -i ens5 -nn -c 5 'tcp port 5432 and host 10.0.1.21'
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on ens5, link-type EN10MB (Ethernet), snapshot length 262144 bytes
0 packets captured
0 packets received by filter
0 packets dropped by kernelZero packets on the database side while the app retries: the drop is in the network (in the cloud: the security group or network ACL), not on either host.
Go further: tcpdump and BPF Capture Filters goes further into the BPF language, including what the kernel actually runs.
Test yourself
A capture shows a client sending the same SYN three times, 1 s and then 2 s apart, with no reply. What does that indicate?
- The server refused the connection
- The SYNs (or the replies) are being dropped somewhere; the client retransmits with back-off and will time out
- A successful handshake
- A DNS failure
Why use -nn on a production server?
- It enables encryption
- It disables DNS and port-name lookups, which are slow and generate extra traffic
- It captures more packets
- It is required with -w
What should you keep in mind about .pcap files from a production server?
- They are always encrypted
- They can contain credentials and personal data; treat them as secrets and delete them after use
- They are too small to matter
- Wireshark can't read them