basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    OverviewLinuxtcpdump

    tcpdump: capturing and reading traffic in a Linux terminal

    tcpdump reads packets straight off a network interface through an AF_PACKET socket and the kernel BPF filter — on any server over SSH, with no GUI or heavy dependencies. It shows what hits the wire and comes back when application logs stay silent or contradict each other.

    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)

    1. AF_PACKETTPACKET_V3The kernel copies an incoming frame before the firewall (netfilter), and an outgoing frame after the firewall and NAT.
    2. BPF filtertcp port 443Compiled bytecode checks the requested header bytes right inside the kernel; non-matching frames are never copied.
    3. Capture buffer-B 2048 KBA queue in kernel memory between the network stack and tcpdump. If the process falls behind, dropped by kernel climbs.
    4. Output or file-nn / -w file.pcapEither decode headers as lines to the terminal, or dump raw frames into a .pcap file.

    terminal screen or .pcap file

    Three practical consequences follow from this pipeline:

    1. A BPF filter saves CPU and disk. Running tcpdump unfiltered and piping into | grep 443 copies all server traffic out of the kernel, decodes every packet to text, and throws most of it away. Putting 'tcp port 443' inside tcpdump discards unrelated packets in the kernel.
    2. Ingress and egress are tapped at different points. An incoming packet hits the capture before nftables and ufw rules, so a SYN dropped in input still appears in tcpdump. An outgoing packet hits the capture after output and postrouting — an outbound packet dropped by the firewall never appears in the dump (tcpdump sees packets before the firewall, Linux network stack).
    3. Writing with -w and 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 -r or Wireshark.

    Where it’s used

    The main case

    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 -w keeps CPU load low.
    • Shared .pcap format. A file captured by tcpdump on a server opens cleanly in Wireshark, tshark, scapy, and tcpdump -r with the same filter syntax.
    • Hard facts, no guesswork. Shows exact L2–L4 headers, checksums, TCP options (mss, wscale), DF flags, 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 .pcap into Wireshark.
    • Default traps. Without -nn it stalls on DNS PTR lookups and generates its own traffic (Always pass -nn to tcpdump); -i any loses the destination MAC and promiscuous mode (Limits of -i any); incoming packets appear before a firewall drop (tcpdump sees packets before the firewall).
    • Encrypted payloads stay opaque. Inside TLS, SSH, and WireGuard, tcpdump shows only metadata (IPs, ports, packet lengths, plaintext SNI in ClientHello), not HTTP bodies or commands.
    • Needs root privileges. Getting write flags wrong (-w without -C/-W limits) can fill a server’s root partition in minutes (Ring buffer for .pcap).

    Where to start

    If it doesn’t work

    tcpdump exits with You don't have permission to capture on that device

    Cause
    Capturing through AF_PACKET requires root privileges or the CAP_NET_RAW and CAP_NET_ADMIN capabilities on the binary.
    Fix
    Run it with sudo tcpdump …. If a regular user must capture without sudo, create a dedicated group, set setcap 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 tcpdump output 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_PACKET sits ahead of netfilter hooks: tcpdump recorded the frame, and nftables or ufw dropped it right after (tcpdump sees packets before the firewall).
    Fix
    Check drop counters in sudo nft list ruleset for the input and forward chains.

    Writing -w /root/cap.pcap or -w ./cap.pcap fails with Permission denied even under sudo

    Cause
    On Ubuntu, tcpdump drops privileges to the tcpdump system user (-Z tcpdump) before opening the output file, and an AppArmor profile also applies.
    Fix
    Write to a directory owned by the tcpdump user (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 -w instead of live decoding, narrow the BPF filter, and raise the kernel buffer with -B 16384 (in KiB).