What problem it solves
Web server, proxy, and application logs only record traffic that reaches the program’s socket. When a
packet disappears on a router, hits an MTU limit, gets a RST from another host, or leaves with the wrong
source address after NAT, service logs show nothing.
tcpdump takes a copy of frames directly on the kernel network interface. In a few seconds you can see:
Did the packet arrive at all
Whether incoming SYN packets reach the interface, whether the server replies with SYN-ACK or RST, and
whether the client retransmits segments because of packet loss.
What is inside the packets
Which Host and SNI leave in TLS ClientHello, which DNS query an app sends, and what IP addresses and
ports come out after SNAT and DNAT.
It installs as a single package (apt install tcpdump), takes under two megabytes, and runs over a plain
SSH session with no GUI or agents. When traffic volume is high or you need deep L7 decoding for HTTP/2 or
TLS, write a .pcap file on the server with -w and open it locally in Wireshark.
How capture works: AF_PACKET, BPF, and libpcap
tcpdump builds on libpcap and two Linux kernel mechanisms: an AF_PACKET socket (which receives raw L2
frames) and the BPF virtual machine (which drops non-matching frames in the kernel before copying anything
into process memory).
network interface (NIC / veth)
- AF_PACKET
TPACKET_V3The kernel copies an incoming frame before the firewall (netfilter), and an outgoing frame after the firewall and NAT. - BPF filter
tcp port 443Compiled bytecode checks the requested header bytes right inside the kernel; non-matching frames are never copied. - Capture buffer
-B 2048 KBA queue in kernel memory between the network stack andtcpdump. If the process falls behind,dropped by kernelclimbs. - Output or file
-nn / -w file.pcapEither decode headers as lines to the terminal, or dump raw frames into a.pcapfile.
terminal screen or .pcap file
Three practical consequences follow from this pipeline:
- A BPF filter saves CPU and disk. Running
tcpdumpunfiltered and piping into| grep 443copies all server traffic out of the kernel, decodes every packet to text, and throws most of it away. Putting'tcp port 443'insidetcpdumpdiscards unrelated packets in the kernel. - Ingress and egress are tapped at different points. An incoming packet hits the capture before
nftablesandufwrules, so aSYNdropped ininputstill appears intcpdump. An outgoing packet hits the capture afteroutputandpostrouting— an outbound packet dropped by the firewall never appears in the dump (tcpdump sees packets before the firewall, Linux network stack). - Writing with
-wand printing to a terminal don’t match in speed. On a busy interface, printing lines over SSH overflows the buffer quickly. For a clean capture, write packets to.pcap(-w) first and inspect them afterward with-ror Wireshark.
Where it’s used
Diagnosing drops and timeouts
Separates a firewall DROP from a closed port (RST), asymmetric routing, and an MTU black hole in under
a minute (tcpdump troubleshooting).
Debugging NAT, proxies, and containers
Run nsenter -t <PID> -n tcpdump to inspect traffic inside a scratch container without installing tools
into the image (Linux network stack).
Catching rare intermittent glitches
A ring buffer (-C 100 -W 10 -w) runs on a server for hours without filling the disk and keeps the last
packets right before a failure (Ring buffer for .pcap).
Handing off a dump to Wireshark
Capture a narrow .pcap on the server with a BPF filter, pull it over scp, and inspect stream flows,
retransmissions, and TLS on your laptop.
Other ways to solve it
| Environment | Strength | Limitation | |
|---|---|---|---|
| tcpdump | Linux/BSD/macOS terminal | tiny package, installed almost everywhere, fast BPF filter, low overhead with -w |
shallow L7 decoding (no HTTP/2 or gRPC stream reassembly, no TLS decryption) |
| tshark | terminal (Wireshark engine) | thousands of L7 protocol dissectors, -T fields extraction, Wireshark display filters |
heavier dependencies, higher memory and CPU use; on production usually reads a saved .pcap |
| Wireshark | desktop GUI | TCP stream graphs, Follow TCP Stream, color highlighting for retransmissions and errors | don’t run it on a server — feed it a .pcap captured by tcpdump |
| dumpcap | terminal (Wireshark collector) | even lower capture overhead at 10G+, pcapng format and multi-interface capture |
barely decodes traffic on screen — built for writing files |
| ss / ip / nft | Linux terminal | instant view of socket states, routes, and firewall counters without tapping frames | don’t show packet payloads or the exact sequence of frames on the wire |
| If… | Use |
|---|---|
| you need a quick check over SSH to see if packets arrive and how the server replies | tcpdump -nn |
| you need to capture traffic on a busy server and inspect it carefully later | tcpdump -w on the server and Wireshark on your computer |
you need to pull specific HTTP, DNS, or TLS fields from a saved .pcap in a script |
tshark -r file.pcap -Y … -T fields |
| you first need to check whether a process is listening and where a route points | ss -tulnp and ip route get (Server network diagnostics) |
What you get and what it costs
What you get
- Ready on any server. One small package with no GUI or agents, and the same flags across Linux, BSD, and macOS.
- In-kernel filtering. A BPF expression drops unrelated traffic before copying to user space; writing
with
-wkeeps CPU load low. - Shared
.pcapformat. A file captured bytcpdumpon a server opens cleanly in Wireshark,tshark,scapy, andtcpdump -rwith the same filter syntax. - Hard facts, no guesswork. Shows exact L2–L4 headers, checksums, TCP options (
mss,wscale),DFflags, and ICMP replies.
What it costs
- Raw text instead of stream tracking. Dozens of parallel connections interleave into one feed; for
longer investigations you have to filter by port or move the
.pcapinto Wireshark. - Default traps. Without
-nnit stalls on DNS PTR lookups and generates its own traffic (Always pass -nn to tcpdump);-i anyloses the destination MAC and promiscuous mode (Limits of -i any); incoming packets appear before a firewalldrop(tcpdump sees packets before the firewall). - Encrypted payloads stay opaque. Inside TLS, SSH, and WireGuard,
tcpdumpshows only metadata (IPs, ports, packet lengths, plaintextSNIin ClientHello), not HTTP bodies or commands. - Needs
rootprivileges. Getting write flags wrong (-wwithout-C/-Wlimits) can fill a server’s root partition in minutes (Ring buffer for .pcap).
Where to start
- tcpdump: capturing and reading traffic in a Linux terminal
- tcpdump basics: running captures, reading output, flags, and .pcap filesPicking an interface, flags
-nn,-c,-v,-A, writing.pcapfiles, and reading every field of an output line. - BPF filters in tcpdump: syntax, logic, and the tcp[13] byteSelecting the packets you need in the kernel:
host,net,port, boolean logic, thetcp[13]flags byte, and checking bytecode with-d. - Troubleshooting failures with tcpdump: five practical scenariosFive real dump walkthroughs: firewall
DROP,RST, MTU black hole, DNS, TLS SNI, and capturing inside a container. - tcpdump cheat sheet: flags, BPF filters, and .pcap captureLaunch flags, ready-to-use BPF filters, file rotation, and reading
.pcapdumps on one screen.
If it doesn’t work
tcpdump exits with You don't have permission to capture on that device
- Cause
- Capturing through
AF_PACKETrequiresrootprivileges or theCAP_NET_RAWandCAP_NET_ADMINcapabilities on the binary. - Fix
- Run it with
sudo tcpdump …. If a regular user must capture withoutsudo, create a dedicated group, setsetcap cap_net_raw,cap_net_admin=eip /usr/bin/tcpdump, and restrict execution of the binary to that group.
Right after launch, the screen floods with thousands of lines from your own SSH session
- Cause
- An unfiltered capture grabs the packets the server uses to send
tcpdumpoutput back to your terminal, creating a feedback loop. - Fix
- Exclude your session in the filter:
sudo tcpdump -nn -i eth0 'not port 22'(or your custom SSH port).
Incoming SYN packets appear in the dump, yet the app never receives them and the client times out
- Cause
- On ingress,
AF_PACKETsits ahead ofnetfilterhooks:tcpdumprecorded the frame, andnftablesorufwdropped it right after (tcpdump sees packets before the firewall). - Fix
- Check
dropcounters insudo nft list rulesetfor theinputandforwardchains.
Writing -w /root/cap.pcap or -w ./cap.pcap fails with Permission denied even under sudo
- Cause
- On Ubuntu,
tcpdumpdrops privileges to thetcpdumpsystem user (-Z tcpdump) before opening the output file, and an AppArmor profile also applies. - Fix
- Write to a directory owned by the
tcpdumpuser (or/tmp/), or disable privilege dropping with-Z root(Ring buffer for .pcap).
The summary at exit shows a growing packets dropped by kernel counter
- Cause
- The kernel capture ring buffer (2 MiB by default in
libpcap) fills up while the process is busy formatting terminal output or waiting on disk. - Fix
- Write raw packets to a file with
-winstead of live decoding, narrow the BPF filter, and raise the kernel buffer with-B 16384(in KiB).