Capturing traffic with tcpdump on Linux: filters, writing files and reading the output
In short: tcpdump is the command-line packet capture tool on Linux and many network appliances. You choose an interface, add a BPF filter such as host, port or net, and either read packets on screen or write them to a pcap file for Wireshark. Use -nn to avoid DNS lookups, limit capture size and duration, and remember that captures may contain sensitive data.
Why tcpdump
Servers, cloud instances, firewalls and Linux-based network devices rarely have a graphical interface. tcpdump runs everywhere, uses very little resource and writes standard pcap files that Wireshark opens. Knowing a few options covers most troubleshooting.
The essentials
sudo tcpdump -D # list interfaces
sudo tcpdump -i eth0 -nn # capture on eth0, no name lookups
sudo tcpdump -i any -nn host 10.10.5.20 # all interfaces, one host
sudo tcpdump -i eth0 -nn port 53 # DNS only
sudo tcpdump -i eth0 -nn 'tcp port 443 and host 203.0.113.10'
sudo tcpdump -i eth0 -nn -c 200 icmp # stop after 200 packets
-ichooses the interface (anyfor all on Linux).-nnshows numbers instead of resolving names and port names, which is faster and avoids creating extra DNS traffic.-cstops after a number of packets.-v,-vvadd detail;-Aprints payload as text;-eshows MAC addresses.
BPF filters
host 10.1.1.5,src host,dst hostnet 10.10.0.0/16port 443,portrange 5060-5070,udp,tcp,icmp,arp- Combine with
and,or,notand brackets in quotes:'host 10.1.1.5 and not port 22' - TCP flags:
'tcp[tcpflags] & tcp-syn != 0'for SYN packets,'tcp[tcpflags] & tcp-rst != 0'for resets.
When capturing over SSH, always add not port 22 (or exclude your own address), or tcpdump will capture its own output being sent to you, in an endless loop of traffic.
Writing and reading files
sudo tcpdump -i eth0 -nn -w /tmp/web.pcap 'port 443'
sudo tcpdump -nn -r /tmp/web.pcap | head
# ring buffer: 10 files of 100 MB each, oldest overwritten
sudo tcpdump -i eth0 -nn -w /var/tmp/cap.pcap -C 100 -W 10 'host 10.10.5.20'
The ring buffer is ideal for catching an intermittent problem: leave it running, and when the problem happens, stop the capture and keep the last files.
Reading a line
10:42:01.123456 IP 10.10.20.5.51522 > 10.10.5.20.443: Flags [S], seq 1234, win 64240, length 0
Time, protocol, source address and port, destination address and port, then TCP flags: [S] SYN, [S.] SYN-ACK, [.] ACK, [P.] push with data, [F.] FIN, [R] reset.
Capturing safely
- Captures can contain passwords, personal data and session cookies. Get permission, keep files secure and delete them when finished.
- On busy servers, filter tightly and limit size so you do not fill the disk or overload the CPU.
- Many appliances (firewalls, load balancers, Cisco IOS XE with Embedded Packet Capture) have their own capture commands that write pcap files too.
Real-world example: was the request even arriving?
An application team said a partner's API calls were never reaching their server. Running tcpdump -i any -nn host 198.51.100.40 and port 443 on the server showed SYN packets arriving and SYN-ACKs leaving, but no ACK ever came back. The server's replies were leaving through a different default route than the requests arrived on, and the partner's firewall dropped them. A return route through the correct firewall fixed it.
Practice
Why use -nn with tcpdump?
How do you save a capture to a file for Wireshark?
-w filename.pcap.Why add not port 22 when capturing over SSH?
Key takeaways
- tcpdump captures traffic on Linux and many appliances.
- Use -i, -nn, -c and BPF filters such as host, port and net.
- Write pcap files with -w, and use -C and -W for ring buffers.
- Captures hold sensitive data; filter tightly and handle files securely.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.