Skip to content
Visual explainer 2h 30m to learn 4 steps

Packet capture with tcpdump

Capture exactly the traffic you need with tcpdump filters and save it for analysis.

By Invicly Tech, , in Computer Networking. About 11 minutes to read.

Topic 38 of 51

In short

tcpdump captures packets from an interface and prints or saves them, which makes it the standard capture tool on servers without a GUI. Capture filters written in BPF syntax limit what is recorded so files stay small. Save to a pcap file and open it in Wireshark for deeper analysis.

Explained simply

Sometimes logs and error messages are not enough, and you need to see the actual packets. tcpdump is a command-line tool that records the traffic on a network card. It runs almost everywhere, including servers with no screen, so it is the go-to tool when you need proof of what really happened.

A busy server sends thousands of packets per second. If you record everything, you will drown. tcpdump lets you write a filter, a short rule like "only traffic to port 443 from this IP", so you catch just the conversation you care about.

You can watch packets live in the terminal, or save them to a file (a pcap file) and open it later in Wireshark, which shows everything in a friendly window. A common pattern is: capture with tcpdump on the server, copy the file, analyse it on your laptop.

Captures can contain passwords, cookies and personal data, so treat them as secrets. Capturing also needs admin rights. Only capture on networks you are responsible for, and delete files you no longer need.

Press Play on a diagram to walk through it one step at a time.

FlowCapture workflow
  1. 1
    Pick interface

    tcpdump -D or ip link.

  2. 2
    Filter

    host, port, net, tcp, udp, icmp.

  3. 3
    Capture

    -w file.pcap with a packet or size limit.

  4. 4
    Reproduce

    Trigger the failing request.

  5. 5
    Analyse

    tcpdump -r or Wireshark.

CompareWhat a failed connection looks like
Nothing listening
  • Client sends [S]
  • Server replies [R.] at once
  • App shows connection refused
  • Check ss -tlnp on the server
Firewall drops
  • Client sends [S]
  • No reply at all
  • Client retries [S] after about 1 s, then 2 s, 4 s
  • App shows a timeout
Path problem
  • Client sends [S]
  • Server capture never sees it
  • Or server's [S.] never arrives
  • Check routes, NAT, security groups

In depth

Choosing where to capture

A capture only sees traffic that passes the chosen interface. tcpdump -D lists the interfaces it can use. On a server, pick the one that carries the traffic, such as eth0 or ens5. On Linux, -i any captures on all interfaces at once; it is handy when you are unsure, but it records a "cooked" header instead of the real Ethernet header, so the destination MAC and VLAN tags are missing.

Where you capture also matters. On the client you see what it sent and received. On the server you see what actually arrived. Capturing on both ends at once is the best way to prove packet loss in between: if the client sent a SYN and the server never saw it, the problem is on the path. In containers, capture inside the container's network namespace or on its veth interface; on a switch, use a mirror (SPAN) port. tcpdump puts the card in promiscuous mode by default; -p turns that off.

  • tcpdump -D: list capture interfaces
  • -i any: all interfaces (Linux), no full Ethernet header
  • -e: print MAC addresses and VLAN tags
  • Capture both ends to locate loss

Writing capture filters

Capture filters use BPF (Berkeley Packet Filter) syntax and are checked in the kernel, so unwanted packets are never copied. The building blocks are type (host, net, port, portrange), direction (src, dst) and protocol (tcp, udp, icmp, arp, ip6). Combine them with and, or, not, and group with parentheses. Put the whole filter in single quotes so the shell does not touch the parentheses.

Examples: host 10.0.0.5 and tcp port 443; net 10.20.0.0/16 and not port 22; src host 10.0.0.5 and dst port 53. You can also test header bits: 'tcp[tcpflags] & (tcp-syn|tcp-fin|tcp-rst) != 0' catches only connection starts and ends, which keeps captures tiny on busy links. Always exclude your own SSH session (not port 22) when capturing remotely, or tcpdump will capture its own output in a loop.

MapParts of a capture filter
BPF capture filter
  • Type
    • host 10.0.0.5
    • net 10.0.0.0/24
    • port 443
    • portrange 8000-8100
  • Direction
    • src host 10.0.0.5
    • dst port 53
  • Protocol
    • tcp, udp, icmp
    • arp, ip6, vlan
  • Logic
    • and, or, not
    • Parentheses inside quotes
  • Header bits
    • tcp[tcpflags] & tcp-syn != 0
    • icmp[icmptype] == icmp-echo

Saving captures safely

-w file.pcap writes raw packets to a file instead of printing them; -r file.pcap reads one back. Always add a limit so a forgotten capture cannot fill the disk. -c 5000 stops after 5000 packets. For long captures, use a ring buffer: -C 100 -W 10 starts a new file every 100 MB (millions of bytes) and keeps only the 10 newest. -G 3600 rotates every hour instead (use a strftime name like trace-%H.pcap).

The default snap length (-s) is 262144 bytes, which captures whole packets. If you only need headers, -s 128 keeps files small and leaves most payload, including secrets, out of the file. Note that -w writes buffered data, so stop tcpdump with Ctrl+C or kill -INT to flush the file properly. The pcap files open directly in Wireshark and tshark.

  • -c 5000: stop after 5000 packets
  • -C 100 -W 10: ring of ten 100 MB files
  • -s 128: headers only, smaller and safer
  • -w writes; -r reads, and filters work with -r too

Reading the output and TCP flags

A typical line looks like: 10:42:01.123 IP 10.0.0.5.51514 > 93.184.216.34.443: Flags [S], seq 1234, win 64240, options [mss 1460,sackOK,TS,nop,wscale 7], length 0. That is time, protocol, source IP.port, destination IP.port, TCP flags, sequence number, window size, TCP options and payload length.

Flags print as letters: S is SYN, F is FIN, R is RST, P is PUSH, and a dot means ACK. So [S] opens a connection, [S.] is the SYN-ACK reply, [.] is a plain ACK, [P.] is data with ACK, [F.] closes politely and [R] or [R.] aborts. By default tcpdump shows relative sequence numbers after the first packet; -S shows absolute ones. -nn prints numbers for both hosts and ports. -v adds IP details like TTL; -X shows payload in hex and ASCII and -A in ASCII only, useful for plain HTTP.

SequenceA short TCP connection as tcpdump prints it
Client
Server
  1. 1Flags [S]: SYN, open
    Client to Server: Flags [S]: SYN, open
  2. 2Flags [S.]: SYN-ACK
    Server to Client: Flags [S.]: SYN-ACK
  3. 3Flags [.]: ACK, handshake done
    Client to Server: Flags [.]: ACK, handshake done
  4. 4Flags [P.]: request data
    Client to Server: Flags [P.]: request data
  5. 5Flags [P.]: response data
    Server to Client: Flags [P.]: response data
  6. 6Flags [F.]: FIN, close
    Client to Server: Flags [F.]: FIN, close
  7. 7Flags [F.] then final [.]
    Server to Client: Flags [F.] then final [.]

Learn it in 4 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Choose the capture interface~40m
  2. 2
    Write host, port and protocol filters~40m
  3. 3
    Save captures to pcap files~40m
  4. 4
    Read TCP flags in the output~40m

Key points

  • -n stops DNS lookups so output is fast and unambiguous.
  • Combine filters with and, or and not, using parentheses in quotes.
  • Flags print as [S] SYN, [S.] SYN-ACK, [.] ACK, [P.] push, [F.] FIN, [R] reset.
  • Capturing needs root or the right capabilities; captures can contain secrets.

Key terms

Tap a card to flip it. 0/4 known

Examples

bash
sudo tcpdump -D                                   # list interfaces
sudo tcpdump -i eth0 -nn host 10.0.0.5 and port 443
sudo tcpdump -i any -nn 'udp port 53 or icmp'
sudo tcpdump -i eth0 -nn -c 2000 -w /tmp/web.pcap 'tcp port 80'
tcpdump -nn -r /tmp/web.pcap 'tcp[tcpflags] & tcp-rst != 0'

Refused or dropped? Proving it with packets

An app cannot reach a database on 10.0.20.15:5432. Step 1: capture on the app server only for that host and port. Step 2: trigger a connection. Step 3: read the flags. If you see [S] followed by [R.], the database host is reachable but nothing listens on 5432, so check the service. If you see [S] repeated with growing gaps and no reply, something drops the SYN: a firewall, security group or wrong route.

bash
sudo tcpdump -i eth0 -nn 'host 10.0.20.15 and tcp port 5432'
# Case A, refused:
# IP 10.0.10.11.40422 > 10.0.20.15.5432: Flags [S], seq 99..., length 0
# IP 10.0.20.15.5432 > 10.0.10.11.40422: Flags [R.], seq 0, ack 1, length 0
# Case B, dropped:
# IP 10.0.10.11.40424 > 10.0.20.15.5432: Flags [S], ... (no reply)
# IP 10.0.10.11.40424 > 10.0.20.15.5432: Flags [S], ... (retransmit)

Catching a rare error with a ring buffer

A partner's webhook fails a few times per day. You cannot watch all day, and a full capture would fill the disk. Step 1: start a ring-buffer capture of only that partner's traffic, headers only, keeping ten 50 MB files. Step 2: when the error appears in the app log, note the time. Step 3: stop the capture and read only the file that covers that time, filtering for resets. Step 4: copy that one file to your laptop for Wireshark.

bash
sudo tcpdump -i eth0 -nn -s 256 -C 50 -W 10 \
  -w /var/tmp/hook.pcap 'net 198.51.100.0/24 and tcp port 443'
# files: hook.pcap0 ... hook.pcap9 (oldest overwritten)
ls -l --time-style=+%H:%M /var/tmp/hook.pcap*
tcpdump -nn -r /var/tmp/hook.pcap3 'tcp[tcpflags] & tcp-rst != 0'

Quick reference

  • sudo tcpdump -D: list interfaces you can capture on
  • -nn: no host or port name lookups; always use it for speed and clarity
  • 'host A and (port 80 or port 443)': quote filters that use parentheses
  • Add 'and not port 22' when capturing over SSH
  • -w f.pcap -c 10000: save to file with a hard packet limit
  • -C 100 -W 10: ring buffer of ten 100 MB files
  • Flags: [S] SYN, [S.] SYN-ACK, [.] ACK, [P.] data, [F.] FIN, [R] reset
  • 'tcp[tcpflags] & (tcp-syn|tcp-fin|tcp-rst) != 0': only connection starts and ends

Common mistakes

  • Capturing on the wrong interface and seeing nothing.
  • Running unfiltered captures on busy servers and filling the disk.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1You capture on the client and see [S] sent three times with growing gaps and no reply. What is most likely?

Question 2

2What does Flags [R.] mean in tcpdump output?

Question 3

3Why is -i any less useful when you need MAC addresses?

Question 4

4Which option set keeps a capture from filling the disk during a long run?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Capture 10 DNS packets while you run a lookup, and identify the query and the answer in the output.

  2. Exercise 2medium

    Save a capture of one HTTPS page load to a file, then read it back showing only the SYN, SYN-ACK, FIN and RST packets.

  3. Exercise 3hard

    Prove whether packet loss happens on the path between two of your servers by capturing on both ends at the same time and comparing SYN counts.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Can tcpdump see HTTPS content?
No. It captures the packets, but the payload is encrypted by TLS. You can still see IPs, ports, timing, TCP flags and the server name in the TLS ClientHello.
Why does tcpdump need root?
Capturing raw packets is a privileged action. You can run it as root or give the binary the CAP_NET_RAW and CAP_NET_ADMIN capabilities.
How do I stop tcpdump and keep the file?
Press Ctrl+C, or send it SIGINT with kill -INT. tcpdump then flushes its buffer and closes the pcap file cleanly.
What is the difference between a capture filter and a display filter?
A capture filter (BPF, used by tcpdump) decides what gets recorded. A display filter (Wireshark syntax) only hides or shows packets that were already captured.

Go deeper

ShareXLinkedInWhatsApp