Free tech learning for everyone, from basics to pro

Network FundamentalsNetworking SecurityIntermediate

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.

Capturing on a server and analysing on a laptoptcpdump on serverwrite to fileLimit size-c, -C, filtersCopy filescp or SFTPOpen in Wiresharkanalyse
Figure 1. Capture where the traffic is, then analyse comfortably on your own machine.

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
  • -i chooses the interface (any for all on Linux).
  • -nn shows numbers instead of resolving names and port names, which is faster and avoids creating extra DNS traffic.
  • -c stops after a number of packets.
  • -v, -vv add detail; -A prints payload as text; -e shows MAC addresses.

BPF filters

  • host 10.1.1.5, src host, dst host
  • net 10.10.0.0/16
  • port 443, portrange 5060-5070, udp, tcp, icmp, arp
  • Combine with and, or, not and 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?
To show IP addresses and port numbers instead of resolving names, which is faster and avoids extra DNS traffic.
How do you save a capture to a file for Wireshark?
Use -w filename.pcap.
Why add not port 22 when capturing over SSH?
Otherwise tcpdump captures its own SSH session traffic, creating a loop of output.

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.
Tracks your learning on this device only. See your progress on the learning paths.
Verified author✓ Valid
Issued to
Mohamed Fayas
Role
Founder of FM TecHub
Credentials
CCNABTEC HND Network EngineeringBEng (Hons) Computer Networking & Cloud Security
Issuer
FM TecHub · fmtechub.com

He writes practical, plain-English guides on networking, security and cloud.

More about the author →

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.

Email updates: coming soon

New notes, tools and security news from FM TecHub by email, explained simply. Free. We're not collecting email addresses yet; see our privacy policy.

PortServiceStatus
SMTP/25Email updatesopening soon

Meanwhile, stay connected: