This guide explains how to use tcpdump, the command-line packet analyzer, to capture and filter network traffic. You’ll learn the essential commands, options, and filter syntax to inspect packets in real time or from a saved file, with practical examples you can run immediately.
What Is tcpdump and Why Use It?
tcpdump is a free, open-source packet analyzer that runs on most Unix-like operating systems, including Linux, macOS, and BSD. It captures raw network packets passing through a network interface and prints them to the terminal or saves them to a file. Unlike full-featured GUI tools like Wireshark, tcpdump is lightweight, scriptable, and works on remote servers without a graphical environment. It is the go-to tool for troubleshooting network issues, verifying security rules, and learning how protocols behave.
Installing tcpdump
Most Linux distributions include tcpdump in their package repositories. To install it, use your system’s package manager:
- Debian/Ubuntu:
sudo apt install tcpdump - RHEL/CentOS/Fedora:
sudo dnf install tcpdump(oryumon older systems) - macOS: tcpdump is pre-installed; if not, install via Homebrew with
brew install tcpdump
You need root privileges (sudo) to capture packets because reading raw network data is a privileged operation. After installation, verify with tcpdump --version.
Basic Capture Syntax and Options
The simplest command captures all packets on the default interface:
sudo tcpdump
This will run until you press Ctrl+C, then display a summary of how many packets were captured and dropped. To specify a network interface, use -i:
sudo tcpdump -i eth0
Common useful options:
-c count— stop after capturing a specific number of packets (e.g.,-c 10)-n— don’t resolve hostnames (faster, avoids DNS lookups)-nn— don’t resolve hostnames or port names (shows numeric IPs and ports)-v,-vv,-vvv— increase verbosity for more packet details-w file.pcap— write raw packets to a file for later analysis-r file.pcap— read packets from a saved file instead of live capture-s 0— capture the entire packet (default is 262144 bytes, which is fine for most cases)-e— print the link-layer header (MAC addresses) on each line
Example: capture 5 packets on eth0 without name resolution:
sudo tcpdump -i eth0 -c 5 -n
Filtering Traffic with Expressions
tcpdump’s real power lies in its filter language (Berkeley Packet Filter, BPF). Filters let you select only the traffic you care about, reducing noise and making analysis manageable. Basic filters include:
- By host:
host 192.168.1.10— packets to or from that IP - By network:
net 192.168.1.0/24— packets to or from that subnet - By port:
port 80— traffic on port 80 (HTTP) - By protocol:
tcp,udp,icmp— only that protocol - By source/destination:
src host 10.0.0.1,dst port 443
Combine filters with logical operators and (&&), or (||), and not (!). For example, to capture HTTP traffic from a specific host:
sudo tcpdump -i eth0 host 192.168.1.10 and port 80
You can also filter by TCP flags, like SYN packets:
sudo tcpdump -i eth0 'tcp[tcpflags] & tcp-syn != 0'
This is useful for spotting connection attempts. Note that when using special shell characters like & or parentheses, wrap the filter in single quotes.
Reading and Analyzing Output
By default, tcpdump prints a line for each packet. A typical line looks like:
12:34:56.789012 IP 192.168.1.10.54321 > 93.184.216.34.80: Flags [S], seq 123456789, win 64240, options [mss 1460], length 0
This shows the timestamp, protocol (IP), source IP and port, destination IP and port, TCP flags ([S] for SYN), sequence number, window size, options, and payload length. For UDP, you’ll see UDP and length instead of flags. ICMP packets show type and code, e.g., ICMP echo request.
To see the full packet contents in hex and ASCII, use -X (hex + ASCII) or -A (ASCII only). This is helpful when inspecting application data, like HTTP requests:
sudo tcpdump -i eth0 -A port 80
For more detailed protocol decoding, combine with -vvv to see fields like TCP options and IP flags.
Saving Captures and Using Wireshark
When you need to analyze traffic later or share it with a colleague, save the capture to a file with -w:
sudo tcpdump -i eth0 -w capture.pcap
This writes the raw packets in pcap format, which can be read by tcpdump, Wireshark, and many other tools. To read a saved file, use -r:
tcpdump -r capture.pcap -n
You can apply filters when reading a file, just as with live capture. A common workflow is to capture a large file on a server, then open it in Wireshark on your desktop for deep inspection. To avoid filling your disk, use -c to limit packet count or -W and -G for rotation (e.g., -G 300 -W 5 creates a new file every 5 minutes, keeping 5 files).
Common Mistakes and Troubleshooting
- Forgetting sudo — you’ll get an error like “You don’t have permission to capture on that device.” Always run as root or with sudo.
- Wrong interface name — use
tcpdump -Dto list all available interfaces and their names. - Filter not matching — double-check your syntax; often a missing
andor wrong port number causes no output. Use-vto see if tcpdump is actually seeing packets. - No output but capture running — the filter may be too restrictive, or the traffic is on a different interface. Try without a filter to confirm traffic is present.
- Dropped packets — tcpdump reports “X packets dropped by kernel”. This happens when the capture buffer is too small. Increase it with
-B 4096(buffer size in KB).
Also note that tcpdump doesn’t capture traffic between virtual machines on the same host unless you use the right interface (e.g., a virtual bridge). For encrypted traffic (HTTPS, SSH), tcpdump only shows the encrypted payload, not the content.
Frequently Asked Questions
How do I capture traffic on a specific port?
Use port followed by the port number. For example, sudo tcpdump -i eth0 port 443 captures all HTTPS traffic. You can also specify a range like portrange 8000-9000.
Why does tcpdump show “0 packets captured”?
This usually means the filter didn’t match any traffic, or you’re listening on the wrong interface. Check that the interface is active (with ip link show) and that the filter is correct. Try capturing without a filter to see if any traffic exists.
Can tcpdump capture traffic from other devices on the network?
Only if your network interface supports promiscuous mode and the traffic is broadcast or routed through your machine. tcpdump sets promiscuous mode by default when capturing, but on a switched network you’ll only see your own traffic and broadcasts. To capture other devices’ traffic, you’d need a hub, a SPAN port on a switch, or use ARP spoofing (which is beyond tcpdump’s scope).
How do I stop tcpdump from resolving hostnames?
Use the -n flag to disable hostname resolution, and -nn to also disable port name resolution. This makes output faster and avoids DNS lookups that could slow down the capture.
Is tcpdump safe to run on a production server?
Yes, tcpdump is a read-only tool; it doesn’t modify traffic or inject packets. However, it can consume CPU and disk space if you capture a lot of traffic. Use filters to limit what you capture, and stop the capture when done. Always run it with appropriate permissions and be mindful of sensitive data in the packets.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Arctic Sea Ice Forecasts Disagree: What to Do
- MySQL Workbench to VPS: SSH Tunnel Setup
- Bluesky Beginners Guide: How to Get Started5.00 min/km
- LG V20 Won’t Turn On? Fixed
- Snagit vs Camtasia: Which One Do You Need?
- Scale a Small Business Without Losing Culture