In the world of network administration and cybersecurity, tcpdump stands as a premier command-line utility for capturing and analyzing network traffic.
This powerful tool allows you to peer into the raw data packets flowing through your system, making it indispensable for troubleshooting network issues, identifying security threats, and gaining a deep understanding of your network’s behavior.
While its command-line interface might seem daunting at first, this guide will walk you through everything from the basics to advanced techniques, empowering you to wield tcpdump with confidence.
Table of Contents
What is tcpdump?
tcpdump is a packet sniffer that intercepts and displays TCP/IP and other packets being transmitted or received over a network.
Because it’s a command-line tool, it is ideal for use on remote servers or devices where a graphical user interface (GUI) is not available. It’s also scriptable, meaning you can automate network captures for scheduled analysis.
The data captured by tcpdump can be displayed in real-time or saved to a file for later, more in-depth inspection.
Getting Started: Installation and First Capture
In many Linux distributions, tcpdump comes pre-installed. You can check if it’s on your system by opening a terminal and typing:
which tcpdump
If it’s not installed, you can easily add it using your distribution’s package manager. For example, on Debian-based systems like Ubuntu, you would use:
sudo apt-get install tcpdump
On Red Hat-based systems, you would use:
sudo dnf install tcpdump
Once installed, you’ll need elevated privileges to run tcpdump, so most commands will be prefixed with sudo.
See also: Mastering the Linux Command Line — Your Complete Free Training Guide
Listing Network Interfaces
Before you start capturing, it’s helpful to know which network interfaces are available. You can list them with the -D option:
sudo tcpdump -D
This will display a numbered list of interfaces, such as eth0 for a wired connection or wlan0 for a wireless one. There’s also a special interface called any that allows you to capture packets from all active interfaces.
Your First Packet Capture
To begin capturing network traffic on a specific interface, use the -i flag:
sudo tcpdump -i eth0
This command will start capturing packets on the eth0 interface and display the output in your terminal.
To stop the capture, press Ctrl+C. The initial output might seem like a fast-scrolling wall of text, but we’ll soon learn how to tame it.
Understanding the tcpdump Output
A single line of tcpdump output for a TCP packet typically looks like this:
14:10:32.52 client.1083 > server.telnet: Flags [P.], seq 1:2(1), ack 2, win 4096
Let’s break down the components of this output:
- Timestamp: The first field is a timestamp (
14:10:32.52) indicating when the packet was captured. - Source and Destination: This shows the source (
client.1083) and destination (server.telnet) hosts and their respective ports. The>indicates the direction of traffic flow. - TCP Flags: The
Flags [P.]section is crucial for understanding the state of a TCP connection. Common flags include:[S](SYN): Initiates a connection.[.](ACK): Acknowledges the receipt of a packet.[F](FIN): Finishes a connection.[R](RST): Resets a connection.[P](PSH): Pushes data to the application.
- Sequence and Acknowledgment Numbers:
seq 1:2(1)andack 2are part of the TCP protocol that ensures data is delivered reliably and in the correct order. - Window Size:
win 4096refers to the amount of data the sender can transmit before receiving an acknowledgment.
Key Command-Line Options
To refine your captures, you’ll use various command-line options:
n: Disables the conversion of IP addresses to hostnames, which can speed up the capture and make the output clearer.nn: Similar ton, but also prevents the conversion of port numbers to service names (e.g., shows80instead ofhttp).c <count>: Exits after capturing a specific number of packets. This is useful for grabbing a quick sample of traffic.v,vv,vvv: Increases the verbosity of the output, providing more detailed information about the packets.X: Displays the packet’s content in both hexadecimal and ASCII, which is useful for inspecting the payload.s <size>: Sets the “snaplen” or snapshot length, which is the amount of data to capture for each packet. Usings0ensures that you capture the full packet.
The Power of Filtering
tcpdump‘s true strength lies in its powerful filtering capabilities, which allow you to isolate the exact traffic you need to analyze. Filters are applied as an expression at the end of the tcpdump command.
Filtering by Host
You can capture traffic to or from a specific IP address or hostname. To be more specific, you can use src or dst to filter by source or destination.
- Traffic involving a specific host:
sudo tcpdump host 192.168.1.1 - Traffic from a specific source:
sudo tcpdump src 192.168.1.1 - Traffic to a specific destination:
sudo tcpdump dst 192.168.1.1
Filtering by Port
To monitor traffic on a particular port, which is often tied to a specific service (e.g., port 80 for HTTP), use the port filter.
- Traffic on a specific port:
sudo tcpdump port 80 - Traffic from a specific source port:
sudo tcpdump src port 1234 - Traffic to a specific destination port:
sudo tcpdump dst port 443
You can also filter for a range of ports: sudo tcpdump portrange 21-25
Filtering by Protocol
You can easily filter for specific protocols like tcp, udp, or icmp.
- Capture only UDP traffic:
sudo tcpdump udp - Capture only ICMP traffic (like pings):
sudo tcpdump icmp
Combining Filters with Logical Operators
For more granular control, you can combine filters using logical operators: and (&&), or (||), and not (!).
- Capture TCP traffic to or from host
192.168.1.5on port443:sudo tcpdump -n host 192.168.1.5 and tcp port 443 - Capture all traffic on port 80 or 443:
sudo tcpdump -n 'port 80 or port 443' - Capture all traffic except for SSH (port 22):
sudo tcpdump -n not port 22
Saving and Reading Captures
For more thorough analysis, especially of a large volume of traffic, it’s best to save the captured packets to a file. The standard format for this is .pcap.
Writing to a File
The -w option allows you to write the captured data to a file:
sudo tcpdump -i eth0 -w traffic_capture.pcap
This will save all captured traffic from the eth0 interface into a file named traffic_capture.pcap.
Reading from a File
You can then analyze this saved file at any time using tcpdump with the -r option:
tcpdump -r traffic_capture.pcap
You can even apply filters to a saved capture file to analyze different aspects of the same data without having to recapture it.
Practical Use Cases and Examples
Here are some real-world scenarios where tcpdump proves invaluable:
- Troubleshooting Web Traffic: To see if a web server is receiving requests, you can monitor HTTP traffic on port 80:
sudo tcpdump -i eth0 -n 'port 80' - Analyzing DNS Queries: To troubleshoot DNS issues, you can capture traffic on port 53:
sudo tcpdump -i any -n port 53 - Detecting a Port Scan: A flood of packets with the SYN flag set can indicate a port scan. You can filter for these packets:
tcpdump 'tcp[tcpflags] & tcp-syn != 0' - Monitoring DHCP Traffic: To troubleshoot issues with obtaining an IP address, you can monitor DHCP traffic on ports 67 and 68:
sudo tcpdump -v -n 'port 67 or port 68'
Synergy with Other Tools: Wireshark
While tcpdump is excellent for capturing and performing quick analysis from the command line, sometimes a graphical interface can provide a more intuitive way to inspect data. This is where Wireshark, a popular GUI-based network protocol analyzer, comes in.
You can use tcpdump to capture traffic on a remote server and then transfer the .pcap file to your local machine for a more detailed, visual analysis in Wireshark. This combination gives you the best of both worlds: the lightweight, remote capturing capabilities of tcpdump and the powerful graphical analysis features of Wireshark.
By mastering the commands and techniques outlined in this guide, you’ll be well-equipped to tackle a wide range of networking challenges. tcpdump is a versatile tool that rewards practice with a deeper understanding of the intricate conversations happening on your network.
20 Advanced Tcpdump Examples On Linux




