December 5, 2026

tcpdump Basics: Capturing One Connection, Reading the Flags, and Filters That Keep It Small

A small inline network tap between two Ethernet cables beside a switch

A web request that “just fails” leaves the application with a timeout and nothing else to say. On the wire, the same failure is two packets long: a request to connect, and a reset coming straight back. tcpdump shows those packets. It also shows a successful request in full, twelve packets from handshake to goodbye, which is the baseline every broken connection gets compared with. Every capture below was taken on a Linux test machine with tcpdump 4.99.4, between curl and a small local web server, and the output shown is what tcpdump printed.

Video: BlueMonkey 4n6 on YouTube

Capturing to a file

-w and -r

The tcpdump manual describes -w as writing the raw packets to file rather than parsing and printing them, to be printed later with -r. Capturing to a file first is the better habit: the capture is small, can be read as many times as needed with different filters, and can be opened in Wireshark on another machine.

$ sudo tcpdump -i lo -n -w cap.pcap 'tcp port 8077'
$ curl -s -o /dev/null http://127.0.0.1:8077/
$ tcpdump -n -r cap.pcap | wc -l
12

One HTTP request and response produced 12 packets and a 1,485-byte capture file. -i lo chose the loopback interface, since client and server were on the same machine; tcpdump -D lists the interfaces available, which on the test machine included eth0, lo, a Docker bridge and the special any device.

-c

-c makes tcpdump exit after receiving or reading count packets. On a busy interface, a capture that stops itself after a few hundred packets is safer than one left running.

Old telephone

Reading a TCP conversation

The twelve packets

The capture of the successful request, trimmed to the parts that matter:

127.0.0.1.53776 > 127.0.0.1.8077: Flags [S],  seq 1834344705
127.0.0.1.8077 > 127.0.0.1.53776: Flags [S.], seq 2462510799, ack 1834344706
127.0.0.1.53776 > 127.0.0.1.8077: Flags [.],  ack 1, length 0
127.0.0.1.53776 > 127.0.0.1.8077: Flags [P.], seq 1:78, ack 1
127.0.0.1.8077 > 127.0.0.1.53776: Flags [.],  ack 78, length 0
127.0.0.1.8077 > 127.0.0.1.53776: Flags [P.], seq 1:157, ack 78
127.0.0.1.53776 > 127.0.0.1.8077: Flags [.],  ack 157, length 0
127.0.0.1.8077 > 127.0.0.1.53776: Flags [P.], seq 157:385, ack 78
127.0.0.1.53776 > 127.0.0.1.8077: Flags [.],  ack 385, length 0
127.0.0.1.8077 > 127.0.0.1.53776: Flags [F.], seq 385, ack 78
127.0.0.1.53776 > 127.0.0.1.8077: Flags [F.], seq 78, ack 386
127.0.0.1.8077 > 127.0.0.1.53776: Flags [.],  ack 79, length 0

What the flags mean

The manual gives the key: TCP flags print as some combination of S (SYN), F (FIN), P (PSH), R (RST) and others, or . (ACK). Read top to bottom, the conversation is:

  • [S], [S.], [.]: the three-way handshake. The client asks to connect, the server agrees and acknowledges, the client acknowledges back.
  • [P.] seq 1:78: the client sends 77 bytes, the HTTP request.
  • [P.] seq 1:157 and seq 157:385: the server sends 384 bytes in two pieces, the response headers and the page.
  • [F.] in both directions: each side closes its half of the connection.

After the first two packets, tcpdump shows sequence numbers relative to the start (seq 1:78) instead of the large absolute values, which makes byte counts readable at a glance.

Timestamps that answer “how long”

Each line starts with a timestamp, and tcpdump can print it several ways. -tttt adds the date, which matters when lining a capture up against application logs. -ttt prints the time since the previous packet instead, which turns a capture into a latency report. On the test capture, the gap between the client’s SYN and the server’s SYN-ACK was 15 microseconds, and the third packet followed 11 microseconds later. On a real network the same view shows at a glance whether a slow request was slow to connect, slow to get a first byte back, or slow to finish, and those three point to different culprits: the network path, the server’s processing, and the size of the response.

The failure that is two packets long

Connection refused, on the wire

The same curl pointed at port 8099, where nothing was listening:

127.0.0.1.33224 > 127.0.0.1.8099: Flags [S], seq 1362255600
127.0.0.1.8099 > 127.0.0.1.33224: Flags [R.], seq 0, ack 1362255601, win 0, length 0

A connection request and an immediate reset: the host is reachable, and it is saying nothing listens on that port. That is the packet-level form of the “Connection refused” errors covered in the post on nginx 502 errors.

Three patterns worth memorizing

  • [S] answered by [R.]: the host is up, the port is closed.
  • [S] repeated with no answer: something is dropping packets, usually a firewall, or the host is down.
  • [S], [S.], [.], then silence or resets after data: the connection works and the problem is in what is sent over it.
Closed shop front

UDP looks different

No handshake, no close

UDP has no connection to set up or tear down, so a UDP exchange is just the messages. A small test server on port 5399 answered a 4-byte ping with a 9-byte pong:ping, and the whole conversation was two lines:

127.0.0.1.43866 > 127.0.0.1.5399: UDP, length 4
127.0.0.1.5399 > 127.0.0.1.43866: UDP, length 9

The UDP version of “refused”

When nothing listens on a UDP port, there is no reset flag to send. The operating system answers with an ICMP message instead. Capturing 'udp port 5398 or icmp' while a single byte was sent to an unused port showed:

127.0.0.1.34937 > 127.0.0.1.5398: UDP, length 1
127.0.0.1 > 127.0.0.1: ICMP 127.0.0.1 udp port 5398 unreachable, length 37

This is why a UDP filter should often include or icmp. Without it, the capture shows a request going out and nothing coming back, which looks like packet loss when the real answer, “port unreachable”, was sent and filtered out. DNS and other UDP services that seem to time out are worth checking this way. A firewall that drops instead of rejecting produces the silence for real, and then the absence of the ICMP reply is the clue.

Long captures without filling the disk

A ring of files

A capture left running to catch an intermittent fault can grow without limit. tcpdump can rotate: -C sets a maximum file size in millions of bytes, and -W limits how many files are kept, overwriting the oldest. A test capture with -C 1 -W 3, while three 3 MB downloads ran, ended as three files named rot.pcap0, rot.pcap1 and rot.pcap2, two of them just over 1 MB each, and the oldest already recycled when the capture was stopped. About 9 MB of traffic passed; the disk held only the most recent two-megabyte slice of it.

One detail from the test: the first attempt failed silently with exit status 1, because tcpdump drops root privileges after opening the interface and the unprivileged user could not create files in the capture directory. Writing to a directory that user can write to, or passing -Z root, fixed it. A capture that leaves no file behind is worth checking for exactly that.

Coffee being brewed

Filters

Capture filters

The quoted expression at the end of the command is a filter, written in the pcap filter language, and it decides which packets are recorded at all. The common building blocks:

host 192.168.1.20            # to or from one address
port 443                     # either direction, TCP or UDP
tcp port 8077                # TCP only
src host 10.0.0.5            # from one address
not port 22                  # everything except SSH
host 10.0.0.5 and port 53    # combined

Excluding SSH matters when capturing on a remote machine over SSH: without not port 22, tcpdump records its own session’s traffic, which generates more traffic, which it records.

Matching flags

Filters can test individual header bits. Reading the saved capture with 'tcp[tcpflags] & tcp-syn != 0' returned exactly the two handshake packets with SYN set, the [S] and the [S.]. On a busy server, a SYN-only capture shows every new connection attempt without the data that follows, which is the fastest way to see who is trying to connect and whether anything is answering.

Names or numbers: -n and -nn

By default tcpdump turns addresses and ports into names where it can. Read without -n, the first packet printed as localhost.53776 > localhost.8077; with -nn, as 127.0.0.1.53776 > 127.0.0.1.8077. The manual describes the option as not converting addresses, host addresses, port numbers, etc., to names. On a real network the lookups are not just cosmetic: each one can be a DNS query, which shows up in the capture and slows tcpdump down. -nn is the usual default for troubleshooting.

Payloads: -A for plain-text protocols

-A prints each packet in ASCII, which the manual notes is handy for capturing web pages. On the test capture, searching that output found the request line GET / HTTP/1.1 and the response line HTTP/1.0 200 OK. That works only for unencrypted traffic; on HTTPS, the packets after the handshake are ciphertext, and tcpdump shows that a conversation happened and how big it was, not what it said.

A short routine

When a connection misbehaves

  • Capture to a file with a tight filter: sudo tcpdump -i <iface> -nn -c 500 -w out.pcap 'host <peer> and port <port>'.
  • Reproduce the problem once, then stop.
  • Read the file and look at the first few packets: did the handshake complete?
  • If it did, look at sizes and timing; large packets that never get acknowledged point to the MTU problems covered in the post on MTU and MSS.
  • If traffic should be tagged on a trunk port, capture on the parent interface with -e to see VLAN tags, as in the post on VLANs at home.

Capturing usually needs root, and a capture can contain passwords and personal data from unencrypted protocols. A .pcap file deserves the same care as any other file holding other people’s traffic.

Sources

  • tcpdump(1) manual page, tcpdump.org: https://www.tcpdump.org/manpages/tcpdump.1.html

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

One Response to “tcpdump Basics: Capturing One Connection, Reading the Flags, and Filters That Keep It Small”

Leave a Reply

Your email address will not be published. Required fields are marked *