Save to File with -w limited.pcap
In this step, you'll learn how to save captured network traffic to a file for later analysis. The -w
option in tcpdump creates a packet capture (pcap) file that preserves all the captured network data. This is particularly useful when you need to examine traffic patterns offline or share captures with colleagues.
- Before starting, make sure your terminal is in the correct working directory. Type:
cd ~/project
This ensures all your capture files will be saved in the designated project folder.
- The
-w
flag tells tcpdump where to store the captured packets. The basic command structure is:
tcpdump -w [filename] [other options]
The filename should end with .pcap
extension, which is the standard format for packet capture files.
- Now let's combine all the options we've learned so far into a practical example. This command will:
- Capture exactly 500 packets (
-c 500
)
- Limit each packet to 128 bytes (
-s 128
)
- Listen on the eth1 interface (
-i eth1
)
- Save everything to a file called limited.pcap (
-w limited.pcap
)
sudo tcpdump -c 500 -s 128 -i eth1 -w limited.pcap
- While tcpdump is running, open another terminal window to generate some test traffic. These commands will create typical network activity:
ping -c 3 google.com
curl http://example.com
This simulated traffic will be captured by our running tcpdump session.
- After capturing 500 packets (or press Ctrl+C to stop earlier), verify your capture file exists and check its size:
ls -lh limited.pcap
The output shows the file details including size (56K in this example) and creation time:
-rw-r--r-- 1 root root 56K Aug 10 15:30 limited.pcap
- To review your captured packets later, use the
-r
option to read the pcap file:
tcpdump -r limited.pcap
This displays the packet contents exactly as they were captured, allowing you to analyze the traffic at your convenience.