Capturing well: capture filters, ring buffers and where to tap
Choose a capture point (on the host, a SPAN port or a TAP) for a given fault, write BPF capture filters that work on tagged trunks, and bound disk use and data exposure with snap lengths, ring buffers and written authorisation.
Contentsand lesson path
On this page
- Why it matters
- Where to capture: on the host, a SPAN port or a TAP
- Capture filters: what BPF keeps, and the VLAN trap
- Capturing responsibly: permission, snap length and ring buffers
- Check your understanding
- Apply it
- Review prompt
Lessons of Packet Capture & Analysis
- Layers that matter in practice: OSI vs TCP/IP and encapsulation Concept · 20 min
- Lab: one web request, every layer, in your first capture Lab · 25 min
- Capturing well: capture filters, ring buffers and where to tap Concept · 20 min
- Lab: tcpdump on a remote server, analysis in Wireshark on your desk Lab · 25 min
- Display filters, conversations and stream reassembly Worked example · 25 min
- Check: answer questions from three capture files Knowledge check · 8 questions
What you will learn
- Capture traffic with tcpdump and Wireshark using capture filters, and choose a capture point (host, SPAN port, TAP) for a given problem.
- Capture responsibly: obtain authorization, limit sensitive data, and use ring buffers and snap lengths to control file size.
Prerequisites: Networking Basics
01. Why it matters
Suppose the badge readers in a warehouse lose contact with their door controller a few times each night. The readers are sealed, so nobody can install tcpdump on them. Someone plugs a laptop into a free port on the same switch and leaves an unfiltered capture running until morning. The result is several gigabytes of broadcasts and the laptop's own traffic, not one frame from the readers, plus data nobody had agreed to collect.
A capture is only as good as four decisions taken before you press Enter: where you listen, which packets you keep, how much of each packet you keep and for how long, and who has agreed to it.
02. Where to capture: on the host, a SPAN port or a TAP
The simplest capture point is the endpoint itself. An on-host capture, tcpdump on a Linux server you administer, needs no cabling. It has two blind spots: it is impossible on sealed appliances, and it sees outgoing packets before the network card finishes them. With checksum offload the checksum is still empty, and with TCP segmentation offload (TSO) or its software form, generic segmentation offload (GSO), the stack hands over one large block that is split into MTU-sized segments later. A host capture can therefore show "packets" of tens of kilobytes on a 1,500-byte link, which never existed on the wire.
Capturing from the network brings in the switch. Once a switch has learnt where each MAC address lives (Ethernet Switching & VLANs), it sends a unicast frame only out of the port towards its destination, so a laptop on a free port receives broadcasts and its own traffic, nothing else. There are two ways to get a copy of someone else's traffic:
- A SPAN port (Switched Port Analyzer, also called port mirroring). You configure the switch to copy the frames sent and received on a source port or VLAN to a monitor port. The copy is made in switch hardware, so the production port is not slowed down, but it is best-effort. A full-duplex port can send and receive at line rate at the same time while the monitor port transmits at only one line rate, so a busy source can produce more copies than the monitor port carries, and the excess is silently dropped from the copy. The switch also mirrors only frames it accepted, so frames with a bad FCS usually never reach the monitor port.
- A network TAP (Test Access Point). A passive optical splitter or an active device in the cable copies both directions, including damaged frames, without depending on switch buffers. Inserting it breaks the link briefly, and it usually delivers the two directions on two outputs, so you need two capture interfaces or an aggregating TAP.
For the badge readers a SPAN session is enough; if the vendor later blames the switch for corrupt frames, a TAP is better evidence because it keeps damaged frames.
Situation Capture point Why
---------------------------------- ------------- ---------------------------------
Linux host you administer On the host No cabling; sees pre-offload data
Sealed device, moderate traffic SPAN port Hardware copy, no link outage
Busy full-duplex link, CRC faults Network TAP Copies every frame, even damaged
Wire-size frames needed from host SPAN or TAP Host view is before TSO and cksum03. Capture filters: what BPF keeps, and the VLAN trap
A capture filter decides which packets are kept at all. Both tcpdump and Wireshark's capture engine dumpcap use the same syntax, documented in pcap-filter(7): primitives such as host 192.0.2.31, net 198.51.100.0/24, tcp port 3001 or udp, combined with and, or and not. The libpcap library compiles the expression into a small program for the Berkeley Packet Filter (BPF) virtual machine, in user space, and on Linux hands it to the kernel, which runs it on every packet before copying anything to tcpdump. Rejected packets cost almost nothing, which is why a tight filter is the first defence against drops and full disks. A Wireshark display filter such as ip.addr == 192.0.2.31 is a different language for a different job: it hides packets that were already captured, and tcpdump rejects it with a syntax error.
Write packets with -w file.pcap; redirecting tcpdump's printed text with > keeps only one-line summaries that Wireshark cannot open, and options that change only the printout, such as -n and -v, change nothing in the file. tcpdump writes the classic pcap format, while dumpcap and Wireshark default to pcapng, which can also hold interface names and comments; Wireshark opens both.
Trunk links carry 802.1Q tags, 4 bytes between the MAC addresses and the EtherType, and BPF offsets count from the start of the frame; pcap-filter(7) notes that the vlan keyword shifts all later offsets by 4. Whether a filter without vlan matches tagged traffic depends on where the tag is when the filter runs. A test on a Linux veth trunk (kernel 6.8, tcpdump 4.99.1) showed three cases:
- Live capture on Linux: the kernel normally keeps the tag in packet metadata, not in the bytes the filter reads. A plain
host 198.51.100.20matched all 10 tagged frames, the same asvlan and host 198.51.100.20. With the trunk's VLAN offloads off, sent frames kept the tag in their bytes, and the plain filter caught only the received half: 5 of 10 (the ARP reply and four echo replies). - Reading a saved file: libpcap writes the tag back into the frames it saves.
tcpdump -r trunk.pcap host 198.51.100.20matched 0 frames, whilevlan and host 198.51.100.20matched all 10. - Other systems and capture cards may deliver tagged frames as they are, which behaves like the saved file.
A filter that works in every case is host X or (vlan and host X); in the test it matched all frames both live and from the file.
# tcpdump: keep only the badge reader's traffic, on a trunk or not, as pcap
tcpdump -i eth1 -w badge.pcap \
"host 192.0.2.31 or (vlan and host 192.0.2.31)"
# dumpcap (Wireshark's capture engine): same BPF filter, pcapng output
dumpcap -i eth1 -f "host 192.0.2.31 or (vlan and host 192.0.2.31)" \
-w badge.pcapng
# Show the BPF program libpcap compiles, without capturing anything
tcpdump -d "host 192.0.2.31 and tcp port 3001"04. Capturing responsibly: permission, snap length and ring buffers
Captures on real networks pick up session cookies, passwords of old protocols, database rows and personal data. Before you capture on any system that is not your own lab, get written authorisation from the owner of the system and of the data, stating the scope (hosts, interfaces, ports, time window), where files are stored, who may read them and when they are deleted. Being able to run sudo is a technical ability, not permission. Encryption does not remove the need: encrypted traffic still shows who talked to whom, when, how much, and usually the server names. Collecting a service's keys to decrypt later is a larger risk and needs its own approval.
The snap length (-s) is how many bytes of each packet are saved; the default, 262,144, keeps whole packets. Header sizes decide a sensible value: Ethernet plus minimal IPv4 and TCP headers is 14 + 20 + 20 = 54 bytes, 66 with the timestamp option Linux sends, but IPv4 and TCP headers can each grow to 60 bytes and IPv6 starts at 40. So -s 96 keeps typical IPv4 TCP headers plus the first 30 or so bytes of data: it limits the payload stored but does not remove it, and on IPv6 with options it can cut the TCP options. Check real header sizes on your traffic, as the lab does, and treat surviving data as sensitive.
A ring buffer bounds the disk. -C 250 -W 8 -w door.pcap starts a new file once the current one has passed 250 million bytes (units of 1,000,000 bytes, checked before each write, so a file can end one packet larger) and keeps at most 8 files, door.pcap0 to door.pcap7, overwriting the oldest. It can run for days in about 2 GB. dumpcap does the same with -b filesize:250000 -b files:8 (kilobytes). Rotating by time with -G is different: with -W, tcpdump stops after that many files. -G 1 -W 3 with a ping every 0.2 s exited after 3 s ("Maximum file limit reached: 3"); rotation waits for the next packet, so with a ping every 2 s it took 6 s.
Even a bounded capture can lose packets. If the kernel buffer fills faster than tcpdump empties it, tcpdump reports "packets dropped by kernel" when it stops, and the file has holes where traffic was busiest. A tighter filter, a smaller snap length or a bigger buffer (-B, in KiB) help.
When not to capture: when metrics or logs already show where the time goes. And an unfiltered full-packet capture left on a busy production host is wrong even when authorised: it fills disks, drops packets and stores far more personal data than needed.
05. Check your understanding
06. Apply it
Write the capture request you would send to the warehouse's network owner for the badge-reader problem. State the capture point and why, the exact tcpdump or dumpcap command with its capture filter, a snap length justified by header sizes, a ring buffer that fits in 4 GB, and the retention period and deletion step. Say who may read the files, and restrict them with ordinary Linux file permissions.
The skill practice asks for a capture with a capture filter and a ring buffer. In the next lab you will measure what -s 96 really keeps, watch a ring buffer wrap and stream a capture from a server to an analysis workstation.
07. Review prompt
Explain from memory why a laptop on a spare switch port sees almost none of a neighbour's traffic; when a TAP gives better evidence than a SPAN port; why a plain BPF host filter can match tagged frames during a live Linux capture and fail on the saved file; and what a snap length and a ring buffer each limit, and what they do not.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.