Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Networking

Linux tcpdump command

Reference for the Linux tcpdump command: capture and read packets, write pcap files, use BPF filters for host, port and protocol, and read the output format.

tcpdump captures the packets crossing a network interface and prints a one-line summary of each. When logs and ping cannot explain a network problem, the packets can: you see whether a request left the machine, whether an answer came back, and what was in the headers.

It needs permission to read raw traffic, so it normally runs as root (sudo tcpdump). It uses BPF filter expressions to select packets in the kernel, so even a busy interface can be watched without drowning in output.

Terminal
sudo tcpdump [OPTIONS] [FILTER EXPRESSION]

Try it

tcpdump is not installed in our sandbox, and capturing needs root, so this block is read-only:

CONSOLE
$ sudo tcpdump -i eth0 -nn -c 3 'tcp port 443'
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
12:00:01.101 IP 10.0.0.5.51532 > 93.184.215.14.443: Flags [S], seq 3015, win 64240, length 0
12:00:01.121 IP 93.184.215.14.443 > 10.0.0.5.51532: Flags [S.], seq 881, ack 3016, win 65535, length 0
12:00:01.121 IP 10.0.0.5.51532 > 93.184.215.14.443: Flags [.], ack 1, win 502, length 0
3 packets captured

$ sudo tcpdump -i any -nn -w web.pcap port 80     # save for Wireshark
$ tcpdump -nn -r web.pcap | head                  # read it later

The Flags field is the part worth learning: S is SYN, S. is SYN-ACK, . is ACK. The three lines above are the TCP handshake. This block decodes such flags in Bash:

Bash Reading TCP flags

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
tcpdump [-AnnvXeq] [-i IFACE] [-c COUNT] [-s SNAPLEN] [-w FILE] [-r FILE] [-G SECS] [-C SIZE] [EXPRESSION]
Option Description
-i IFACE Interface to capture on (eth0, lo, or any for all).
-n Do not resolve addresses to host names; -nn also keeps port numbers numeric.
-c N Stop after N packets.
-w FILE Write raw packets to a pcap file instead of printing.
-r FILE Read packets from a pcap file.
-s N Snapshot length; -s 0 captures whole packets.
-A Print packet payload as ASCII (useful for plain HTTP).
-X Print payload as hex and ASCII.
-v, -vv More detail (TTL, checksums, protocol decoding).
-e Print link-level (MAC) headers.
-q Quiet: shorter lines.
-G SECS, -C SIZE Rotate the output file by time or size.

Filter expressions combine primitives with and, or, not and parentheses (quote them): host 10.0.0.5, src net 10.0.0.0/24, dst port 53, tcp, udp, icmp, portrange 8000-8100, tcp[tcpflags] & tcp-syn != 0.

Returns: 0 when the capture ended normally; non-zero on an error such as a bad filter or missing permission.

Usage notes

  • Use -nn almost always. Name lookups slow the output and generate their own DNS traffic, which then appears in the capture.
  • Filter early. The expression runs in the kernel; filtering later with grep makes the capture lose packets under load. Always put the filter in tcpdump itself.
  • Capture on the right interface. lo is loopback, any is every interface (with a different link header). In containers the interface is often eth0 inside the namespace.
  • HTTPS is encrypted. You see the handshake and sizes, not the content. For plain protocols, -A shows the text.
  • Save and analyse elsewhere. -w produces a pcap you can open in Wireshark or read back with -r. Run with -c or a size limit so the file does not fill the disk.
  • Privacy and permission. A capture can contain passwords and personal data. Only capture traffic you are allowed to see, and protect the files.
  • Offload artifacts. On a sending host, checksum errors in -v output are often just NIC offloading and not a real fault.