basforge

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

    RU

    Capturing .pcap in production: -C and -W ring buffer instead of an unbounded file

    A forgotten tcpdump -w /tmp/dump.pcap on a production server fills the disk to 100% within hours and takes down databases and containers. Cap disk usage with a -C (file size) and -W (file count) ring buffer, raise the kernel buffer with -B, and account for the -Z tcpdump privilege drop on Ubuntu.

    Safe command for long-running captures

    To catch a rare glitch that happens once every few hours, run a ring buffer with a hard disk cap — for example, at most 500 MB (10 files of 50 million bytes):

    sudo install -d -o tcpdump -g tcpdump -m 750 /tmp/pcap
    
    sudo tcpdump -nn -i eth0 \
      -B 16384 \
      -C 50 -W 10 \
      -w /tmp/pcap/https.pcap \
      'tcp port 443'

    What each flag does:

    • -C 50 — closes the current file and opens the next once size exceeds 50 million bytes (50 × 1,000,000; tcpdump uses decimal megabytes, not 1024 × 1024).
    • -W 10 — writes https.pcap0 through https.pcap9, then wraps around and overwrites https.pcap0. Disk usage stays capped at ~500 MB.
    • -B 16384 — raises the kernel capture ring buffer to 16 MiB (unit is kilobytes; libpcap defaults to 2 MiB), keeping packets dropped by kernel from climbing during traffic bursts.
    • -s 128 (optional) — when you need Ethernet/IP/TCP/TLS headers without payloads, truncating the snapshot cuts file sizes several times over and stretches the time window inside the same 500 MB.

    Difference between -C -W and -G -W

    -W in tcpdump has two different meanings depending on its partner flag:

    Combination Behavior When to use
    -C 50 -W 10 -w /tmp/pcap/cap.pcap Endless ring: cap.pcap0..cap.pcap9 overwrite in a loop Waiting hours for a rare failure while enforcing a hard disk cap
    -G 300 -W 12 -z gzip -w '/tmp/pcap/cap_%H%M%S.pcap' Captures 12 five-minute files, compresses each with gzip, and exits after an hour Need a capture for a fixed window with no manual stop
    -C 50 -G 300 -W 10 Trap: -W stops capping the number of files Do not use

    On Ubuntu 24.04, the default AppArmor profile for tcpdump allows -z gzip out of the box, while blocking custom scripts passed to -z. Once the incident happens and you need to stop the ring, press Ctrl+C (or send SIGINT to the process in tmux/screen) so the latest file closes cleanly.

    If it doesn’t work

    sudo tcpdump -C 50 -W 10 -w /var/log/cap.pcap fails with Permission denied

    Cause
    On Ubuntu, tcpdump drops privileges to user tcpdump (-Z tcpdump) before opening the output file; that user cannot write to /var/log or /root, and the AppArmor profile /etc/apparmor.d/usr.bin.tcpdump restricts paths further.
    Fix
    Create a capture directory owned by tcpdump:tcpdump (sudo install -d -o tcpdump -g tcpdump -m 750 /tmp/pcap), write to /tmp/, or pass -Z root if AppArmor permits the directory.

    Ran -G 300 -W 6 -w '/tmp/cap_%H%M%S.pcap', and tcpdump exited after 30 minutes instead of rotating continuously

    Cause
    Paired with -G (time-based rotation), -W sets a hard file limit instead of a ring: after the sixth file, tcpdump prints Maximum file limit reached: 6 and exits with code 0.
    Fix
    For an endless ring buffer that overwrites old files, pair -C and -W (size-based). Use -G + -W intentionally when you want a capture for a fixed time window.

    Combined -C 100 -G 300 -W 10, and more than ten files accumulated on disk

    Cause
    Documented tcpdump behavior (man tcpdump): when both -C and -G are specified together, -W no longer caps the file count and affects filename numbering only.
    Fix
    Never combine -C and -G in one command: pick either a size-based ring (-C + -W) or time-based rotation (-G + -W).

    How to check

    Verify ring-buffer rotation and directory ownership with a short run on lo, read the file back, and clean up:

    sudo install -d -o tcpdump -g tcpdump -m 750 /tmp/pcap-check
    sudo tcpdump -nn -i lo -c 4 -C 1 -W 3 -w /tmp/pcap-check/test.pcap 'icmp' &
    sleep 0.5 && ping -c 2 127.0.0.1 >/dev/null && wait
    ls -lh /tmp/pcap-check/
    sudo tcpdump -nn -r /tmp/pcap-check/test.pcap0
    sudo rm -rf /tmp/pcap-check

    You should see test.pcap0 owned by tcpdump:tcpdump, two pairs of ICMP echo request/reply in the -r output, and 0 packets dropped by kernel in the capture summary.