basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    Cheat sheetLinuxtcpdump

    tcpdump cheat sheet: flags, BPF filters, and .pcap capture

    Everyday launch flags, BPF filters for addresses, protocols, and tcp[13] bits, safe ring-buffer recording, and reading .pcap files. Verified on tcpdump 4.99.4 and libpcap 1.10.4.

    Core launch flags

    Flag What it does
    -D list available interfaces and their numbers
    -i eth0 / -i any capture on one interface or all at once (any uses SLL2, Limits of -i any)
    -nn skip resolving IPs and port numbers to names (Always pass -nn to tcpdump)
    -c 50 stop after capturing 50 packets
    -e show the L2 header: MAC addresses, ethertype, VLAN tag
    -v / -vv verbose IP/TCP header: ttl, id, DF, checksums
    -tttt / -ttt full date and time / delta from the previous packet
    -l line-buffer stdout when piping into grep or tee
    -S print absolute seq/ack numbers instead of relative ones
    -Q in / -Q out capture incoming only or outgoing only
    -A / -X print packet body in ASCII / hex + ASCII
    -s 0 / -s 96 snapshot length (0 — full frame, 96 — headers only)
    -d compile the BPF filter, print bytecode, and exit without capturing

    BPF filters: hosts, ports, and protocols

    Always wrap the expression in single quotes '…' so bash doesn’t intercept parentheses or operators.

    Filter What it matches
    'host 203.0.113.10' all traffic to and from the host (IPv4 or IPv6)
    'src host 198.51.100.20' packets from this source IP only
    'net 10.0.0.0/24' traffic for an entire CIDR subnet
    'tcp port 443' TCP port 443 in both directions
    'dst port 53' queries sent to port 53 (UDP and TCP)
    'tcp portrange 8000-8099' a TCP port range
    'host 203.0.113.10 and (port 80 or port 443)' host plus either port (parentheses required)
    'not port 22' everything except your SSH session
    'arp or icmp' local ARP lookups and all IPv4 ICMP
    'icmp6' ICMPv6: ping -6, NDP, RA, and Packet Too Big
    'vlan and tcp port 80' HTTP inside an 802.1Q VLAN (vlan must come first, Order of vlan in a filter)
    'vlan 20 and host 10.0.20.5' host strictly inside VLAN ID 20
    'ether host bc:24:11:af:27:2b' frames by MAC address (specific interface only, not -i any)

    TCP flags, ICMP, and payloads (IPv4)

    Byte-offset expressions like tcp[…] and icmp[…] work only for IPv4 in libpcap (for plain IPv6 without extension headers, the TCP flags byte sits at ip6[53]).

    Filter What it matches
    'tcp[13] == 0x02' pure SYN only (new connection attempts)
    'tcp[13] == 0x12' SYN+ACK replies only
    'tcp[13] & 0x04 != 0' any RST or RST+ACK reset
    'tcp[tcpflags] & (tcp-syn|tcp-rst|tcp-fin) != 0' connection starts, closes, and resets without payload noise
    'tcp[13] == 0x18' PSH+ACK packets carrying data
    'icmp[icmptype] == 3 or icmp[icmptype] == 11' Destination Unreachable and Time Exceeded errors (no ping)
    'icmp[0] == 3 and icmp[1] == 4' ICMP Fragmentation Needed only (MTU checks)
    'tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420' HTTP requests starting with GET
    'tcp port 443 and (tcp[((tcp[12:1] & 0xf0) >> 2):1] = 0x16) and (tcp[(((tcp[12:1] & 0xf0) >> 2) + 5):1] = 0x01)' TLS ClientHello packets (contain plaintext SNI)

    Writing .pcap, ring buffers, and reading back

    Command What it does
    sudo tcpdump -nn -i eth0 -c 1000 -w /tmp/cap.pcap 'tcp port 443' write 1000 packets to disk without live terminal decoding
    sudo tcpdump -nn -i eth0 -B 16384 -C 50 -W 10 -w /tmp/ring.pcap 'port 443' ring buffer: 10 files of 50 million bytes with a 16 MiB kernel buffer (Ring buffer for .pcap)
    sudo tcpdump -nn -i eth0 -G 300 -W 12 -z gzip -w '/tmp/cap_%H%M%S.pcap' 'port 443' 12 five-minute files compressed with gzip, then exit automatically
    sudo tcpdump -nn -Z root -i eth0 -w /root/cap.pcap 'port 443' write into /root/ without dropping privileges to the tcpdump user
    tcpdump -nn -r /tmp/cap.pcap read a saved .pcap without sudo
    tcpdump -nn -r /tmp/cap.pcap 'tcp[13] & 0x04 != 0' filter an existing .pcap with a new BPF expression
    tcpdump -nn -A -r /tmp/cap.pcap -c 20 'tcp port 80' print the first 20 HTTP packets from a file in ASCII

    Docker containers and pairing with other tools

    Command What it does
    sudo nsenter -t $(docker inspect -f '{{.State.Pid}}' traefik) -n tcpdump -nn -i any 'port 8080' capture inside a container without installing tools in the image (Linux network stack)
    sudo tcpdump -l -nn -i eth0 -A 'tcp port 80' | grep -E '^(GET|POST|Host:)' live view of HTTP requests and Host headers
    ssh web1 "sudo tcpdump -nn -U -i eth0 -w - 'tcp port 443 and not port 22'" | wireshark -k -i - stream a remote server’s capture straight into local Wireshark (-U flushes every packet)