Basics
tcpdump needs root. Every command here assumes , and Ctrl+C stops a capture.
| Command | What it does |
|---|---|
| Capture on one interface | |
| Capture on all interfaces at once | |
| List interfaces you can capture on | |
| Skip DNS lookups, show raw IPs and ports (faster, and usually what you want) | |
| Stop after 100 packets | |
| Verbose output: TTL, ID, checksums ( for more) | |
| Quieter, shorter lines | |
| Version, plus the libpcap it was built with |
Host and Port Filters
Quote any filter that has more than one word so the shell leaves it alone.
| Command | What it does |
|---|---|
| Traffic to or from one IP | |
| Traffic coming from an IP | |
| Traffic going to an IP | |
| Traffic on a port, either direction | |
| Traffic from a source port | |
| Traffic to a destination port | |
| A range of ports | |
| Traffic to or from a whole subnet | |
| To one IP on one port |
Combining Filters
, , and combine primitives; parentheses group them. Always quote the whole expression, or the shell will grab the special characters first.
| Command | What it does |
|---|---|
| One host's traffic on port 80 | |
| Only the conversation between two hosts | |
| Either of two ports | |
| Everything except SSH (essential when capturing over SSH) | |
| From a host, minus its DNS lookups | |
| Grouping with parentheses |
Protocols
A bare protocol name is a filter on its own, and it combines with everything above.
| Command | What it does |
|---|---|
| TCP only | |
| UDP only | |
| Pings and other ICMP (watch a land) | |
| ARP requests and replies (who-has, is-at) | |
| DNS queries and answers | |
| All DNS, including large TCP responses | |
| HTTP over TCP | |
| The low-level chatter, useful when a host is unreachable |
Saving and Reading Captures
Write with , analyze later with or Wireshark. Terminal output stops while writing to a file; add to see a packet count tick up.
| Command | What it does |
|---|---|
| Write raw packets to a file | |
| Full packets, not truncated (do this for Wireshark) | |
| Capture 1000 packets, then stop | |
| Rotate at 100 MB, keep 5 files | |
| Read a capture back | |
| Filter while reading (filters work on files too) | |
| Cut a big capture down to one host |
The pcap format is Wireshark's native format: capture on the server with , the file to your machine, and open it in Wireshark for the graphical view.
Reading Packet Contents
| Command | What it does |
|---|---|
| Print payloads as ASCII (readable HTTP) | |
| Hex and ASCII side by side | |
| Full payloads; without long packets are cut off | |
| Hex including the ethernet header |
TLS traffic on 443 shows only gibberish with . That is the encryption working, not a tcpdump problem.
MAC Address Filters
For layer 2 problems: DHCP fights, ARP mysteries, finding a device with no IP yet.
| Command | What it does |
|---|---|
| Traffic to or from one MAC | |
| Frames sent by a MAC | |
| Show MAC addresses on every line | |
| Broadcast frames only |
BPF Syntax
Filters compile to Berkeley Packet Filter programs, and the bracket syntax reads raw bytes: is a byte within that protocol's header. The practical use is matching TCP flags.
| Filter | What it matches |
|---|---|
| Any packet with SYN set (new connections) | |
| SYN only, no ACK (inbound connection attempts) | |
| Same, spelled with a mask | |
| Resets (something refusing connections) | |
| Connection teardowns | |
| SYN again, by raw offset (13 is the flags byte) |
# Who is trying to open connections to this box, live:
sudo tcpdump -i any -n 'tcp[tcpflags] == tcp-syn'
# Check what a filter compiles to without capturing:
sudo tcpdump -d 'tcp[tcpflags] & tcp-syn != 0'Gotchas
- at the end means tcpdump could not keep up. Add , write to a file with instead of printing, and narrow the filter; raises the capture buffer.
- is usually the shell eating your quotes. Wrap any multi-word filter in single quotes, and check keywords (, not ).
- Capturing over SSH shows you your own SSH packets, which generate more packets, forever. Start every remote capture with .
- sees all interfaces but cannot use promiscuous mode, so it misses traffic not addressed to the host. Switch to the real interface once you know it.
- Old tcpdump versions truncated packets at 68 bytes by default. Modern ones capture full packets, but still makes the intent explicit for files headed to Wireshark.
- tcpdump shows packets arriving; it does not show what the firewall then drops. Compare with your iptables rules when packets arrive but the service never sees them, and check what is listening with ss.