cheat-sheets

tcpdump

tcpdump captures and prints network packets from the command line using BPF filter expressions. Use it to confirm whether traffic reaches a host, to debug DHCP, DNS or VLAN problems, and to save a capture for Wireshark and tshark.

Documentation: tcpdump(1) · pcap-filter(7) Project: tcpdump and libpcap

Verification: options and filter primitives were checked against the tcpdump and pcap-filter man pages in Ubuntu 24.04. Captures need root (sudo) and were not run against live traffic here.

Basics #

COMMANDDESCRIPTION
sudo tcpdump -DList capture interfaces
sudo tcpdump -i <IF>Capture on one interface
sudo tcpdump -i anyAll interfaces (Linux)
sudo tcpdump -nNo name resolution of hosts
sudo tcpdump -nnNo name or port-name resolution
sudo tcpdump -c 20Stop after 20 packets
sudo tcpdump -qQuieter, shorter lines
sudo tcpdump -eShow link-level headers (MAC addresses, VLAN tags)
sudo tcpdump -v / -vvMore detail (TTL, flags, checksums)
sudo tcpdump -APrint payload as ASCII
sudo tcpdump -XPayload as hex and ASCII
sudo tcpdump -s 0Capture whole packets (the default snapshot length is already 262144 bytes on current versions)
sudo tcpdump -lLine-buffered output, for piping to grep

Filters #

COMMANDDESCRIPTION
sudo tcpdump -nn host 192.0.2.10Traffic to or from a host
sudo tcpdump -nn src host 192.0.2.10From a host
sudo tcpdump -nn dst host 192.0.2.10To a host
sudo tcpdump -nn net 192.0.2.0/24A network
sudo tcpdump -nn port 443A port, either direction
sudo tcpdump -nn portrange 8000-8100A port range
sudo tcpdump -nn tcp / udp / icmpOne protocol
sudo tcpdump -nn host 192.0.2.10 and port 22Combine with and
sudo tcpdump -nn 'port 53 or port 853'Combine with or
sudo tcpdump -nn not port 22Exclude (handy over SSH)
sudo tcpdump -nn vlan 20Frames tagged with a VLAN ID
sudo tcpdump -nn ether host 00:00:5e:00:53:01A MAC address
sudo tcpdump -nn arpARP only
sudo tcpdump -nn 'tcp[tcpflags] & (tcp-syn) != 0'TCP SYN packets
sudo tcpdump -nn 'tcp[tcpflags] & (tcp-rst) != 0'TCP resets

Quote any filter that contains brackets, parentheses or !.

Save and Read #

COMMANDDESCRIPTION
sudo tcpdump -i <IF> -w capture.pcapWrite raw packets to a file
sudo tcpdump -i <IF> -w capture.pcap -C 100 -W 5Rotate at 100 MB, keep 5 files
sudo tcpdump -i <IF> -w 'cap-%F-%H%M.pcap' -G 3600New file every hour (-G seconds, time format in the name)
tcpdump -nn -r capture.pcapRead a file (no root needed)
tcpdump -nn -r capture.pcap port 53Apply a filter when reading
sudo tcpdump -i <IF> -z gzip -w capture.pcap -G 3600Compress rotated files (needs -G or -C)

Common Tasks #

Watch DHCP exchanges:

bash
sudo tcpdump -nn -e -i eth0 'port 67 or port 68'

Watch DNS queries to and from one resolver:

bash
sudo tcpdump -nn -i any 'host 192.0.2.53 and port 53'

Check ping reaches the host:

bash
sudo tcpdump -nn -i eth0 icmp

See traffic without exposing your own SSH session:

bash
sudo tcpdump -nn -i eth0 'not port 22'

Troubleshooting #

SYMPTOMFIX
permission deniedRun with sudo, or grant the binary capture capabilities
Nothing capturedWrong interface (-D), or the traffic is on a bridge or VLAN sub-interface
dropped by kernel at exitCapture is too slow: add -n, use -w, filter, or raise -B <KiB> buffer size
Capture inside a containerRun it in the container’s network namespace: sudo nsenter -t <PID> -n tcpdump -nn

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.