tcpdump
TCPDump Tutorial: A Beginner's Guide¶
tcpdump is a powerful command-line packet analyzer used for capturing and analyzing network traffic in real time. It's widely used by network administrators, security analysts, and developers for troubleshooting, security analysis, and debugging network applications.
1. Installing TCPDump¶
Most Linux distributions come with tcpdump pre-installed. If it's not installed, you can install it using:
-
Ubuntu/Debian:
-
CentOS/RHEL:
-
MacOS:
2. Basic Usage¶
Check Available Network Interfaces¶
This lists all available network interfaces where tcpdump can capture traffic.
Capture Packets on a Specific Interface¶
This captures packets on eth0 (replace with your actual interface).
Capture Only N Packets¶
Captures only 10 packets and then stops.
3. Filtering Traffic¶
tcpdump allows you to filter packets based on protocols, ports, IP addresses, etc.
Capture Traffic to/from a Specific IP¶
Captures packets to and from IP 192.168.1.100.
Capture Traffic from a Specific Source IP¶
Capture Traffic to a Specific Destination IP¶
Capture Only TCP or UDP Packets¶
Capture Packets for a Specific Port¶
Captures packets for HTTP traffic (port 80).
Capture Packets Based on Source or Destination Port¶
4. Writing and Reading Packet Captures¶
Save Packets to a File¶
This saves packets to a PCAP file for later analysis.
Read Packets from a File¶
5. Display Options¶
Verbose Output¶
Provides more details about packets.
Very Verbose Output¶
Even more detailed packet information.
Show Hex and ASCII Output¶
Displays packet content in hex and ASCII.
Show Only Packet Headers¶
Minimizes output details.
6. Advanced Filters¶
Capture Packets of a Specific Protocol¶
Captures ICMP (Ping) packets.
Capture Packets with a Specific String¶
Filters packets containing the word "password".
Filter Packets from a Specific Network¶
Captures packets from 192.168.1.0/24 subnet.
7. Analyzing Captured Packets with Wireshark¶
If you prefer a GUI for analyzing packets, open the .pcap file in Wireshark:
8. Running TCPDump in the Background¶
To run tcpdump as a background process:
This runs the capture in the background.
To stop it:
9. Common Use Cases¶
✔️ Monitor network activity:
✔️ Capture HTTP traffic:
✔️ Monitor SSH logins:
✔️ Find traffic related to a specific process:
Find the process using netstat or ss and filter by port.
10. Conclusion¶
tcpdump is a powerful tool for analyzing network traffic, debugging applications, and monitoring security threats. With the right filters and options, you can quickly find the information you need.