How to Capture Network Packets: tcpdump to Kubernetes
tcpdump flags, filters and file rotation tested on a real capture, plus dumpcap, macOS pktap, Windows pktmon, Docker, Kubernetes and capture privacy.
By Redbison Dev · · 12 min read

Connections time out between two services, the logs show nothing useful, and someone has asked for a packet capture. On Linux the command is sudo tcpdump -i any -nn -w /tmp/capture.pcap 'host 198.51.100.20'. Reproduce the problem, then press Ctrl+C. On macOS use a named interface (sudo tcpdump -i en0 -w trace.pcap, as Apple documents), on Windows use Wireshark or the built-in pktmon, and in Kubernetes run tcpdump from an ephemeral debug container. The tcpdump output quoted below was produced by tcpdump 4.99.1 (Apple version 156, libpcap 1.10.1). Live capture needs root, which the machine used for this guide did not have, so the examples read, filter and rotate a capture composed from RFC 5737 documentation addresses. Linux-only behaviour is cited from tcpdump.org's manual. The PCAP File Inspector reads the result in your browser without uploading it.
tcpdump: the flags that matter for a capture file
sudo tcpdump -i any -nn -s 0 -B 4096 -w /tmp/capture.pcap -C 100 -W 10 'tcp port 443 and host 198.51.100.20'
| Flag | What it does, per the tcpdump manual |
|---|---|
-i any | all interfaces on Linux; "captures on the 'any' pseudo-interface will not be done in promiscuous mode" |
-i en0, -D | one interface; -D lists them (it worked without root here and listed 23) |
-w file | write raw packets instead of printing them; always do this for anything you will share |
-nn | no name or port lookups; DNS lookups slow the capture and leak queries of their own |
-s snaplen | bytes kept per packet; default 262144, and -s 0 means the same default |
-c count | stop after count packets |
-C size | start a new file when the current one passes size, in millions of bytes |
-W n | with -C, keep at most n files and overwrite in a ring |
-G secs | rotate every secs seconds; put strftime fields in the -w name or each file overwrites the last |
-B KiB | operating system capture buffer, in KiB; raise it when tcpdump reports packets dropped by kernel |
-U, --immediate-mode | flush each packet to the file, or deliver packets as they arrive |
-Z user | after opening the device, drop root and become user |
-p | do not put the interface into promiscuous mode |
On snaplen, the manual says -s 0 "sets it to the default of 262144, for backwards compatibility with recent older versions of tcpdump". So -s 0 in old runbooks is harmless but no longer needed. Smaller values are the useful ones. The largest Ethernet, IPv4 and TCP headers come to 14 + 60 + 60 = 134 bytes, so on untagged Ethernet -s 134 keeps every IPv4 TCP header and drops the payload. That is a privacy setting as much as a size one. The manual adds that bigger snapshots reduce buffering and "may cause packets to be lost".
-Z matters for where files can go. The manual notes that dropping privileges "can also be enabled by default at compile time". With a build like that, -w into a directory only root can write to fails after the capture has started. Write to a directory the dropped user owns, or pass -Z with your own user.
Reading and filtering a capture
The worked capture has 15 packets: a DNS lookup, a plain-HTTP request, a connection attempt nobody answers, and an HTTPS handshake. Read it back with -r:
$ tcpdump -nn -r capture.pcap
reading from file capture.pcap, link-type EN10MB (Ethernet)
09:00:00.000100 IP 192.0.2.10.53001 > 192.0.2.53.53: 6699+ A? api.example.com. (33)
09:00:00.012200 IP 192.0.2.53.53 > 192.0.2.10.53001: 6699 1/0/0 A 198.51.100.20 (49)
09:00:00.013000 IP 192.0.2.10.50100 > 198.51.100.20.80: Flags [S], seq 1000, win 65535, length 0
...
09:00:01.326400 IP 192.0.2.10.50300 > 198.51.100.20.443: Flags [P.], seq 1:518, ack 1, win 65535, length 517
Filters use pcap-filter syntax, and the same expression works at capture time and on -r. Here are the counts each one matched in that file:
| Filter | Packets | Picks out |
|---|---|---|
| (none) | 15 | everything |
'tcp port 80' | 7 | the HTTP exchange |
'net 198.51.100.0/24' | 11 | both connections to the API's network |
'udp port 53' | 2 | DNS query and answer |
'host 203.0.113.7' | 2 | the unanswered attempt and its retry |
'not port 443' | 11 | all but the HTTPS connection |
Connection attempts alone are the classic first question in a timeout. The flags filter below keeps packets with SYN set and ACK clear:
$ tcpdump -nn -r capture.pcap 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn'
09:00:00.013000 IP 192.0.2.10.50100 > 198.51.100.20.80: Flags [S], seq 1000, win 65535, length 0
09:00:00.200000 IP 192.0.2.10.50200 > 203.0.113.7.8080: Flags [S], seq 9000, win 65535, length 0
09:00:01.200400 IP 192.0.2.10.50200 > 203.0.113.7.8080: Flags [S], seq 9000, win 65535, length 0
09:00:01.300000 IP 192.0.2.10.50300 > 198.51.100.20.443: Flags [S], seq 20000, win 65535, length 0
The SYN to 8080 is sent twice, a second apart, with no SYN-ACK: a retransmission into a dead port or a dropping firewall. tcpdump -r in.pcap -w out.pcap 'tcp port 80' cut the file to a 761-byte capture of the 7 HTTP packets, which is how you trim a capture for a ticket.
Rotating files: -C, -W and -G
A capture left running fills a disk. Rotation was tested offline by rewriting a 2,515-packet, 3,676,896-byte capture through tcpdump with -r ... -w:
| Command | Files written |
|---|---|
-w part.pcap -C 1 | part.pcap 1,000,026 bytes, part.pcap1 and part.pcap2 1,001,094 each, part.pcap3 674,754 |
-w ring.pcap -C 1 -W 3 | ring.pcap0 674,754, ring.pcap1 and ring.pcap2 1,001,094 each |
Four things the table shows:
-Cunits are millions of bytes, as the manual says (1,000,000, not 1,048,576). Files run slightly over, because tcpdump checks the size before each write.- Without
-W, the first file has no number. The next ones get 1, 2 and 3 appended after the extension. - With
-W, every file is numbered with zero padding, and the ring wraps. The newest packets ended up inring.pcap0, starting at 09:00:02.4205, whilering.pcap1held older ones. Sort by timestamp, not by name. - The ring kept 1,821 of 2,515 packets. The oldest 694 were overwritten. Size
-Ctimes-Wto cover the time between the fault and someone stopping the capture.
-G 300 -w 'cap-%Y%m%d-%H%M%S.pcap' rotates every five minutes instead. With -W, -G stops tcpdump after that many files rather than wrapping.
Wireshark, dumpcap and tshark
Wireshark's capture dialog sets the same things (interface, capture filter, snaplen, promiscuous mode, buffer, ring buffer), and its User's Guide says "pcapng is the default". For long captures use its command-line engine dumpcap (Wireshark 4.6 manual):
dumpcap -i eth0 -f 'tcp port 443' -b filesize:100000 -b files:10 -w /captures/api.pcapng
dumpcap -i eth0 -a duration:600 -w /captures/ten-minutes.pcapng
tshark -i eth0 -f 'tcp port 443' -w /captures/api.pcapng
-b filesize:is in kB of 1,000 bytes. The manual's own example says-b filesize:1000 -b files:5makes "a ring buffer of five files of size one megabyte each".filesize:100000is therefore 100 MB, and ten of them cap the capture at 1 GB.-b duration:switches files by time;-a duration:stops the capture.-Bis in MiB (default 2 MiB), unlike tcpdump's KiB.-F pcapwrites classic pcap. The older-Pis deprecated in favour of-F, and pcapng is forced anyway when capturing from several interfaces.- tshark's
-fis a capture filter,-Ya display filter. The tshark manual warns capture filters are "much more efficient", so filter with-fon a busy link.
pcap or pcapng? Classic pcap holds one link type, no comments and microsecond or nanosecond timestamps. pcapng adds per-interface blocks, comments and metadata. tcpdump, Wireshark and the inspector read both. pktmon converts to pcapng only.
Permissions: the Wireshark wiki's Linux recipe gives dumpcap the two capabilities it needs instead of running everything as root, with sudo setcap cap_net_raw,cap_net_admin+eip /usr/sbin/dumpcap, plus a wireshark group. capabilities(7) defines CAP_NET_RAW as "Use RAW and PACKET sockets" and CAP_NET_ADMIN as including "set promiscuous mode". On macOS, capture means reading /dev/bpf*. Here those devices were crw------- root wheel, which is why live capture needed root while -D and -r did not.
macOS and iOS specifics
Apple's packet trace page gives sudo tcpdump -i en0 -n and sudo tcpdump -i en0 -w trace.pcap, and finds the interface with networksetup -listallhardwareports. Apple's tcpdump adds options of its own, documented in its man page:
- No
-iat all uses a pseudo interface across the interfaces the kernel picks, excluding loopback and tunnels. -i pktap,lo0,en0captures several interfaces at once, and-i allincludes loopback and tunnels. A pktap capture "provides for packet metadata" and is written as pcapng.-kprints that metadata. Its letters includeN(process name),P(process ID),D(direction) andI(interface). That answers "which app sent this packet" from the capture itself.-Qfilters on metadata, and-Pwrites pcapng.
For an iPhone or iPad, connect it by USB and run rvictl -s <UDID>. That creates rvi0, and sudo tcpdump -i rvi0 -w trace.pcap records the device's whole network stack. Apple says an RVI cannot target one interface on the device, but -k shows which one each packet used.
Windows: Wireshark with Npcap, or pktmon
Wireshark on Windows captures through Npcap. Its guide lists an NPF_Loopback adapter for loopback traffic, the one capture Windows' own stack makes awkward. Without installing anything, use pktmon, which Microsoft calls "an in-box, cross-component network diagnostics tool":
pktmon start --capture --pkt-size 0 --file-name api.etl
pktmon stop
pktmon etl2pcap api.etl --out api.pcapng
Per Microsoft's reference, --pkt-size defaults to 128 bytes ("To always log the entire packet, set this to 0"), the log defaults to PktMon.etl, and --file-size defaults to 512 MB in circular mode. etl2pcap writes pcapng and leaves dropped packets out by default. The pktmon overview says the tool ships from build 19041, while the syntax page says Windows 10 and Server 2019 version 1809 and later. Run pktmon help on the machine to see what its build supports.
Docker and Kubernetes
A container has its own network namespace, so tcpdump on the host's eth0 misses traffic between containers on a bridge. Join the container's namespace instead, with a sidecar sharing its network stack (Docker's --network container:<name|id>) or the host's tcpdump run inside it:
docker run --rm -it --network container:api -v "$PWD":/out nicolaka/netshoot tcpdump -i eth0 -nn -w /out/api.pcap
sudo nsenter -t "$(docker inspect -f '{{.State.Pid}}' api)" -n tcpdump -i eth0 -nn -w /tmp/api.pcap
Docker's run reference lists NET_RAW among the capabilities "allowed by default", so the sidecar can capture without --privileged. A container started with --cap-drop ALL will not. The nsenter -n form needs nothing in the image and writes the file on the host.
In Kubernetes, ephemeral containers (stable since v1.25) do the same job inside the pod:
kubectl debug -n shop -it api-7d9f --image=nicolaka/netshoot --target=app --profile=netadmin -- tcpdump -i any -nn -w /tmp/api.pcap
kubectl cp -n shop api-7d9f:/tmp/api.pcap ./api.pcap -c <debugger-name>
--profile=netadminadds NET_ADMIN and NET_RAW (allowNetadminCapabilityin kubectl'sprofiles.go). The docs disagree on the default: the debugging task page sayslegacyapplies when--profileis omitted, while the kubectl reference saysgeneral. Pass it explicitly.--targetshares the app container's process namespace. Containers in a pod share the network namespace anyway. The docs warn that--targetmust be supported by the container runtime.- The file stays in the debug container.
kubectl cp -c <debugger-name>needstarin the debug image. Use the container namekubectl debugprinted. - Pod Security admission refuses it in namespaces at
baselineorrestricted: the Pod Security Standards page allows only a fixed list of added capabilities, and NET_ADMIN and NET_RAW are not on it. Settle the exception with the cluster owner before an incident.
What a capture holds, and how big it gets
Everything on the wire for the window, verbatim. Reading the plain-HTTP packets of the worked capture with -A prints the payload as text:
$ tcpdump -nn -A -r capture.pcap 'tcp port 80' | grep -aE 'Authorization|Cookie|email'
Authorization: Basic ZGVtbzpodW50ZXIy
Cookie: session=7f3c9a1e5b
{"order":42,"email":"customer@example.com","total":"19.99"}
ZGVtbzpodW50ZXIy is base64 for demo:hunter2, a username and password. TLS hides payloads but not who talked to whom, when, or how much: the DNS query names api.example.com in clear. Store captures like credentials, cut them to the conversation the ticket needs, and use -s 134 when headers are enough.
Size is throughput times duration, plus framing. The worked file is 1,896 bytes: the 24-byte pcap header plus 15 packets of 1,632 bytes, each with a 16-byte record header. A link averaging 100 Mbit/s for 10 minutes writes about 7.5 GB at full snaplen. That is why the rotation flags exist.
Which method for which situation
| Situation | Use |
|---|---|
| Linux server, intermittent timeouts | tcpdump -i any -nn -w ... -C 100 -W 10 with a host or port filter |
| Need to see which macOS app sends traffic | tcpdump -i pktap,en0 -k NP |
| iPhone app | rvictl -s <UDID>, then tcpdump -i rvi0 |
| Windows, nothing installed | pktmon start --capture --pkt-size 0, then etl2pcap |
| Windows loopback | Wireshark with Npcap's loopback adapter |
| Docker container without tcpdump | sidecar with --network container:<name>, or nsenter -n |
| Kubernetes pod | kubectl debug --profile=netadmin --target=<app> |
| Long unattended capture | dumpcap -b filesize: -b files: ring buffer |
If the failure is in a browser, a HAR capture gives the request-level detail that TLS hides from a packet capture. For a Go or Node server that is slow on its own, Go pprof profiles or a Node heap snapshot look inside the process instead.
Tools for this job
- PCAP File Inspector: drop a
.pcapor.pcapng. It shows protocol shares, top conversations, DNS queries and unanswered SYNs, parsed locally. On the worked file it reports 15 packets over 1.3 s, 4 IPv4 hosts and 2 SYNs never answered. It decodes Ethernet, raw IP and Linux cooked v1 framing, so capture on a named interface when you want its protocol tables. - HAR File Analyzer: when the question is HTTP timing in a browser rather than packets.
The pcap triage guide covers what to read first once the file exists and when to move to Wireshark.
Frequently asked questions
Why does tcpdump say permission denied?
Live capture opens a raw socket on Linux or a /dev/bpf device on macOS, and both need privileges. Use sudo, or on Linux give dumpcap CAP_NET_RAW and CAP_NET_ADMIN. Reading a file with -r and listing interfaces with -D need none.
Should I still use -s 0 with tcpdump?
It does no harm and no longer does anything. The tcpdump manual says -s 0 sets the snaplen to the default of 262144 bytes, which already captures whole packets on ordinary links. Choose a smaller value deliberately when you want headers without payload.
How do I capture traffic inside a Kubernetes pod?
Start an ephemeral debug container with an image that has tcpdump, the netadmin profile and --target set to the app container. Write to a file inside it, then copy it out with kubectl cp naming the debug container. The debug image needs tar for kubectl cp.
pcap or pcapng: which should I save?
pcapng when the tools allow it: it records several interfaces, comments and capture metadata, and Wireshark has used it by default since version 1.8. Write classic pcap only for an old tool that cannot read pcapng. Both open in Wireshark and the PCAP File Inspector.
Sources and further reading
- tcpdump(1), The Tcpdump Group. Every flag above: -i any, -s, -C, -W, -G, -Z, -B.
- pcap-filter(7), The Tcpdump Group. Filter syntax and the tcpflags examples.
- dumpcap(1), Wireshark Foundation. Ring buffers, autostop, buffer size and file format (4.6).
- Recording a packet trace, Apple. tcpdump on macOS and rvictl for iOS devices.
- pktmon start, Microsoft. Capture parameters and defaults; etl2pcap is on its sibling page.
- Debug running pods, Kubernetes. Ephemeral containers, --target and debugging profiles.
- Running containers, Docker. Default capabilities, NET_RAW included.
- capabilities(7), Linux man-pages project. CAP_NET_RAW and CAP_NET_ADMIN.