tcpdump — Command-Line Packet Capture
Capture scoped network evidence with tcpdump and read packet summaries.
Small, Fast, and Close to the Interface
tcpdump captures packets from a network interface, applies a BPF filter, and prints a concise decode or writes a pcap file. It is valuable on remote Linux systems where a graphical analyzer is unavailable. The same discipline applies as with Wireshark: define an authorised scope, ask a specific question, and minimise what you collect.
Most live captures require elevated privileges because packet capture accesses network interfaces. Use the least privilege available, ask an administrator when needed, and avoid leaving broad capture processes running. Never capture traffic from a network or device without permission.
Select Interface and Scope
First inspect available interfaces. Use the loopback interface for a local lab exchange, or a specific approved interface for an investigation. The -n option avoids DNS and service-name lookups, keeping output predictable and reducing extra traffic. A short packet limit or time-bounded capture is safer than an indefinite session.
# List capture interfaces (may require elevated privileges)
sudo tcpdump -D
# Inspect only a local test service on loopback; stop after a small sample
sudo tcpdump -i lo0 -nn -c 20 'host 127.0.0.1 and port 8080'
# Linux often names loopback "lo"; confirm with the interface list firstInterface names differ across operating systems and containers. Capturing on “any” can combine multiple interfaces and create duplicate-looking packets or different link-layer details. Choose the narrowest interface that can observe the path you care about.
Build and Reason About BPF Filters
BPF uses primitives such as host, net, port, tcp, and udp. Combine them with and, or, and not. Parentheses control grouping; single-quote the expression to keep the shell from interpreting punctuation. Begin broad enough to see the expected exchange, then narrow deliberately.
# Common, read-only capture scopes
sudo tcpdump -i lo0 -nn 'udp port 53'
sudo tcpdump -i lo0 -nn 'tcp port 8080'
sudo tcpdump -i lo0 -nn 'host 127.0.0.1 and (tcp port 8080 or tcp port 8443)'
sudo tcpdump -i lo0 -nn 'icmp and host 127.0.0.1'
# Capture only headers in summary; avoid payload collection where possible
sudo tcpdump -i lo0 -nn -c 25 -s 96 'host 127.0.0.1 and port 8080'A snap length limits captured bytes per packet; it does not guarantee that the retained headers are sufficient for every protocol. Set the value to the minimum needed, and do not assume truncation eliminates all sensitive data. Test your filter in a controlled lab before relying on it during an incident.
Write, Read, and Share Capture Files
Use -w to save a pcap and -r to read it. Capture files should have restrictive permissions and an explicit retention plan. A bounded packet count or duration helps prevent filling a disk. Include a clear filename, case identifier, time zone, interface, and filter in the evidence notes rather than relying on the filename alone.
# Save a narrowly scoped local lab capture
sudo tcpdump -i lo0 -nn -c 50 -w local-test.pcap 'host 127.0.0.1 and port 8080'
# Read it later; -nn keeps addresses and ports numeric
tcpdump -nn -r local-test.pcap
# Summarise packets to standard output without changing the saved evidence
tcpdump -nn -r local-test.pcap 'tcp[tcpflags] & tcp-syn != 0'Opening a pcap in Wireshark supports protocol dissection and stream analysis. The file may contain more than the summary printed to the terminal. Treat it as sensitive, verify its hash if chain-of-custody matters, and transfer it only through approved channels.
Operational Caveats
Capture loss may occur under load; tcpdump reports packet statistics on exit, which should be included in the interpretation. Host offload features, virtual interfaces, container networking, and asymmetric routes can affect what appears. A capture on the sender may show packets before a network device drops them; capture at a receiver or approved tap can answer a different question.
For a safe lab workflow, start a local test server and send a request only to loopback. Do not copy commands that capture all interfaces or unfiltered payloads from examples into production. Be especially careful with passwords and session tokens on plaintext protocols.
# Reproduce a harmless local exchange
python3 -m http.server 8080 --bind 127.0.0.1
# Separate terminal
curl -I http://127.0.0.1:8080/
# Filter to this loopback address and port; inspect only the evidence neededMistakes, Practice, and Review
Common mistakes: forgetting -n and creating confusing name lookups; using an interface name that does not exist on the host; leaving a capture unbounded; and saving a full pcap in a world-readable directory. If no packets appear, validate the interface and filter using a harmless known exchange.
Practice: (1) List interfaces and identify loopback on your OS. (2) Capture a local HTTP request with a host-and-port filter. (3) Read the pcap and identify the handshake. (4) Explain when a snap length is useful and what it does not guarantee. (5) Write evidence notes with capture point, time, scope, and packet-drop count.
🎯 Key Takeaways
- ✓tcpdump is a command-line packet capture and BPF filtering tool; choose interface and scope deliberately.
- ✓Use quoted, narrow filters and numeric output for predictable, low-noise diagnostics.
- ✓Bound captures by time or packet count, and minimise payload with an appropriate snap length.
- ✓Use -w and -r for controlled pcap workflows; saved files remain sensitive even if terminal output is brief.
- ✓Capture point, offloads, asymmetric paths, and packet drops affect conclusions.
- ✓Restrict privileges, permissions, retention, and sharing; practice only on authorised traffic.
HELP US IMPROVE
How was this lesson?
Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.
Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.
