Learn tcpdump in Two Minutes? Yes, Here’s How
Yes, you can learn the core of tcpdump in about two minutes—enough to capture traffic, filter it, and read the output. This article gives you that fast start, then fills in the background, syntax, common pitfalls, and safety notes that turn a quick look into real skill.
What tcpdump Actually Does
tcpdump is a command-line packet analyzer. It listens on a network interface and prints a line for each packet that matches your filter. You can save the raw packets to a file (a pcap) for later analysis with Wireshark or another tool. It runs on Linux, macOS, and other Unix-like systems; Windows users typically use a port like WinDump or use Wireshark instead.
The basic command is simple:
sudo tcpdump -i eth0
That captures everything on the interface eth0 until you press Ctrl+C. The sudo is usually required because reading raw packets needs root privileges.
The Two-Minute Core: Filter, Capture, Read
Here is the fastest path to being productive with tcpdump:
- Pick an interface with
-i. Runtcpdump -Dto list available interfaces. - Add a filter to reduce noise. The most common are
host,port, andproto(liketcporudp). Example:tcpdump -i eth0 host 192.168.1.1captures only traffic to or from that IP. - Save to a file with
-winstead of printing to screen:tcpdump -i eth0 -w capture.pcap. Read it later withtcpdump -r capture.pcap. - Read the output. A typical line looks like:
14:03:22.123456 IP 192.168.1.10.54321 > 93.184.216.34.80: Flags [P.], seq 1:1461, ack 1, win 64240, length 1460
That means: timestamp, protocol (IP), source IP and port, destination IP and port, TCP flags, sequence and acknowledgment numbers, window size, and payload length in bytes.
That is the whole two-minute version. Everything else is refinement.
How to Choose Filters That Matter
Filters are the real power of tcpdump. They use a syntax called BPF (Berkeley Packet Filter). You can combine them with and, or, and not. Common examples:
tcpdump -i eth0 port 80— only HTTP traffic (port 80).tcpdump -i eth0 port 53— DNS queries and responses.tcpdump -i eth0 src 10.0.0.1— packets whose source address is 10.0.0.1.tcpdump -i eth0 dst port 443— packets destined for HTTPS port 443.tcpdump -i eth0 tcp and not port 22— all TCP except SSH.tcpdump -i eth0 'tcp[13] & 2 != 0'— TCP packets with the SYN flag set (new connection attempts). The[13]refers to the byte in the TCP header that contains the flags.
When in doubt, start with host and port—they cover most debugging needs.
Reading the Output Like a Pro
The default output is compact and can look cryptic. Here is a breakdown of the key fields:
- Timestamp: local time by default. Use
-ttttfor a full date and time. - Protocol: usually IP, but can be ARP, IPv6, etc.
- Source and destination: shown as IP.port. The port is the source port on the left, destination port on the right.
- Flags: letters like
[S](SYN),[.](ACK),[P](push),[F](FIN),[R](RST). A combination like[S.]means SYN-ACK. - seq and ack: TCP sequence and acknowledgment numbers. With
-Syou see absolute numbers instead of relative ones. - length: payload size in bytes. The total IP packet length is shown with
-v.
If you need to see the actual payload data, use -X (hex and ASCII) or -A (ASCII only). For HTTP requests, -A is often enough to read the method, path, and headers.
Common Mistakes and How to Avoid Them
Even experienced users trip on these:
- Forgetting to specify the interface. Without
-i, tcpdump picks the first available one, which may not be the one you want. Always use-i. - Capturing too much. On a busy network, you can fill your disk quickly. Use filters and limit the count with
-c(e.g.,-c 100stops after 100 packets). - Not using
-wfor long captures. Printing to the terminal slows things down and scrolls past useful data. Save to a file and analyze later. - Mixing up source and destination. In the output, the left side is source, right side is destination. In filters,
srcanddstare explicit. - Forgetting that DNS uses UDP. If you filter
port 53, you get both TCP and UDP unless you specifyudp port 53.
Safety and Privacy: What You Must Know
tcpdump is a powerful tool that can see all unencrypted traffic on your network segment. That raises real concerns:
- Only capture on networks you own or have permission to monitor. Sniffing on a network without authorization is illegal in many jurisdictions and violates most terms of service.
- Be careful with sensitive data. Packet captures can contain passwords, cookies, and personal messages if the traffic is not encrypted. Store pcap files securely and delete them when no longer needed.
- Use encryption. Modern web traffic is mostly HTTPS, so you will see encrypted payloads that are useless to an attacker. But plaintext protocols like HTTP, FTP, and Telnet are still out there—avoid using them on untrusted networks.
- Running tcpdump with sudo gives you root-level access. Always run it with the least privilege needed and never leave a terminal with root open unattended.
Alternatives and When to Use Them
tcpdump is not the only tool, and sometimes it is not the best one. Consider these:
- Wireshark: a graphical tool that reads pcap files and offers deep protocol analysis, filters, and follow-stream features. Great for complex debugging, but heavier than tcpdump.
- tshark: the command-line version of Wireshark. It can do everything tcpdump does plus protocol dissection, but it has a steeper learning curve.
- nmap: for scanning ports and discovering services, not for packet capture. Use it when you need to map a network.
- netstat or ss: for current connections and listening ports, not for capturing traffic. Faster for a quick check.
In practice, many engineers use tcpdump to capture raw packets and then open the pcap in Wireshark for analysis. That combines the lightweight capture of tcpdump with the rich UI of Wireshark.
Frequently Asked Questions
Do I need root to run tcpdump?
Yes, on most systems you need root or sudo because reading raw packets requires elevated privileges. Some systems allow a non-root user with the right capabilities (e.g., cap_net_raw), but that is not the default. If you get a permission error, use sudo.
How do I stop tcpdump?
Press Ctrl+C. That stops the capture and prints a summary of how many packets were captured, filtered, and dropped. If you used -c, it stops automatically after that many packets.
Can I capture traffic on a remote machine?
Yes, but not directly. You can run tcpdump on the remote machine and save to a file, then copy the file to your local machine. Or you can use SSH with a pipe: ssh user@remote "sudo tcpdump -i eth0 -w -" > capture.pcap. That sends the raw output over SSH to a local file.
What does “promiscuous mode” mean?
By default, tcpdump puts the interface into promiscuous mode, meaning it captures all packets the interface sees, not just those addressed to your machine. This is necessary on a switch to see other hosts’ traffic, but it may not work on all networks (e.g., some wireless networks isolate clients). You can disable it with -p.
Why do I see only my own traffic?
On a modern switched network, you usually only see broadcast, multicast, and your own unicast traffic. To see other hosts’ traffic, you need a hub (rare), a switch with port mirroring, or an ARP-spoofing attack (which is illegal without permission). tcpdump cannot bypass this limitation.
How do I read a pcap file without a GUI?
Use tcpdump -r file.pcap to read it. You can apply the same filters as live capture, e.g., tcpdump -r file.pcap port 80. For more advanced analysis, use tshark.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.