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 exceeds50million bytes (50 × 1,000,000;tcpdumpuses decimal megabytes, not1024 × 1024).-W 10— writeshttps.pcap0throughhttps.pcap9, then wraps around and overwriteshttps.pcap0. Disk usage stays capped at~500MB.-B 16384— raises the kernel capture ring buffer to16MiB (unit is kilobytes;libpcapdefaults to2MiB), keepingpackets dropped by kernelfrom 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 same500MB.
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,
tcpdumpdrops privileges to usertcpdump(-Z tcpdump) before opening the output file; that user cannot write to/var/logor/root, and the AppArmor profile/etc/apparmor.d/usr.bin.tcpdumprestricts 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 rootif 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),-Wsets a hard file limit instead of a ring: after the sixth file,tcpdumpprintsMaximum file limit reached: 6and exits with code0. - Fix
- For an endless ring buffer that overwrites old files, pair
-Cand-W(size-based). Use-G+-Wintentionally 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
tcpdumpbehavior (man tcpdump): when both-Cand-Gare specified together,-Wno longer caps the file count and affects filename numbering only. - Fix
- Never combine
-Cand-Gin 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.