basforge

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

    RU
    ManualLinuxtcpdump

    tcpdump basics: running captures, reading output, flags, and .pcap files

    From your first safe capture on an interface to reading every field of an output line, inspecting payloads, and writing .pcap dumps. Covers the flags that stop tcpdump from flooding the screen with your own SSH session or misleading you on checksums.

    What one output line shows

    Even a short command like sudo tcpdump -nn -i eth0 'tcp port 80' prints a dense line packed with a dozen fields. Click any segment to highlight what it means:

    sudo tcpdump -nn -i eth0 'tcp port 80' · Pick a segment
    On Ctrl+C, tcpdump prints three counters: captured (processed), received by filter (matched the BPF expression), and dropped by kernel (lost to buffer overflow).

    The dot . instead of A for the ACK flag is the most common source of confusion at first. Almost every packet after the opening SYN carries ACK, so [S.], [.], [P.], [F.], and [R.] appear constantly in the stream.

    Step by step: from interface list to .pcap

    1. List available interfaces

      Without -i, tcpdump picks the lowest-numbered interface that is up (excluding lo). On a server running Docker or a VPN, eth0 sits alongside br-* bridges, docker0, and dozens of veth* pairs, so always name the interface explicitly:

      sudo tcpdump -D
      # 1.eth0 [Up, Running, Connected]
      # 2.br-5d661bc4358d [Up, Running, Connected]
      # 3.docker0 [Up, Running, Connected]
      # 4.any (Pseudo-device that captures on all interfaces) [Up, Running]
      # 5.lo [Up, Running, Loopback]

      The -i any pseudo-device listens on every interface at once and prints the interface name and direction (In/Out), but switches the frame header to Linux SLL2, drops the destination MAC, and disables promiscuous mode (Limits of -i any). For local services on 127.0.0.1, use -i lo.

    2. Run a safe baseline capture

      Your everyday baseline flags are -nn (no DNS or port-name lookups), -i <interface>, and -c <count> (stop after N packets). When connected over SSH on the same interface, exclude your SSH port:

      sudo tcpdump -nn -i eth0 -c 5 'not port 22'
      # tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
      # listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
      # 20:32:22.714428 IP 198.51.100.20.49034 > 203.0.113.10.443: Flags [S], seq 3950828314, win 64240, options [mss 1460,sackOK,TS val 2868003974 ecr 0,nop,wscale 7], length 0
      # 20:32:22.714444 IP 203.0.113.10.443 > 198.51.100.20.49034: Flags [S.], seq 1048291102, ack 3950828315, win 65160, options [mss 1460,sackOK,TS val 401288112 ecr 2868003974,nop,wscale 7], length 0
      # ...
      # 5 packets captured
      # 5 packets received by filter
      # 0 packets dropped by kernel

      Always check that third summary line: 0 packets dropped by kernel. If it isn’t zero, packets were missed — tighten the filter, write to a file with -w, and enlarge the buffer with -B.

    3. Add detail: L2, TTL, direction, and timestamps

      When the default one-liner isn’t enough, add the flag for the layer you need:

      • -e — prints the L2 header: source and destination MAC, ethertype, and 802.1Q VLAN tag (on eth0 you see bc:24:11:af:27:2b > e2:a1:e8:41:67:85, ethertype IPv4 (0x0800), length 98).
      • -v / -vv — expands the IP header: ttl, id, DF flag, protocol, and checksums.
      • -tttt — full date and time (2026-11-24 20:32:22.714428); -ttt — microsecond gap since the previous packet (1–3 second stalls jump right off the screen).
      • -Q in or -Q out — capture only incoming or only outgoing packets on the interface.
      • -S — absolute seq/ack numbers instead of relative ones (handy when comparing two dumps taken at opposite ends of a link).
      sudo tcpdump -nn -v -i lo -c 2 'tcp port 80'
      # 20:32:22.714428 IP (tos 0x0, ttl 64, id 2718, offset 0, flags [DF], proto TCP (6), length 60)
      #     127.0.0.1.49034 > 127.0.0.1.80: Flags [S], cksum 0xfe30 (incorrect -> 0xda16), seq 3950828314, win 65495, ...
      # 20:32:22.714444 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 40)
      #     127.0.0.1.80 > 127.0.0.1.49034: Flags [R.], cksum 0x2b5b (correct), seq 0, ack 3950828315, win 0, length 0

      Notice cksum 0xfe30 (incorrect -> 0xda16) in the first packet — that is a normal artifact of TX Checksum Offloading on an outbound frame.

    4. Inspect payloads with -A and -X

      For unencrypted text protocols (internal HTTP backends, plain SMTP, Redis), print packet contents with -A (ASCII only) or -X (hex + ASCII side by side). Narrow the filter first so binary noise doesn’t swamp the terminal:

      sudo tcpdump -nn -A -i lo -c 4 'tcp port 8080'
      # 20:35:10.112041 IP 127.0.0.1.51944 > 127.0.0.1.8080: Flags [P.], seq 1:79, ack 1, win 512, length 78: HTTP: GET /health HTTP/1.1
      # E..z..@.@..............\...1...u...........
      # GET /health HTTP/1.1
      # Host: 127.0.0.1:8080
      # User-Agent: curl/8.5.0
      # Accept: */*

      On encrypted traffic (HTTPS, SSH, WireGuard), -A shows random bytes plus a few readable strings such as the plaintext SNI hostname in ClientHello. When piping the output into grep, add -l so tcpdump flushes stdout after every line.

    5. Write raw packets to .pcap and read them back

      For unhurried analysis or moving the dump to Wireshark, write it to disk with -w. In this mode tcpdump skips live decoding and isn’t throttled by SSH terminal throughput:

      # Capture up to 500 packets into /tmp/web.pcap:
      sudo tcpdump -nn -i eth0 -c 500 -w /tmp/web.pcap 'tcp port 443'
      
      # Read the same file later and apply any new BPF filter on top:
      tcpdump -nn -r /tmp/web.pcap 'tcp[13] & 0x04 != 0'

      Reading with -r doesn’t require root (as long as you have read permission on the file), and the filter after the filename re-filters stored packets in milliseconds. For long-running captures on a production server, -w alone isn’t enough — use a ring buffer with -C and -W (Ring buffer for .pcap).

    If it doesn’t work

    After running sudo tcpdump -nn -i eth0 'port 80' | grep GET, the screen stays blank even though requests are arriving

    Cause
    When tcpdump stdout is redirected to a pipe or file, the C standard library switches to block buffering (4 KiB): lines wait until the buffer fills.
    Fix
    Add -l (line buffering): sudo tcpdump -l -nn -i eth0 -A 'tcp port 80' | grep -E 'GET|POST'.

    With -v, every outgoing packet shows cksum 0xfe30 (incorrect -> 0xda16)

    Cause
    TX Checksum Offloading is active on the NIC: the kernel hands the driver a packet with an unfilled checksum, and tcpdump taps it before the card computes the sum in hardware.
    Fix
    Normal for outbound packets leaving the same host. An incorrect checksum is only a warning sign on incoming packets from the external network.

    tcpdump captured the traffic, but Wireshark shows truncated data (Packet size limited during capture)

    Cause
    The capture used a small snaplen (for example -s 96) that cut off everything past the headers.
    Fix
    For a full dump, omit -s or pass -s 0 (tcpdump 4.99.4 defaults to 262144 bytes — the entire frame).

    Writing a .pcap into your home directory or /root fails with Permission denied

    Cause
    Before opening the -w file, the process drops privileges to the tcpdump user (-Z tcpdump), which cannot write to /root, and Ubuntu also enforces an AppArmor profile.
    Fix
    Save dumps in /tmp/capture.pcap or a directory writable by tcpdump, or pass -Z root.

    How to check

    Capture four icmp packets on lo into a file, confirm dropped by kernel is zero, and read the file back without sudo:

    sudo tcpdump -nn -i lo -c 4 -w /tmp/check-lo.pcap 'icmp' &
    sleep 0.5 && ping -c 2 127.0.0.1 >/dev/null && wait
    tcpdump -nn -r /tmp/check-lo.pcap
    rm -f /tmp/check-lo.pcap

    The output should list two pairs of ICMP echo request and ICMP echo reply between 127.0.0.1 and 127.0.0.1. If rm complains about permissions, the file was created under the tcpdump user — remove it with sudo rm -f.