Marcio Cunha

How to inspect network packets with tcpdump on Linux

Learn how to capture, filter, and analyze network traffic between services on Linux using tcpdump to quickly diagnose communication failures.

Marcio Cunha•3 min
Also available in:PortuguêsEspañol
Summary
  • Tcpdump intercepts raw network traffic directly from the network interface for detailed analysis.
  • BPF filters prevent visual clutter by isolating specific IP addresses, ports, or protocols.
  • Saving capture files allows for thorough offline investigation using graphical tools later.
  • Rapid microservice troubleshooting relies on correctly reading TCP flags and packet payloads.
  • Monitoring the correct network interface prevents false negatives during troubleshooting.

Why tcpdump is the ultimate network diagnostic Swiss army knife

When two microservices stop talking and application logs just repeat generic connection error messages, the big question arises: is the bug in the code, the firewall, or the underlying infrastructure? That is precisely when tcpdump steps in as the ultimate command-line tool on Linux. In practice, it acts like an invisible audio recorder intercepting all data packets—the small chunks into which information is divided to travel across the network—entering and leaving your machine.

Unlike complex graphical interfaces, tcpdump runs straight from the terminal, making it indispensable for inspecting remote servers via SSH where no visual desktop exists. For anyone handling complex infrastructure diagnostics, extracting precise network traffic insights complements essential sysadmin skills, much like knowing how to read huge log files in Linux without crashing when investigating high-severity production incidents.

Preparing the ground and choosing the right interface

Before firing random commands, you need to know which network port is listening to the traffic. On Linux, every physical or virtual connection passes through a network interface, such as eth0 for the main adapter or lo for local loopback traffic. If you try to listen on the wrong interface, you will only see digital silence, even if your application is generating thousands of requests per second.

To list available system interfaces and pick the correct target, run the standard listing command:

ip link show

Once the interface is identified, the most basic tcpdump command opens listening without filters. In practice, this dumps every packet passing through the card directly onto your screen, creating a rapid torrent of data that is hard to follow visually. For this reason, running the raw command on busy production environments is rarely a good idea.

Filtering traffic to find errors quickly

The great secret to mastering tcpdump is not capturing everything, but applying smart filters based on Berkeley Packet Filter (BPF) syntax. These filters act like a rigorous sieve that discards all background noise and lets through only the traffic relevant to your investigation, such as a specific port or the IP address of an unresponsive microservice.

If you need to inspect requests destined for port 443 of an HTTPS service, or check if the database on port 5432 is accepting connections, filter syntax becomes your best ally. Here is how to structure that command in practice:

sudo tcpdump -i eth0 -nn 'port 5432'

The -nn flag prevents the utility from wasting precious time trying to resolve IP addresses into domain names or numeric ports into standard service names (like converting 80 to http), drastically speeding up output display. This attention to diagnostic performance is analogous to rigorous NVMe SSD health monitoring on Linux, where every millisecond of system response delay affects end-user experience.

Interpreting packets and finding the bottleneck

Each line printed by tcpdump tells the story of a microsecond in server communication. Understanding the alphabet soup of TCP flags—like [S] for SYN (connection start), [S.] for SYN-ACK (acknowledgment), and [F] for FIN (termination)—reveals exactly where communication stalled. If a service repeatedly sends [S] packets with no reply, you have found a firewall rule blocking the port or a dead service.

Often, real-time terminal analysis is too fast, causing you to miss the exact moment of an intermittent failure. To solve this, we can record raw traffic into a digital file for calmer inspection, or examine network flows by combining this technique with decrypting HTTPS traffic using session keys in Wireshark.

To safely save the capture file without corrupting binary packets, use the -w flag:

sudo tcpdump -i eth0 -nn -w network_diagnostic.pcap 'host 192.168.1.50 and port 8080'

Final thoughts on network diagnostics

Mastering tcpdump transforms an engineer or system administrator from an error guesser into a surgical investigator. Knowing how to isolate interfaces, apply precise BPF filters, and interpret TCP packet lifecycles eliminates hours of frustration when debugging distributed microservices across Linux environments.

Understanding the invisible flow of data powering modern infrastructure ensures operational resilience and accelerates critical incident resolution. Practice these commands in staging environments, get familiar with reading flags, and keep this powerful utility close at hand in your terminal toolkit.