tcpdump – CLI Packet Capture
What is tcpdump?
Definition: tcpdump is a powerful command-line packet analyzer for Unix/Linux systems. It allows network administrators and security professionals to capture and inspect network traffic for real-time troubleshooting and deep protocol analysis.
Purpose & Role in Network Analysis
- Diagnose connectivity issues
- Identify abnormal or malicious activity
- Verify network configurations and protocol operations
- Analyze protocol behavior and performance
Basic tcpdump Usage
Syntax:tcpdump [options] [filter_expression]Capture on an interface:
sudo tcpdump -i eth0Tip: Use
sudo for root privileges, which are usually required.
Filtering Captures (Focus Traffic)
| Purpose | Command Example |
|---|---|
| By host | sudo tcpdump host 192.168.1.5 |
| By port | sudo tcpdump port 80 |
| By protocol | sudo tcpdump icmp |
| Combined filter | sudo tcpdump tcp and port 22 and host 10.0.0.5 |
Capture File Management
- Write to file (PCAP format):
sudo tcpdump -i eth0 -w capture.pcap(view later in Wireshark) - Read from file:
tcpdump -r capture.pcap
Output & Display Options
| Option | Description |
|---|---|
| -v, -vv, -vvv | Increase output verbosity (more protocol details) |
| -X | Print packets in hex + ASCII for deep inspection |
| -c [num] | Capture [num] packets then stop (e.g., -c 50) |
| timeout | Capture for a time limit (e.g., timeout 60s tcpdump -i eth0) |
Common Protocols & Ports
| Protocol/Port | tcpdump Filter Example |
|---|---|
| TCP | tcpdump tcp |
| UDP | tcpdump udp |
| ICMP | tcpdump icmp |
| ARP | tcpdump arp |
| DNS (port 53) | tcpdump port 53 |
Integrating with Other Tools
- Open .pcap files in Wireshark for graphical analysis
- Filter output with grep:
sudo tcpdump -i eth0 | grep "192.168.1.5"
Security & Permissions
- Root privileges required for most interfaces
- Only capture on networks where you are authorized!
- Data may include sensitive information—respect privacy and legal regulations
Advanced Filtering Expressions
Combine boolean operators (and, or, not):
sudo tcpdump 'src net 192.168.1.0/24 and not dst port 22'Tip: Use single quotes for complex filters on the CLI.
Best Practices
- Use filters to minimize capture size and system impact
- Document each capture’s purpose, time, and filter
- For long captures, use file rotation (
-Cfor size,-Gfor time)
Example Scenarios
- Verify HTTP Traffic:
sudo tcpdump -i eth0 port 80 - Capture all ICMP (for ping troubleshooting):
sudo tcpdump -i eth0 icmp - Write to file, analyze later:
sudo tcpdump -i eth0 -w web_traffic.pcap- Later:
tcpdump -r web_traffic.pcap
Hands-On tcpdump Practice Tasks
| Task | Example Command | Goal |
|---|---|---|
| Capture all traffic on interface eth0 for 30 seconds | sudo timeout 30s tcpdump -i eth0 |
Observe live packet flow |
| Filter by source IP address | sudo tcpdump -i eth0 src host 192.168.1.100 |
See only traffic from one host |
| Capture only HTTP (port 80) traffic | sudo tcpdump -i eth0 port 80 |
Inspect web packets |
| Save 100 packets to a file & read it later | sudo tcpdump -i eth0 -c 100 -w sample.pcaptcpdump -r sample.pcap |
Work with .pcap files |
| View packet contents in hex and ASCII | sudo tcpdump -X -i eth0 |
Protocol headers and data |
| Monitor DNS requests and responses | sudo tcpdump -i eth0 port 53 |
Watch DNS troubleshooting |
| Advanced: ICMP except a host | sudo tcpdump 'icmp and not host 10.1.1.1' |
Exclude noisy/irrelevant hosts |
tcpdump “Gotchas” for Exam and Real Networks
- If you see “permission denied,” use
sudo - Find your interface:
tcpdump -D - Always filter where possible to avoid overload
- Add
-Xfor payload details; use-vvvfor more protocol info - For long sessions, rotate files (
-Cfor MB,-Gfor seconds) - Never capture traffic without proper authorization
Summary Table: tcpdump Command Options
| Option | Function | Example |
|---|---|---|
| -i | Select interface | -i eth0 |
| -w | Write to .pcap file | -w capture.pcap |
| -r | Read from file | -r capture.pcap |
| -c | Packet count limit | -c 50 |
| -X | Show packet data (hex+ASCII) | -X |
| -v, -vv, -vvv | Increase verbosity | -vvv |
| timeout | Set time limit | timeout 60s ... |
| -C, -G | File rotation (size/time) | -C 10 -G 60 |
Exam Tips & Key Points
- Know basic syntax, filters, file operations, and permissions
- Always explain legal/ethical aspects in answers
- Mention Wireshark for advanced/graphical analysis
- Be ready to describe troubleshooting scenarios