tcpdump (1/2)
tcpdump captures packets on an interface and either prints a one-line decode per packet or saves the raw packets to a pcap file. It is the standard CLI sniffer on Linux and macOS (built in on macOS, with Apple extras); live capture needs root or sudo. Lab: client 192.168.142.10, web server 192.168.142.20.
122.1 Why is it used?
- Quick troubleshooting on a server with no GUI: see the handshake, the HTTP request, ARP, DNS.
- Save a capture with -w on production and analyse it later in Wireshark.
- The capture filter runs in the kernel (BPF), so even busy servers can be captured with low overhead.
- Available on almost every Linux box, container image and macOS by default.
122.2 How it works
- Step 1
1.tcpdump starts on eth0 with filter 'host 192.168.142.20 and port 80'
- Step 2
2.Client sends a SYN through the switch to the web server
- Step 3
3.Kernel BPF filter matches the packet and hands one copy to tcpdump
- Step 4
4.Server's SYN-ACK comes back and is copied too
- Step 5
5.Client's DNS query to the gateway does not match: no copy at all
- Step 6
6.tcpdump prints one line per packet or writes it to a .pcap file
The original packets are never delayed; tcpdump only gets copies
122.3 Interface and capture source
- -D lists interfaces, -L link types (EN10MB = Ethernet), -J timestamp types.
- --time-stamp-precision=nano prints nine digits after the second.
- -i any captures on all interfaces and shows In/Out on every line (Linux cooked v2).
- -Q in keeps only incoming packets; -p keeps the NIC out of promiscuous mode.
- -I (monitor mode) is for Wi-Fi only: the lab's eth0 gave a real 'not supported' error.
Terminal
tcpdump -Dtcpdump -i eth0 -Ltcpdump -i eth0 -Jtcpdump -i eth0 -nn -c 1 icmp \ --time-stamp-precision=nanotcpdump -i any -nn -c 2 icmptcpdump -i eth0 -Q in -nn -c 1 icmpOutput
$ tcpdump -i eth0 -LData link types for eth0 (use option -y to set): EN10MB (Ethernet) DOCSIS (DOCSIS) (printing not supported)$ tcpdump -i eth0 -JTime stamp types for eth0 (use option -j to set): host (Host)$ tcpdump ... --time-stamp-precision=nano02:42:12.716285436 IP 192.168.142.10 > 192.168.142.20: ICMP echo request, ...$ tcpdump -i any -nn -c 2 icmplistening on any, link-type LINUX_SLL2 (Linux cooked v2), ...... eth0 Out IP 192.168.142.10 > 192.168.142.20: ICMP echo request, ...... eth0 In IP 192.168.142.20 > 192.168.142.10: ICMP echo reply, ...$ tcpdump -i eth0 -Q in -nn -c 1 icmp... IP 192.168.142.20 > 192.168.142.10: ICMP echo reply, ...122.4 Stop and count: -c
- -c 6 stops after six packets: here the full TCP handshake plus the HTTP request and reply.
- Flags [S] = SYN, [S.] = SYN-ACK, [.] = ACK, [P.] = PUSH with data, [F.] = FIN.
Terminal
tcpdump -i eth0 -nn -c 6 \ 'host 192.168.142.20 and port 80' &curl -s http://192.168.142.20/Output
... 192.168.142.10.39762 > 192.168.142.20.80: Flags [S], ...... 192.168.142.20.80 > 192.168.142.10.39762: Flags [S.], ...... 192.168.142.10.39762 > 192.168.142.20.80: Flags [.], ack 1, ...... Flags [P.], seq 1:78, ack 1, ... length 77: HTTP: GET / HTTP/1.1... Flags [P.], seq 1:186, ack 78, ... length 185: HTTP: HTTP/1.0 200 OK6 packets captured12 packets received by filter0 packets dropped by kernel122.5 Reading and writing files
- -w writes raw packets; --print also shows the decode at the same time.
- -r reads the file back; -r - reads from stdin, so pipes work.
- --count prints only the number of matching packets.
Terminal
tcpdump -i eth0 -w /tmp/web.pcap --print \ -nn -c 14 'host 192.168.142.20' &curl -s http://192.168.142.20/ping -c 1 -q 192.168.142.20file /tmp/web.pcaptcpdump --count -r /tmp/web.pcaptcpdump --count -r /tmp/web.pcap 'tcp port 80'cat /tmp/web.pcap | tcpdump -r - -nn icmpOutput
14 packets captured$ file /tmp/web.pcap/tmp/web.pcap: pcap capture file, microsecond ts (little-endian) ...$ tcpdump --count -r /tmp/web.pcap14 packets$ tcpdump --count -r /tmp/web.pcap 'tcp port 80'12 packets$ cat /tmp/web.pcap | tcpdump -r - -nn icmpreading from file -, link-type EN10MB (Ethernet), ...... IP 192.168.142.10 > 192.168.142.20: ICMP echo request, ...... IP 192.168.142.20 > 192.168.142.10: ICMP echo reply, ...122.6 File rotation for long captures
- -G 2 starts a new file every 2 seconds; use a strftime name (%H%M%S) so files are not overwritten.
- -W 3 with -G stops after 3 files; -z gzip compresses each closed file.
- -C 1 starts a new file every 1,000,000 bytes; with -W 3 it becomes a ring buffer (ring.pcap0..2).
- -U flushes every packet to the file immediately.
Terminal
cd /tmptcpdump -i eth0 -w 'rot-%H%M%S.pcap' \ -G 2 -W 3 -z gzip icmptcpdump -i eth0 -w ring.pcap \ -C 1 -W 3 'host 192.168.142.20'tcpdump -i eth0 -w /tmp/u.pcap -U -c 2 icmpOutput
Maximum file limit reached: 349 packets captured$ ls /tmp | grep rot-rot-024249.pcap.gzrot-024251.pcap.gzrot-024253.pcap.gz517 packets captured$ ls -l /tmp | grep ring... 1003004 Nov 9 02:43 ring.pcap0... 322180 Nov 9 02:43 ring.pcap1... 1033165 Nov 9 02:43 ring.pcap2122.7 Names, numbers and output detail
- Default: IPs are resolved to names; -n turns DNS lookups off (-nn also ports), -N drops the domain part.
- ACK numbers are relative by default (ack 1); -S shows absolute sequence numbers.
- -v shows TTL, IP ID, flags and checksum; -e shows MAC addresses.
- -A prints the payload in ASCII (the GET request is readable); -X prints hex + ASCII.
- 'tcp[13] & 8 != 0' matches packets with the PSH flag, i.e. packets carrying data.
Terminal
tcpdump -r /tmp/web.pcap -c 1tcpdump -r /tmp/web.pcap -c 1 -ntcpdump -r /tmp/web.pcap -c 1 -Ntcpdump -r /tmp/web.pcap -c 3 -nn -SOutput
$ tcpdump -r /tmp/web.pcap -c 1... IP client.39774 > n122-web.n122-lan.80: Flags [S], ...$ tcpdump -r /tmp/web.pcap -c 1 -n... IP 192.168.142.10.39774 > 192.168.142.20.80: Flags [S], ...$ tcpdump -r /tmp/web.pcap -c 1 -N... IP client.39774 > n122-web.80: Flags [S], ...$ tcpdump -r /tmp/web.pcap -c 3 -nn -S... Flags [S], seq 3155756839, ...... Flags [S.], seq 2308821435, ack 3155756840, ...... Flags [.], ack 2308821436, win 63, ...Terminal
tcpdump -r /tmp/web.pcap -nn -v -c 1tcpdump -r /tmp/web.pcap -nn -e -c 1tcpdump -r /tmp/web.pcap -nn -A \ 'tcp[13] & 8 != 0'tcpdump -r /tmp/web.pcap -nn -X -c 1 icmpOutput
$ tcpdump -r /tmp/web.pcap -nn -v -c 1... IP (tos 0x0, ttl 64, id 12033, offset 0, flags [DF], proto TCP (6), length 60) 192.168.142.10.39774 > 192.168.142.20.80: Flags [S], cksum 0x9d9e (incorrect -> 0xcbc7), ...$ tcpdump -r /tmp/web.pcap -nn -e -c 1... fe:46:45:b6:5f:3c > c2:cd:8c:24:da:9a, ethertype IPv4 (0x0800), length 74: ...$ tcpdump -r /tmp/web.pcap -nn -A 'tcp[13] & 8 != 0'... Flags [P.], ... length 77: HTTP: GET / HTTP/1.1E.../.@.@.n.....>..At..GET / HTTP/1.1Host: 192.168.142.20User-Agent: curl/8.7.1Accept: */*$ tcpdump -r /tmp/web.pcap -nn -X -c 1 icmp... IP 192.168.142.10 > 192.168.142.20: ICMP echo request, ... 0x0000: 4500 0054 57b7 4000 4001 4582 c0a8 8e0a E..TW.@.@.E..... 0x0010: c0a8 8e14 0800 c8f4 20e4 0001 1a33 f16a .............3.j 0x0020: 0000 0000 40b5 0300 0000 0000 1011 1213 ....@...........122.8 Timestamp display
| Option | Shows | Lab example |
|---|---|---|
| -t | No timestamp | IP 192.168.142.10.39774 > ... |
| -tt | Epoch seconds | 1794192154.236440 |
| -ttt | Delta from previous line | 00:00:00.000089 (SYN-ACK after 89 us) |
| -tttt | Full date and time | 2026-11-09 02:42:34.236440 |
| -ttttt | Time since the first line | 00:00:00.000111 |
122.9 Forcing a decoder: -T type
- tcpdump picks the decoder by port, so DNS on port 5300 shows only 'UDP, length 52'.
- -T domain decodes it as DNS; others: vxlan, cnfp (NetFlow), radius, resp (Redis), rtp, snmp, tftp, ptp.
Terminal
tcpdump -r /tmp/dns.pcap -nntcpdump -r /tmp/dns.pcap -nn -T domainOutput
$ tcpdump -r /tmp/dns.pcap -nn... IP 192.168.142.10.52582 > 192.168.142.20.5300: UDP, length 52$ tcpdump -r /tmp/dns.pcap -nn -T domain... IP 192.168.142.10.52582 > 192.168.142.20.5300: 62415+ [1au] A? example.com. (52)122.10 Filter compilation and debugging
- -d prints the compiled BPF program; -y EN10MB compiles for Ethernet without an interface.
- -O disables the optimizer (22 instructions instead of 20); -ddd decimal, -dd a C fragment.
- -F reads the filter from a file; a typo ('prot') gives a syntax error.
Terminal
tcpdump -d -y EN10MB 'tcp port 80' | wc -ltcpdump -O -d -y EN10MB 'tcp port 80' | wc -ltcpdump -ddd -y EN10MB 'icmp'echo 'icmp and host 192.168.142.20' > /tmp/f.txttcpdump -r /tmp/web.pcap -nn -F /tmp/f.txttcpdump -d -y EN10MB 'tcp prot 80'Output
$ tcpdump -d ... | wc -l20$ tcpdump -O -d ... | wc -l22$ tcpdump -ddd -y EN10MB 'icmp'640 0 0 1221 0 3 204848 0 0 2321 0 1 16 0 0 2621446 0 0 0$ tcpdump -r /tmp/web.pcap -nn -F /tmp/f.txt... IP 192.168.142.10 > 192.168.142.20: ICMP echo request, ...... IP 192.168.142.20 > 192.168.142.10: ICMP echo reply, ...$ tcpdump -d -y EN10MB 'tcp prot 80'tcpdump: can't parse filter expression: syntax error122.11 Version and signals while running
- --version shows tcpdump, libpcap and OpenSSL (so ESP decryption is built in).
- kill -USR1 prints the counters without stopping; USR2 flushes the buffer; INT (Ctrl-C) stops and prints the summary.
Terminal
tcpdump --versiontcpdump -i eth0 -w /tmp/sig.pcap icmp \ > /tmp/sig.log 2>&1 &ping -c 3 -i 0.3 -q 192.168.142.20kill -USR1 $(pidof tcpdump)ping -c 2 -q 192.168.142.20kill -INT $(pidof tcpdump)cat /tmp/sig.logOutput
$ tcpdump --versiontcpdump version 4.99.4libpcap version 1.10.4 (with TPACKET_V3)OpenSSL 3.3.0 9 Apr 2024$ cat /tmp/sig.logtcpdump: listening on eth0, link-type EN10MB (Ethernet), ...tcpdump: 2 packets captured, 6 packets received by filter, 0 packets dropped by kernel8 packets captured10 packets received by filter0 packets dropped by kernel122.12 Capture filter primitives
| Primitive | Example | Lab result (web.pcap) |
|---|---|---|
| host / src host / dst host | src host 192.168.142.20 | Only server -> client packets |
| net | net 192.168.142.0/24 or net 192.168.142 | 14 packets |
| port / dst port | tcp dst port 80 | 7 packets |
| portrange | portrange 1-1023 | 12 packets |
| greater / less | greater 100 / less 70 | 2 data packets / 7 packets |
| ether | ether src $MAC | 8 packets |
| arp host | arp host 192.168.142.20 | ARP request + reply |
| gateway / mask | gateway router1, net 192.168.1.0 mask 255.255.255.0 | (not run) |
Terminal
# tcpdump --count -r /tmp/web.pcap <filter>net 192.168.142.0/24 # 14 packetsnet 192.168.142 # 14 packetstcp dst port 80 # 7 packetsportrange 1-1023 # 12 packetsless 70 # 7 packetsether src $MAC # 8 packets# live, after: ip neigh flush alltcpdump -i eth0 -nn arp host 192.168.142.20Output
$ tcpdump -i eth0 -nn arp host 192.168.142.20... ARP, Request who-has 192.168.142.20 tell 192.168.142.10, length 28... ARP, Request who-has 192.168.142.20 tell 192.168.142.10, length 28... ARP, Reply 192.168.142.20 is-at c2:cd:8c:24:da:9a, length 28122.13 Option cheat sheet
| Group | Options | What they do |
|---|---|---|
| Interface | -D, -i eth0 / any, -L, -y, -p, -I, -Q in|out | List/choose interface, link type, no promisc, Wi-Fi monitor, direction |
| Buffer / size | -B KiB, -s 96, --immediate-mode | Kernel buffer, snapshot length, no buffering |
| Stop / count | -c N, --count | Stop after N packets; print only the count |
| Files | -w, -r (- = stdout/stdin), -V list, --print, -U | Write/read pcap, many files, save + decode, flush every packet |
| Rotation | -C MB, -G sec, -W n, -z gzip | Size/time rotation, file limit or ring, compress |
| Names | -n / -nn, -N, -S | No DNS lookup, no domain, absolute TCP seq |
| Detail | -q, -v/-vv/-vvv, -e, -A, -x/-X/-XX, -#, -K, -l | Short/verbose, MAC header, ASCII/hex payload, number, no checksum check, line buffered |
| Time | -t, -tt, -ttt, -tttt, -ttttt | None, epoch, delta, full date, since first |
| Debug | -d, -dd, -ddd, -O, -F file | Compiled BPF, C/decimal, no optimizer, filter from file |
| Other | -T type, -E, -M, -m, -Z user | Force decoder, ESP decrypt, TCP-MD5, SNMP MIB, drop root |
| macOS option | Meaning |
|---|---|
| -i pktap,lo0,en0 / -i all | Several interfaces together, saved with metadata |
| -k NPD | Process name, PID and direction on every packet |
| -P | Save as pcapng |
| -Q 'proc = curl' / 'dir = in' | Metadata filter (PKTAP or pcapng only), combine with and/or/not |
122.14 Common mistakes
- Running tcpdump over SSH with no filter: it captures its own SSH output and scrolls forever. Add 'not port 22'.
- Reading 'cksum incorrect' as corruption: on outgoing packets the NIC fills the checksum later (offload); -K skips the check.
- Forgetting -nn: slow output because of DNS lookups, and the lookups themselves add traffic.
- Long captures without -C/-G/-W: the disk fills up.
122.15 Interview tips
- Safe production capture: tight host + port filter, -nn, limit with -c or -C/-G + -W, write with -w, analyse in Wireshark.
- Always check '0 packets dropped by kernel' after the capture.
- Know the TCP flags in tcpdump output: [S], [S.], [.], [P.], [F.], [R].
- -i any shows direction (In/Out) but uses the Linux cooked link type, not Ethernet.