Capturing traffic with tcpdump

Intermediate · 19 min

Capture exactly the packets you need with tcpdump filters, read a TCP handshake from its flags, and save pcap files to open later in Wireshark.

Before thisSockets and listening ports with ssPacket filtering with nftables

David Mahler · 19 min

Capturing traffic with tcpdump

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by David MahlerReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • tcpdump shows what actually crossed an interface. That is the evidence to reach for when two sides of a connection each blame the other.
  • Always narrow the capture: pick an interface with `-i`, stop after `-c` packets, skip name lookups with `-n`, and pass a filter expression such as `'host 192.0.2.7 and port 443'`.
  • Filters use pcap-filter syntax: `host`, `net`, `port`, `src`/`dst`, combined with `and`, `or`, `not` and parentheses. Quote the whole filter so the shell leaves it alone.
  • TCP flags print in brackets: `[S]` SYN, `[S.]` SYN-ACK, `[.]` ACK, `[P.]` data, `[F.]` FIN, `[R]` reset. A SYN with no SYN-ACK and a SYN answered by `[R]` point to different problems.
  • `-w file.pcap` saves raw packets for Wireshark or `tcpdump -r`. Capture on the server, analyse on your desktop.
  • Capturing needs root or the `CAP_NET_RAW` capability, and it shows encrypted payloads only as ciphertext.

When both sides blame each other

The application team says the database never answered. The database team says no request arrived. Both are reading their own logs, and logs record what a program believed happened. A packet capture records what the network card actually saw. When two stories disagree, a capture settles it in minutes.

tcpdump is the standard tool for this on Linux servers. It asks the kernel for a copy of packets on an interface, applies a filter, and prints a one-line summary of each or writes them to a file. Think of it as a CCTV camera on the loading dock: it does not stop anything, it just records what came and went.

A capture that does not drown you

A busy server moves thousands of packets a second, and an unfiltered capture scrolls too fast to read. Narrow it every time. The tcpdump man page (tcpdump.org, checked 2026-11-28) defines the options:

sudo tcpdump -D                                   # list capture interfaces
sudo tcpdump -i enp5s0 -n -c 20 'port 443'
sudo tcpdump -i any -nn 'host 192.0.2.7 and tcp port 5432'

-i any captures on all interfaces at once, which is handy when you are not sure which one the traffic uses. -n stops tcpdump from turning addresses into names. Without it the tool sends its own DNS queries, which slows the output and can land in your capture. -c 20 exits after 20 packets. The default snapshot length is 262144 bytes, so whole packets are kept unless you shorten it with -s.

None of these were run. There is no Linux machine here, and on this Mac tcpdump (4.99.1) refused to touch the capture device without root, which I did not use.

Filter syntax

The filter is a pcap-filter expression, compiled into a small program that runs in the kernel, so unwanted packets are discarded before they are copied out. The pieces, from the pcap-filter man page:

  • host 192.0.2.7, net 10.0.0.0/8, port 53, portrange 8000-8100
  • src and dst narrow a primitive to one direction: src host 192.0.2.7
  • protocols: tcp, udp, icmp, ip6, arp
  • and, or, not, and parentheses for grouping

Put the whole expression in single quotes so the shell does not interpret the parentheses. Flags can be matched too. The man page’s example for selecting only SYN and FIN packets uses tcp[tcpflags] & (tcp-syn|tcp-fin) != 0, which gives you the start and end of every connection and nothing in between.

Reading a handshake

For TCP, each line shows source and destination with ports, then the flags in brackets. The man page’s legend is S for SYN, F for FIN, P for PSH, R for RST and a dot for ACK, combined:

Flags Meaning
[S] client opens: SYN
[S.] server agrees: SYN-ACK
[.] client acknowledges; the connection is established
[P.] data being pushed, with an ACK
[F.] one side closing
[R] or [R.] reset: refused or aborted

The patterns are the useful part. Three lines, [S], [S.], [.], mean the network and the listener are fine, so look higher up the stack. A [S] answered by [R.] means the host is reachable but nothing listens on that port, which ss will confirm. A [S] repeated with growing gaps and no answer means something is silently dropping it, often a firewall from the nftables lesson. The client is retransmitting because it heard nothing. If the SYNs never appear on the server at all, the problem is upstream of it.

Saving captures for Wireshark

Wireshark is far better at reading packets than a terminal, but a server rarely has a desktop. So split the work:

sudo tcpdump -i enp5s0 -n -w /tmp/db.pcap 'tcp port 5432'
# reproduce the problem, then Ctrl-C
scp server:/tmp/db.pcap .

The man page describes -w output as a pcap savefile, which Wireshark opens directly, and tcpdump -r db.pcap reads it back in a terminal. For long-running captures, -C starts a new file once the current one passes a size, and -G rotates every so many seconds. Rotating files stops a forgotten capture from filling the disk.

Limits worth knowing

Capturing needs root, or the CAP_NET_RAW capability that the capabilities lesson explains. On a modern network most payloads are TLS, so you see the handshake and the sizes and timing of records, not the HTTP inside. That is still enough to answer “did it connect, and who hung up”. Captures can also contain passwords and personal data from unencrypted protocols, so treat pcap files like secrets and delete them when you are done.

About the video

The embed is all of David Mahler’s “Introduction to TCPDUMP”, December 2014, about 19 minutes. It is old, but tcpdump’s options and filter language have barely changed in that time. Its description lists exactly the options used here: -D, -i, -c, -n, -s, -w, -r, plus host, port and protocol filters. Mahler’s networking videos are known for clear, slow explanations, which suits a tool whose output looks dense at first. I judged it from its title and description. If you want a 2024 recording with chapters per filter type, Rajneesh Gupta’s crash course is in deeper. English was judged from the English auto-caption track. Facts were checked against the tcpdump and pcap-filter man pages on tcpdump.org on 2026-11-28.

Where this fits

That completes the networking group: addresses, routes, names, persistence, sockets, filtering and capture. Next comes locking the machine down, starting with the service every server exposes: hardening SSH on a server.

Check yourself

  1. 01A client's connection attempts to port 5432 time out. On the server, `tcpdump -n -i any port 5432` shows repeated `[S]` packets arriving and nothing sent back. What does that tell you?

    The SYNs reach the server's interface but nothing answers. Something on the server is dropping them, most likely a firewall rule, since a closed port would answer with a reset.

  2. 02Same capture, but every `[S]` is answered immediately by `[R.]`. What now?

    The packets get through and the kernel actively refuses them because nothing is listening on that address and port. Check `ss -tlnp` and the service's bind address.

  3. 03Why is `-n` worth adding almost every time?

    Without it tcpdump does reverse DNS lookups for addresses it prints. That slows output and generates extra DNS traffic, which can show up in your own capture.

  4. 04How do you capture on a server and look at the result in Wireshark on your laptop?

    Run `tcpdump -i <iface> -w capture.pcap <filter>` on the server, stop it, copy the file with scp, and open it in Wireshark.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark