TCPdump

Modified: 2025-11-12

tcpdump is a powerful command‑line packet analyzer that lets you capture and inspect network traffic on Unix‑like systems (Linux, macOS, BSD). It works by putting the network interface into promiscuous mode and dumping raw packets, which you can then filter, display, or save for later analysis.


Things to keep in mind


Quick‑Reference Cheat Sheet for tcpdump

Category

Command

What it Does

Handy Tips

Basic capture sudo tcpdump Starts sniffing on the default interface, printing a one‑line summary of each packet. Add -i <iface> to choose a specific NIC (e.g., -i eth0). Limit packet count sudo tcpdump -c 20 Stops after 20 matching packets. Great for quick checks; prevents runaway captures. Save to file sudo tcpdump -w capture.pcap Writes raw packets to capture.pcap (binary pcap format). Open later with Wireshark or tcpdump -r. Read from file sudo tcpdump -r capture.pcap Prints a summary of packets stored in a pcap file. Combine with -X/-A for payload view. Full‑packet snaplen sudo tcpdump -s 0 Captures the entire packet (no truncation). Use when you need the full payload (e.g., HTTP headers). ASCII payload sudo tcpdump -A Shows packet data as printable ASCII characters. Pair with -s 0 for complete text. Hex + ASCII sudo tcpdump -X Hex dump plus ASCII on the same line. Helpful for binary protocols. Verbose output -v, -vv, -vvv Increases detail level (e.g., TTL, window size, options). -vvv can expose IP options and TCP flags. Filter by protocol tcpdump icmp, tcpdump udp, tcpdump tcp Shows only the specified protocol. Combine with other filters (and, or). Port filtering tcpdump port 22, tcpdump port 443 Captures traffic where either source or destination port is 22 (SSH), 443 (https). Use src port or dst port for direction‑specific captures. Host filtering tcpdump host 192.168.1.10 All packets to or from that IP address. src host / dst host for one‑way traffic. Network filtering tcpdump net 10.0.0.0/8 Any packet whose IP belongs to that subnet. Useful for monitoring a whole LAN segment. Expression combos tcpdump src host 10.0.0.5 and dst port 80 Traffic from 10.0.0.5 to port 80. Parentheses group complex logic: (src net 10.0.0.0/24) and (dst port 53 or dst port 5353). Capture HTTP (unencrypted) and HTTPS (Encrypted) sudo tcpdump -A -s 0 -c 10 port 80 or port 443 Shows first ten HTTP or HTTPS packets with readable payload. Remember most modern traffic is HTTPS (port 443). Capture DNS sudo tcpdump -n -vvv -s 0 port 53 Shows DNS (port 53) queries/responses in detail. -n avoids DNS lookups on captured IPs (keeps output fast). Capture only SYN packets tcpdump 'tcp[tcpflags] & tcp-syn != 0' Lists connection attempts (handshake starts). Useful for spotting scans. Exclude traffic tcpdump not port 22 Captures everything except SSH. Combine with other filters (and, or). Timestamp formats -tttt (human‑readable)-t (default, relative)-tt (epoch) Controls how timestamps appear. -tttt is great for logs. Interface list tcpdump -D Prints all interfaces tcpdump can listen on. Choose the right one with -i. Promiscuous mode toggle -p Disables promiscuous mode (captures only traffic destined for the host). Useful on switched networks where you only need local traffic. Colorized output (if compiled with libpcap ≥ 1.9) --color Highlights protocol fields. Makes reading easier, especially on long runs.

Example Workflows

Capture the first 20 HTTP requests and save them for later analysis

sudo tcpdump -i eth0 -c 20 -w http_first20.pcap 'tcp port 80'

Live‑view DNS queries in a readable form

sudo tcpdump -n -vvv -s 0 -A 'udp port 53'

Spot a possible port‑scan on your server

sudo tcpdump -i eth0 -nn -e 'tcp[tcpflags] & (tcp-syn|tcp-ack) = tcp-syn' and not src host <your‑server‑IP>

Shows SYN packets from hosts other than yourself.

Debug an HTTPS handshake (you won’t see the encrypted payload, but you can see the TLS record types)

sudo tcpdump -i eth0 -nn -vv -s 0 'tcp port 443'


Quick “What‑If” Thoughts (Balanced Discourse)


Getting Started in One Line

If you just want a quick glance at what’s happening on your primary NIC:

sudo tcpdump -i $(ip route get 8.8.8.8 | awk '{print $5; exit}') -nn -c 10

Explanation:


TL;DR