Hello! Welcome to the final lesson in our module on Networking Fundamentals.
In our last session, we used the ss command to answer the question, "Who is talking on my network?" We learned how to identify listening services, see active connections, and pinpoint the exact process responsible for a given network port. This gave us a high-level view of the network state.
Today, we're going to dive much deeper to answer the question, "What are they talking about?". We will learn how to capture and perform a basic analysis of network traffic using tcpdump. This powerful command-line tool allows you to see the raw data packets being exchanged on your network, which is an indispensable skill for deep-level troubleshooting, security analysis, and understanding protocol behavior.
What is tcpdump?
tcpdump is a command-line packet analyzer. While graphical tools like Wireshark are excellent for detailed analysis on a desktop, tcpdump is essential for DevOps engineers because it can run on any remote server, inside a container, or in automated scripts where a GUI is not available. It gives you a direct, unfiltered view of the network traffic flowing through your machine.
To get a better understanding of what tcpdump is and when you'd use it, let's start with a short reading.
Network Traffic Analysis with tcpdump: Complete Guide
The article 'Network Traffic Analysis with tcpdump: Complete Guide' provides a great introduction to the tool.
Please read the 'Introduction' and the 'Understanding Packet Capture' sections. Focus on what tcpdump is and its common use cases, like troubleshooting connectivity and security analysis.
Installation and Setup
tcpdump is likely already installed on your Linux system, but if not, you can install it easily. Because it needs to access network hardware at a low level, tcpdump requires root privileges to run. You will need to prefix all tcpdump commands with sudo.
Network Traffic Analysis with tcpdump: Complete Guide
This guide provides clear, simple installation instructions.
Review the 'Prerequisites' section and use the appropriate command under 'Installation' for your system (Debian/Ubuntu or RHEL/CentOS). You can verify the installation with tcpdump --version.
Your First Packet Capture
Before you can capture traffic, you need to know which network interface to listen on. You can list the available interfaces with the -D flag.
sudo tcpdump -D
You'll see a numbered list of interfaces like eth0, lo (the loopback interface), and a special one called any that captures from all interfaces.
Now, let's start a basic capture. We'll use a few essential flags:
-i <interface>: Specifies the interface to listen on (e.g.,-i eth0).-c <count>: Stops after capturing a specific count of packets. This is useful to prevent an endless stream of output.-n: Disables IP address-to-hostname resolution.-nn: Disables both hostname and port-to-service name resolution (e.g., shows80instead ofhttp). This is highly recommended as it's faster and avoids generating extra DNS traffic during your capture.
Let's capture 5 packets on your primary network interface (e.g., eth0 or ens33—use the one from the -D list that isn't lo).
# Replace 'eth0' with your actual interface name
sudo tcpdump -i eth0 -c 5 -nn
You'll see a few lines of output fly by. If there is no traffic, you can generate some by running ping 8.8.8.8 in another terminal. Press Ctrl+C to stop the capture if you didn't use the -c flag.
Understanding the Output
The output of tcpdump can look cryptic at first, but it follows a standard format. Let's break it down.
An introduction to using tcpdump at the Linux command line
The Red Hat article 'An introduction to using tcpdump' has an excellent, detailed section that explains the format of a standard TCP packet capture.
Read section '3. Understanding the output format'. It breaks down a sample TCP packet line field by field, from the timestamp to the packet length.
Here's a quick summary of a typical line:13:02:14.911808 IP 192.168.1.10.39366 > 54.204.39.132.80: Flags [P.], ... length 112
- Timestamp:
13:02:14.911808 - Protocol:
IP(Internet Protocol) - Source:
192.168.1.10.39366(IP address and port) - Destination:
54.204.39.132.80(IP address and port) - TCP Flags:
[P.](Indicates the type of TCP packet, e.g., PUSH, ACK) - Length:
length 112(The size of the packet's payload in bytes)
Filtering: The Key to Meaningful Analysis
A busy server can generate thousands of packets per second. Capturing everything is rarely useful. The real power of tcpdump comes from its filtering engine, which lets you capture only the traffic you care about.
Filters are made up of expressions. The most common ones are:
host <ip_or_hostname>: Captures traffic to or from a specific host.net <network/cidr>: Captures traffic from or to an entire network range.port <number>: Captures traffic using a specific source or destination port.srcordst: Qualifiers to specify whether you're filtering on the source or destination (e.g.,src host 1.2.3.4).tcp,udp,icmp: Protocol filters.
You can combine these with logical operators: and (&&), or (||), and not (!). Use parentheses () to group expressions. If you use parentheses, you must wrap the entire filter expression in quotes.
Let's study some examples.
Network Traffic Analysis with tcpdump: Complete Guide
This guide offers a great reference for various filtering techniques.
Scan through the sections on 'Host Filters', 'Network Filters', 'Port Filters', 'Protocol Filters', and 'Combining Filters'. You don't need to memorize every command, but focus on understanding the syntax for each type of filter.
Practice with Filters
-
Capture
pingtraffic:pinguses the ICMP protocol. Let's capture only that. In one terminal, start this capture:sudo tcpdump -i eth0 -nn icmpIn a second terminal, ping a host:
ping -c 3 google.comYou will see only the ICMP echo requests and replies in your
tcpdumpwindow. -
Capture SSH traffic: Let's see traffic to a specific port.
# Capture traffic where the source or destination port is 22 (SSH) sudo tcpdump -i eth0 -nn -c 10 port 22If you are connected via SSH, you will see your own traffic.
-
Combine filters: Capture only HTTP traffic from a specific IP.
# Capture TCP traffic on port 80 coming from the source host 192.0.2.10 sudo tcpdump -i eth0 -nn 'tcp and port 80 and src host 192.0.2.10'
Test your understanding!
You are running a web server and a PostgreSQL database on the same machine. You want to debug a problem where you suspect the web server (running on port 80) is making unexpected connections to an external IP, 203.0.113.55.
Write a tcpdump filter to capture only the traffic that originates from your web server's port and is going to that specific external IP.
Show answer
sudo tcpdump -i eth0 -nn 'src port 80 and dst host 203.0.113.55'
src port 80: Captures traffic originating from port 80.dst host 203.0.113.55: Ensures the destination is the external IP you're investigating.and: Combines the two conditions.
Saving Captures and Reading from Files
Analyzing traffic in real-time is often difficult. A much better workflow is to capture traffic to a file and analyze it later. These files are saved in the .pcap (packet capture) format, which can also be opened by tools like Wireshark.
-w <filename.pcap>: Writes the raw packets to a file.-r <filename.pcap>: Reads packets from a file.
Workflow:
-
Capture to a file:
# Capture 1000 packets on port 443 (HTTPS) and save to a file sudo tcpdump -i eth0 -nn -c 1000 -w https_traffic.pcap port 443Notice that no packets are printed to the screen.
-
Analyze from the file: You can now analyze this capture offline, even on a different machine. The best part is that you can apply filters when reading the file.
# Read the file and show all packets tcpdump -nn -r https_traffic.pcap # Read the same file, but filter for a specific host tcpdump -nn -r https_traffic.pcap host 52.94.236.48
Basic Analysis: Looking Inside the Packets
So far, we've only looked at packet headers. To see the actual data payload, you can use:
-A: Prints the payload in ASCII. This is great for unencrypted protocols like HTTP, SMTP, or DNS.-X: Prints the payload in heX and ASCII.
Let's try to view some HTTP traffic. In one terminal, start this capture:
# The -A flag will show us the content of the packets
sudo tcpdump -i eth0 -nn -A port 80
In a second terminal, use curl to make a web request:
curl http://example.com
In your tcpdump output, you should now be able to see the plain-text HTTP GET request from your machine and the HTML response from the server. This is incredibly useful for debugging API calls or web service issues.
Conclusion
Mastering tcpdump is a significant step in becoming a proficient DevOps engineer. You've moved from observing that connections exist with ss to being able to intercept and inspect the raw data flowing between applications.
Key Takeaways:
tcpdumpis a command-line tool for capturing and analyzing network packets, essential for remote server troubleshooting.- Always run
tcpdumpwithsudoand use the-nnflags to prevent unwanted name resolution. - Use filters (
host,port,proto) to isolate the exact traffic you need to analyze. This is the most critical skill for usingtcpdumpeffectively. - For offline analysis, save captures to a
.pcapfile with-wand read them with-r. You can apply new filters every time you read the file. - Use the
-Aflag to inspect the payload of unencrypted protocols like HTTP.
This lesson concludes our module on networking fundamentals. You've learned about the OSI model, IP addressing, TCP vs. UDP, and how to use essential tools like ping, traceroute, ss, and now tcpdump.
In our next lesson, we will begin the Advanced Networking and Core Services module. We'll start by learning how to configure basic network interface settings, such as IP addresses and netmasks, on a Linux system. This will move us from observing the network to actively configuring it.