Lesson 13 of 14 · 10 min · Troubleshooting networks

Packet capture with tcpdump

Covers: Ubuntu 24.04 LTS, tcpdump 4.99, libpcap 1.10. Updated 13 Nov 2026. Part of the DevOps Engineer roadmap.

What you will learn

  • When packet capture is the right tool, and when it is not
  • Running tcpdump with the right options
  • Capture filters for host, port, protocol and TCP flags
  • Reading TCP and DNS lines
  • Saving to a .pcap file and analysing it with Wireshark
  • Safety and privacy rules

In simple words

Sometimes two machines disagree about what was sent. A packet capture settles it: it is a CCTV recording of the network, showing each message that really passed. tcpdump makes that recording. This lesson shows how to record only what you need and how to read it.

Words to know

  • Packet capture: a recording of the network messages going in and out of a machine.
  • tcpdump: the command-line tool that captures packets.
  • Filter: a rule that keeps only the traffic you care about, such as one port.
  • pcap file: a saved capture that you can open later in a tool such as Wireshark.

When to capture

Logs show what applications think happened. A packet capture shows what actually crossed the wire. Use it when:

  • two sides disagree ("we sent it" / "we never got it"),
  • connections reset, hang or time out and you need to see who sent the reset or which packet is missing,
  • you need to verify that traffic leaves an interface, passes a firewall or reaches a host,
  • you debug DNS, TLS handshakes or protocols with unclear behaviour.

Capturing needs root (or the right capabilities), and it can produce enormous amounts of data and expose secrets. Reach for it after the simpler tools (ss, curl -v, logs) have not explained the problem.

Basic use

sudo tcpdump -i eth0 -nn 'tcp port 8080'
Option Meaning
-i eth0 interface to listen on (-i any for all; lo for loopback). List with tcpdump -D
-n / -nn do not resolve host names (-nn also not port names). Always use it: lookups slow things and generate their own DNS traffic
-c 20 stop after 20 packets
-w file.pcap write raw packets to a file instead of printing
-r file.pcap read a saved file
-A / -X show packet payload as ASCII / hex+ASCII
-s 0 capture whole packets (default is enough for headers)
-v, -vv more protocol detail
-G 60 -W 5 rotate files every 60 seconds, keep 5

The part in quotes is a capture filter (BPF syntax), applied by the kernel so that irrelevant traffic is never copied. Always filter on busy machines.

Filters you will use

host 10.0.1.5                  traffic to or from this host
src host 10.0.1.5 / dst host   one direction
net 10.0.1.0/24               a subnet
port 443                       either TCP or UDP port 443
tcp port 8080 / udp port 53   one protocol
portrange 8000-8100
icmp, arp
not port 22                    exclude (use this to hide your own SSH session!)
'tcp[tcpflags] & tcp-syn != 0' packets with SYN set
'tcp[tcpflags] & tcp-rst != 0' resets

Combine with and, or, not and parentheses: 'host 10.0.1.5 and (port 80 or port 443) and not port 22'. Note the capture filter language differs from Wireshark's display filters (tcp.port == 80).

Reading TCP lines

We captured a real request to a local web server:

IP 127.0.0.1.37042 > 127.0.0.1.8080: Flags [S],  seq 496300367, win 65495, ...
IP 127.0.0.1.8080 > 127.0.0.1.37042: Flags [S.], seq 810009957, ack 496300368, ...
IP 127.0.0.1.37042 > 127.0.0.1.8080: Flags [.],  ack 1, ...
IP 127.0.0.1.37042 > 127.0.0.1.8080: Flags [P.], seq 1:78, ack 1, ... length 77: HTTP: GET / HTTP/1.1
IP 127.0.0.1.8080 > 127.0.0.1.37042: Flags [P.], seq 1:186, ack 78, ... length 185: HTTP: HTTP/1.0 200 OK

Format: source.port > destination.port: Flags [..], seq, ack, win, length. Flags: S SYN, S. SYN-ACK, . ACK, P. data (PSH+ACK), F. FIN, R RST. With -A the payload is visible. Because this was plain HTTP, the request line and headers are readable:

GET / HTTP/1.1
Host: 127.0.0.1:8080

This is exactly why unencrypted protocols are dangerous: anyone able to capture on the path can read them. With HTTPS the same capture shows only the TLS handshake and encrypted data.

Patterns that diagnose problems

  • Repeated [S] with no [S.]: the SYN is not getting a reply: a firewall dropping it, wrong IP, or a down host. This is the signature of a timeout.
  • [S] answered immediately by [R.]: nothing is listening on that port ("connection refused").
  • [R] or [R.] in the middle of a connection: one side aborted it. The capture shows who sent it: an app crash, a firewall idle-timeout, a load balancer or a proxy.
  • Many retransmissions (the same seq sent again): packet loss on the path.
  • win 0: the receiver's buffer is full; the application is not reading fast enough.
  • Long gaps between a request and its response: the server is slow, not the network.

DNS

sudo tcpdump -n -i eth0 'udp port 53'
172.17.0.2.49023 > 192.168.0.1.53: 51526+ [1au] A? example.com. (52)
192.168.0.1.53 > 172.17.0.2.49023: 51526 2/0/1 A 172.66.147.243, A 104.20.23.154 (72)

The first line is the query (query id 51526, type A? for example.com.), the second the response from the resolver with the same id: 2/0/1 means 2 answers, 0 authority, 1 additional record, then the two addresses. If you see queries but no responses, DNS is the problem.

Saving and analysing with Wireshark

sudo tcpdump -nn -i eth0 -w /tmp/issue.pcap 'host 10.0.1.5 and tcp port 8080'
# reproduce the problem, then Ctrl+C
tcpdump -nn -r /tmp/issue.pcap | head

A .pcap file can be copied to your laptop and opened in Wireshark, which decodes hundreds of protocols, follows a TCP stream (Follow TCP Stream), colours problems and can decrypt TLS if you have the session keys. Our small capture: 12 packets, 1296 bytes. Capture on the server and, if possible, on the client at the same time; comparing both ends shows exactly where packets disappear.

On a headless server, capture with tcpdump -w and analyse elsewhere. In Kubernetes, use an ephemeral debug container or kubectl debug with a network-tools image to capture inside a pod's network namespace.

Safety, privacy and good practice

  • Captures contain secrets: passwords on plain protocols, cookies, tokens, personal data. Treat a pcap like a credential: store it securely, share only with the people who need it, delete it afterwards.
  • Only capture on networks you are authorised to monitor. Capturing other people's traffic without permission can be illegal and violate company policy.
  • Do not capture to the disk you are debugging (if it is full) and limit size: -c, -C 100 (rotate at 100 MB), -G.
  • Exclude your own session: not port 22 avoids a feedback loop when capturing over SSH.
  • Performance: on very busy links tcpdump can drop packets (N packets dropped by kernel). Use tight filters and write to a file.

Common mistakes

  • No filter on a busy interface.
  • Forgetting -nn, causing slowness and DNS noise.
  • Capturing on the wrong interface (any, lo vs eth0); in containers the interface names differ.
  • Looking for HTTPS content in a capture (it is encrypted).
  • Concluding "packet loss" from one missing packet without checking both ends of the path, or capture offloading artefacts (checksums look wrong on the sending host).
  • Leaving captures lying around.

Try it yourself

  1. Start a local web server on port 8080, capture with sudo tcpdump -nn -i lo -A 'tcp port 8080', run curl http://127.0.0.1:8080/, and find the handshake, the request text and the response.
  2. Capture DNS while running dig example.com, and match the query id of the request and the response.
  3. Block a port with iptables -j DROP and capture a curl to it; compare with a closed port (no firewall). What are the packets in each case?
  4. Save a capture with -w, open it in Wireshark and use "Follow TCP Stream".

Check yourself

What does a series of repeated SYN packets with no reply indicate?

The SYN is not reaching a responder: a firewall or security group dropping it, a routing problem, or the destination is down. A closed port would answer immediately with RST.

Why use -nn with tcpdump?

It disables host and port name resolution, so output is faster and the capture is not polluted with its own DNS lookups.

What is the difference between a capture filter and a display filter?

A capture filter (tcpdump/BPF) decides in the kernel which packets are captured at all. A display filter (Wireshark) only hides packets in an existing capture. The syntaxes differ.

Why can you read an HTTP request in a capture but not an HTTPS request?

HTTP is plain text on the wire. HTTPS is encrypted by TLS after the handshake, so a capture shows only the handshake (including the server name in SNI unless encrypted) and encrypted data.

Interview questions

A client says connections to your service sometimes reset. How would you use tcpdump?

Capture at the server (and the client or load balancer) filtering by the client IP and port with tcp[tcpflags] & tcp-rst != 0 or the full conversation; find who sends the RST and when relative to the last data packet to tell an app crash, an idle timeout or a middlebox apart.

How do you capture traffic on a remote server without filling the disk or locking yourself out?

Tight filters including not port 22, -c or -C/-G limits, write to a file in a location with free space, and analyse offline.

What does tcp[tcpflags] & tcp-syn != 0 match?

Packets with the SYN flag set (both SYN and SYN-ACK), useful for watching connection attempts.

Summary

  • tcpdump shows what really crossed the wire; use filters, -nn, and -w to save for Wireshark.
  • Read the flags and seq/ack; SYN with no reply = drop, immediate RST = closed port, retransmissions = loss, win 0 = slow receiver.
  • Plain protocols reveal everything; HTTPS shows only the handshake.
  • Treat captures as sensitive data, capture only where authorised, and keep them small and short-lived.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on Networking for DevOps for DevOps Engineer and scores every answer, with what to fix.