Tcpdump Write To File

Tcpdump Write To File

Reading time1 min
#networking#cybersecurity#linux#tcpdump#pcap

Mastering tcpdump File Output: Capturing Network Traffic Efficiently and Safely

Consider a situation: production systems are experiencing intermittent packet loss, and the incident window is tight. Dumping packets on the terminal for ad hoc inspection isn’t going to scale—especially on a busy interface with thousands of flows per second. Packet capture must be performed methodically: controlled disk output, minimal system impact, and lossless preservation.

Here’s a focused tour through tcpdump output management—not just capturing, but capturing well.


Why Redirect tcpdump Traffic to Files?

There’s no opportunity for postmortem analysis if packets scroll off the terminal. Engineers and analysts need:

  • Long-term, lossless retention: For audits or compliance, raw packet data (with microsecond timestamps) is the source of truth.
  • Downstream inspection: Tools like Wireshark, Zeek, or Suricata require native .pcap files for deep parsing and reassembly.
  • Multi-GB workloads: File-based output is the only option when sniffing hours or days of traffic on high-speed links.

On live systems, printing to stdout is almost always a performance hazard. Even low-volume captures benefit from pcap-formatted output.


tcpdump File Output Fundamentals

The standard method:

sudo tcpdump -i eth0 -w /var/log/captures/suspicious_20240617.pcap
  • -i eth0: Target interface (replace as necessary, e.g., ens160, bond0).
  • -w <file>: Write binary pcap data. Output is not human-readable. Direct inspection in a text editor yields meaningless bytes.

Common gotcha: If you redirect via shell operators (| or >), output turns into undecodable text, not a valid pcap.

Open the resulting file with Wireshark or tshark:

wireshark /var/log/captures/suspicious_20240617.pcap

Or review from CLI:

tcpdump -r /var/log/captures/suspicious_20240617.pcap

Performance: Minimizing Packet Drop

Busy interfaces can overwhelm tcpdump’s default buffer. This leads to silent packet drops—hard to spot without vigilance. Syslog may emit:

Dropped 1421 packets

Increase the kernel capture buffer size:

sudo tcpdump -i eth0 -B 4096 -w capture.pcap
  • -B 4096 allocates a 4MB buffer (default varies by distro; usually 2MB or lower).

Note: On heavy traffic, even 4MB may not suffice. Monitor tcpdump stats after closing:

100938 packets captured
1122 packets dropped by kernel

If drops persist, consider a dedicated capture interface or mirroring traffic to a less-busy NIC.


Filtering at Capture: Save Only What’s Relevant

It’s rarely efficient to capture an entire DC link. Pre-filtering reduces disk I/O and analysis time. Capture only HTTP:

sudo tcpdump -i eth0 'tcp port 80' -w http_20240617.pcap

Layered filters are also possible:

sudo tcpdump -i eth0 '(tcp port 443 or tcp port 8443) and src net 10.0.4.0/24' -w tls_internal.pcap

Tip: Always test filters with -v (verbose) or -c 10 (max 10 packets) before running unattended captures.


Snaplen: Avoid Silent Truncation

By default, many linux distros build tcpdump with a snaplen (-s) value of 262 bytes, meaning large packets are silently truncated. For application debugging, this can eliminate payloads:

sudo tcpdump -i eth0 -s 0 -w fullpkt.pcap
  • -s 0 disables truncation, capturing the entire Ethernet frame.
  • Drawback: Files grow rapidly on jumbo frame networks. Ensure disk I/O is not a bottleneck.

Managing File Size: Automatic Rotation

Large captures risk exhausting local storage. Tcpdump supports basic file rotation by size:

sudo tcpdump -i eth0 -w sess-%03d.pcap -C 500
  • Files rotate every 500 MB (-C 500).
  • Patterns like sess-000.pcap, sess-001.pcap...
  • Some releases (Debian tcpdump 4.9.x, RHEL 8.x+) don’t support timed rotation (-G). For time-slicing, coordinate with external scheduling (cron, systemd-timer, or timeout).
timeout 45m sudo tcpdump -i eth0 -w hour.pcap

Known issue: If a capture ends abruptly (disk full, lost ssh, system kill), the last file may be corrupt. Always plan cleanup and monitor available space:

df -h /var/log/captures

Avoiding Data Loss and Ensuring Clean Writes

  • Shutdown signals: Invoking SIGINT (Ctrl+C) generally flushes buffers. Unexpected terminations (kill -9) do not.
  • Resource starvation: On overloaded systems, packet loss is inevitable. Run tcpdump with elevated scheduling priority if possible:
sudo nice -n -5 ionice -c 1 -n 0 tcpdump ...
  • Verify post-capture: Always inspect summary stats for packet drops. Don’t blindly trust the generated pcap.

Reference Table: tcpdump File Output Essentials

PurposeCommand ExampleNotes
Save all trafficsudo tcpdump -i eth0 -w capture.pcapDisk fills quickly—monitor available space
Read capture`tcpdump -r capture.pcapmore`
Boost buffertcpdump -i eth0 -B 4096 -w buf.pcapUseful on busy interfaces
Full-packet capturetcpdump -i eth0 -s 0 -w fullcap.pcapNeeded for application-level forensics
Filter HTTPtcpdump -i eth0 'tcp port 80' -w httpcap.pcapShrinks result set
Rotate by 500 MBtcpdump -i eth0 -w seg-%03d.pcap -C 500No built-in time-based split (see above)

Advanced Note: Non-Obvious Behavior

On some hardware (e.g., Broadcom NICs with offload), checksummed packets may be reported as malformed in Wireshark even if the raw pcap is correct. Disable offloads before capture for clean output:

sudo ethtool -K eth0 rx off tx off tso off gso off gro off lro off

Re-enable as necessary post-capture.


Closing Remarks

For high-fidelity packet analysis, disciplined use of tcpdump file output is non-negotiable. Tune buffer sizes to load, filter aggressively, capture full packets only where necessary, and design for rotation and integrity. The difference between a valid and an incomplete capture is occasionally subtle—often revealed only days later, mid-investigation.

Where workflows permit, consider using dumpcap (Wireshark’s headless backend) or dedicated appliance tools for very high throughput or reliable rotation. Sometimes the simplest approach—a dedicated USB SSD and periodic local syncs—remains most robust.

Don’t trust. Verify.


Feedback, war stories, or corrections? Direct PRs or email—constructive nitpicks appreciated.