tcpdump

On this page 7

tcpdump is a command-line packet analyzer that captures network traffic passing through an interface, decodes the protocol headers and either prints a summary of each packet or saves the raw data to a file. In Linux networking, it is the standard first tool for answering a simple but decisive question: what is actually on the wire, as opposed to what configuration files and logs claim should be there.

tcpdump is built on the libpcap library, which provides portable packet capture and the filter compiler shared by many other tools. Its output format, the pcap file, is the common exchange format for network diagnostics, so a capture taken with tcpdump on a remote server can later be examined in a graphical analyzer such as Wireshark.

How tcpdump captures packets

On Linux, libpcap opens an AF_PACKET socket bound to the chosen interface, which receives a copy of every frame the kernel sees there. The kernel uses a memory-mapped ring buffer (TPACKET_V3) to hand frames to user space efficiently, and tcpdump then decodes the link, network and transport headers for display.

Filtering happens inside the kernel, not in tcpdump. The filter expression is compiled into classic BPF bytecode and attached to the socket, so packets that do not match are discarded before they are copied to user space. This design keeps CPU cost low even on busy links, and it explains why a precise filter matters more than any other option.

Capturing requires the CAP_NET_RAW capability, which normally means running as root or granting that capability to the binary. Many distributions also let tcpdump drop privileges after opening the socket, using the -Z option, to reduce the risk of parsing hostile traffic as root.

Essential tcpdump options

A small set of options covers most daily work. The table summarizes the flags that experienced administrators use most often and what each one changes.

OptionEffectPractical note
-iSelects the interface, or any for allThe any pseudo-interface uses a cooked header without MAC addresses
-n / -nnDisables name and port resolutionAvoids slow DNS lookups and extra traffic
-w / -rWrites to or reads from a pcap fileStore raw packets for later analysis
-sSets the snapshot lengthDefault 262144 bytes captures whole packets
-cStops after a number of packetsPrevents runaway captures
-v / -vvIncreases decoding detailShows TTL, options and checksums
-A / -XPrints payload as ASCII or hexUseful for plain-text protocols only
-C / -GRotates files by size or timeEssential for long-running captures
Frequently used command-line options

A typical diagnostic command is tcpdump -i eth0 -nn -w web.pcap port 443, which records all HTTPS traffic on one interface without resolving names. Adding -c 1000 or a rotation limit protects the disk on a busy host.

Writing capture filters with BPF syntax

The pcap-filter language combines primitives with the operators and, or and not. Primitives name a direction (src, dst), a type (host, net, port, portrange) and a protocol (tcp, udp, icmp, arp, ip6). Parentheses must be quoted or escaped so that the shell does not interpret them.

GoalFilter expression
Traffic to or from one hosthost 192.0.2.10
DNS queries and answersudp port 53
A subnet except SSHnet 10.1.0.0/16 and not port 22
TCP connection attempts onlytcp[tcpflags] & tcp-syn != 0 and tcp[tcpflags] & tcp-ack == 0
ICMP errors such as unreachableicmp and icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply
VLAN-tagged web trafficvlan and tcp port 80
Representative filter expressions

Byte-offset expressions that index into the TCP header give direct access to header fields, which is powerful but easy to get wrong. The -d option prints the compiled BPF program, a useful check that a complex filter means what the author intended.

tcpdump observes traffic; most neighboring tools in the Linux networking toolkit configure or generate it instead. Knowing the boundaries helps administrators pick the right instrument for each question.

ToolPrimary purposeRelation to tcpdump
Wireshark / tsharkDeep protocol dissectionReads tcpdump pcap files with richer decoding
netcatOpens raw TCP or UDP connectionsGenerates test traffic that tcpdump observes
ip commandConfigures addresses, routes and linksShows configuration, not live packets
nftables / iptablesFilters and rewrites packetsRules can be verified by capturing before and after
ethtoolInspects NIC settings and offloadsExplains odd checksums or oversized frames in captures
Where tcpdump fits among network tools

Where capture points matter, remember that tcpdump sees ingress packets before netfilter processes them and egress packets after it, so a packet visible in a capture may still be dropped by a firewall rule further along.

Pitfalls and interpretation errors

Reading a capture correctly requires knowledge of offloads and capture points, meaning knowledge of what the kernel and the network card do to packets. Several recurring misreadings lead administrators to chase problems that do not exist.

  • Bad checksum warnings on outgoing packets usually reflect checksum offload: the NIC fills in the value after the capture point.
  • Packets larger than the MTU often come from TSO or GRO, which merge segments before or after the capture.
  • Dropped by kernel counters at exit mean the ring buffer overflowed; a tighter filter or a larger -B buffer helps.
  • Encrypted payloads such as TLS cannot be read, but handshake metadata and timing remain visible.
  • On a switched network, a host only sees its own traffic unless a mirror port or TAP is used.

Captures also carry privacy and security risk: they can contain credentials from unencrypted protocols, personal data and internal addresses. Files should be stored with restrictive permissions, shared only when necessary and deleted when the investigation ends.

The idea in brief

Think of a network cable as a road and data as cars. tcpdump is a camera at the roadside that records every car that passes, noting where it came from, where it is going and what kind of vehicle it is.

Administrators use it when something does not work and they need proof of what really happened, rather than guesses. A filter tells the camera to record only the cars that matter, such as traffic to one website.

A practical scenario

An operations engineer receives reports that an internal web application on a Linux server times out for some users. Logs on the application show no failed requests, so the engineer runs tcpdump -i ens192 -nn -c 500 'tcp port 8443 and tcp[tcpflags] & tcp-syn != 0' to watch incoming connection attempts.

The capture shows SYN packets from one office subnet arriving and SYN-ACK replies leaving, but the clients keep retransmitting. A second capture on the office router reveals that the replies never arrive. The cause turns out to be an asymmetric route: replies leave through a different gateway whose stateful firewall drops them.

After the routing table is corrected, a final five-minute capture confirms complete three-way handshakes. The investigation took under an hour, because packet evidence ruled out the application and pointed directly at the network path.