Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท Capturing Traffic

dumpcap, tshark and Long Captures

Capture from the command line with dumpcap, tshark and tcpdump, use ring buffers and autostop conditions, and keep long captures small, safe and searchable.

The Wireshark GUI is perfect for looking at a few thousand packets. When a problem happens once a day, when the machine has no desktop, or when you need hours of data, you capture with command-line tools and open the result later. This lesson covers dumpcap, tshark and tcpdump, the multi-file ring buffer, and habits that keep a long capture useful.

The tools

Tool Purpose
dumpcap Capture only, minimal and fast. Wireshark itself uses it.
tshark Capture and analyze in the terminal: displays packets, applies display filters, prints chosen fields.
tcpdump The classic capture tool, installed on most servers. Writes pcap files that Wireshark opens.
capinfos Prints facts about a file: duration, packet count, data rate, interfaces.
editcap Cuts, splits, deduplicates, shifts time or converts a file.
mergecap Merges several files in time order.

List interfaces first: dumpcap -D (or tshark -D, tcpdump -D).

Basic captures

Terminal
# capture 1000 packets on interface 2 to a file
dumpcap -i 2 -c 1000 -w quick.pcapng

# capture only web traffic for one minute
tshark -i eth0 -f "tcp port 80 or tcp port 443" -a duration:60 -w web.pcapng

# same with tcpdump: -s 0 means full packets, -n no name lookups
sudo tcpdump -i eth0 -s 0 -n -w web.pcap 'tcp port 80 or tcp port 443'

(Read-only commands; the output depends on your system.) Options you will use constantly: -i interface, -f capture filter, -w output file, -c packet count, -a autostop condition, -b ring buffer, -B buffer size in MiB, -s snaplen, -n no name resolution.

Autostop conditions

-a ends the capture when a condition is met. Conditions can be combined; the first one to hit stops the run:

Condition Example Meaning
duration: -a duration:3600 stop after 3600 seconds
filesize: -a filesize:500000 stop at about 500,000 kB
files: -a files:20 stop after 20 files (with multiple files)
packets: -a packets:100000 stop after this many packets

Ring buffers: capture for hours or days

A ring buffer writes a sequence of files of limited size and, when it reaches the maximum number of files, deletes the oldest. The disk never fills, and you always have the latest stretch of traffic. It is the right tool for "it happens sometimes": leave the capture running, and when the problem occurs, stop it and open the newest files.

Terminal
# 10 files of 100 MB each (filesize is in kB); old files are deleted
dumpcap -i eth0 -f "not port 22" -b filesize:100000 -b files:10 -w /var/captures/ring.pcapng

dumpcap names the files ring_00001_20260105100000.pcapng, ring_00002_... and so on: a counter and a timestamp. Other -b options rotate by time instead (-b duration:600 starts a new file every 10 minutes) or by packet count (-b packets:50000).

The stand-in simulates the rotation logic so you can see which files survive. It is a model of the behavior, not the real tool.

Python Simulate a ring buffer of capture files (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading files from the command line

tshark reads a file with -r and applies the display filter with -Y. -T fields with -e prints just the fields you choose, perfect for scripts and quick questions:

Terminal
tshark -r web.pcapng -Y "http.request" -T fields -e frame.time -e ip.src -e http.host -e http.request.uri
tshark -r web.pcapng -q -z io,stat,1          # packets per second
tshark -r ring_00007.pcapng -Y "tcp.analysis.retransmission" | head
capinfos web.pcapng

Never mix up -f (capture filter, BPF) and -Y (display filter). -R is a legacy read filter; use -Y.

The stand-in mimics -T fields -e by printing selected fields from invented packet records, so you see why this output is easy to feed to other tools. We use Python's csv-style tabs to match tshark's default separator.

Python Extract fields like tshark -T fields (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Habits for long captures

  • Test the filter first on a short run and check that the packets you expect appear.
  • Keep time in sync. Use NTP on all machines. Note the exact time the problem occurred, so you can find it in the ring (frame.time >= "2026-01-05 10:42:00").
  • Mind privacy and storage. Captures hold secrets. Store them in a restricted folder, delete them when done and share them only after cleaning (for example with editcap to trim, or a snaplen that cuts payloads).
  • Watch disk and CPU. Write to a fast local disk, not a network share; use -B 64 or more on busy links; avoid name resolution.
  • Run as the right user. Use the capture group or setcap for dumpcap, not blanket root.
  • Split later. editcap -c 100000 big.pcapng part.pcapng splits by packet count; editcap -A "2026-01-05 10:40:00" -B "2026-01-05 10:45:00" in.pcapng out.pcapng cuts a time window.
  • Document what you did: the command line, interface, filter and start time. In pcapng you can add a capture comment (Statistics > Capture File Properties).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a ring buffer do?

    A ring buffer keeps the disk from filling and leaves you the most recent traffic.
  2. 2. Which tshark option applies a DISPLAY filter when reading a file?

    -Y is the display filter, -f is the capture filter, -w writes a file and -i selects the interface.
  3. 3. Why is dumpcap preferred for long captures?

    dumpcap does no dissection, so it uses less CPU and is the same engine Wireshark uses to capture.
  4. 4. What does 'tcpdump -s 0' mean?

    A snaplen of 0 means the maximum, so packets are captured in full.

Summary

  • dumpcap, tshark and tcpdump capture without a GUI; Wireshark reads their files.
  • -f is a capture filter, -Y a display filter; -a sets autostop conditions.
  • Ring buffers (-b filesize:... -b files:...) let you capture for days without filling the disk.
  • tshark -T fields -e ... turns a capture into a table that scripts can use.
  • Synchronize clocks, protect capture files and note exactly what you ran.

Next: Display Filter Basics