Packet Capture & Analysis
tcpdump syntax and BPF expressions, Wireshark display filters, pcap format, and decoding protocol layers from a capture.
Overview
Overview Packet capture and analysis is a fundamental SRE and network engineering skill for diagnosing connectivity issues, performance problems, protocol errors, and security incidents that cannot be solved with higher-level metrics and logs alone. When an application reports "connection refused" or "timeout" and log analysis is inconclusive, packet-level inspection is often the only way to determine ground truth.
tcpdump is the standard command-line packet capture tool available on nearly every Linux system. It uses the libpcap library and supports Berkeley Packet Filter (BPF) expressions for efficient kernel-level packet filtering. BPF filtering happens in the kernel before packets are copied to userspace, making tcpdump efficient even on high-traffic interfaces. Understanding BPF syntax — filtering by host, port, protocol, direction, and combining expressions with and/or/not — is essential for targeted captures that don't overwhelm disk or memory.
Wireshark is the standard GUI analysis tool that processes pcap files captured by tcpdump or libpcap. Wireshark dissects protocol layers, reconstructs TCP streams, identifies retransmissions and out-of-order packets, decodes application-layer protocols, and provides powerful display filter syntax separate from capture filters. The combination of tcpdump for capture and Wireshark for offline analysis is the standard production workflow.
In cloud environments, traditional packet capture requires some adaptation. On AWS, VPC Traffic Mirroring mirrors ENI traffic to a network monitoring appliance. EC2 instances can run tcpdump directly on their interfaces. In Kubernetes, tcpdump can be run in a privileged pod or using tools like kubectl-sniff that inject a tcpdump sidecar. Understanding how encapsulation affects what you see in a capture — VXLAN outer headers, TLS encryption, VPN tunnels — is critical for correct interpretation.
Key concepts
- BPF (Berkeley Packet Filter)
- pcap File Format
- TCP Stream Reassembly
- Wireshark Display Filters
- Promiscuous Mode
Interview questions
- Write tcpdump commands for the following capture scenarios: capturing HTTP traffic, capturing DNS queries, capturing only TCP RST packets, and capturing traffic to/from a specific host while excluding SSH.
- How do you use Wireshark to analyze a TCP connection problem? Walk through diagnosing a scenario where connections complete the handshake but data transfer is slow.
Sign up free to read the full topic
More Networking topics
Start free on Cariara · Coding problems · System design studio