Skip to main content

Web servers and networking · Intermediate · 18 min

Watch a TCP connection open and close with tcpdump

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · tcpdump 4.99.6

You will capture a curl request to a local web server with tcpdump on Ubuntu 26.04, read its handshake, data and teardown line by line, see the reset a closed port sends, and save, filter and rotate captures as pcap files.

On this page

Most connection problems come down to a question you cannot answer from logs alone: did the packets actually go out, and what came back? tcpdump answers it by printing every packet that crosses an interface, one line each. Once you can read those lines, "the site times out", "connection refused" and "it connects but hangs" stop being guesses and become three different patterns you can point at.

In this tutorial you run a tiny web server on the loopback interface, so nothing leaves the machine, and watch one curl request from the first SYN to the last ACK. Then you look at the reset a closed port sends, make the output more readable, narrow it with filters, and save captures to files you can read later or copy to a laptop.

Only capture traffic you are allowed to see. On your own server and your own test traffic that is you. On a shared server, a client's network or an employer's network, get permission first: a capture can contain passwords, session cookies and personal data, and in many countries intercepting other people's traffic without authority is a crime. Treat saved capture files like secrets and delete them when you are done.

Packet timings, port numbers and sequence numbers differ on every run, so the sample output below is "something like" what you will see.

Step 1 · Install tcpdump and find the loopback interface

Bash
sudo apt update
sudo apt install -y tcpdump curl python3
tcpdump --version

The first line of the version output reads tcpdump version 4.99.6 on Ubuntu 26.04, followed by the libpcap version it was built with. Everything in this tutorial works on any 4.99 release.

List the interfaces tcpdump can capture on:

Bash
sudo tcpdump -D

Something like:

Text
1.ens3 [Up, Running, Connected]
2.any (Pseudo-device that captures on all interfaces) [Up, Running]
3.lo [Up, Running, Loopback]

Capturing needs root (strictly, the CAP_NET_RAW capability), which is why every capture command below starts with sudo. Reading a saved file does not.

Check it worked

tcpdump --version prints a 4.99 version and sudo tcpdump -D lists lo with Loopback next to it. If sudo tcpdump -D says You don't have permission, you ran it without sudo.

Step 2 · Start a small web server to watch

Make a demo folder with one page in it and serve it with Python's built-in web server on port 8080, bound to 127.0.0.1 so only the server itself can reach it:

Bash
mkdir -p ~/tcpdump-demo/site
echo 'hello from tcpdump-demo' > ~/tcpdump-demo/site/index.html
cd ~/tcpdump-demo
nohup python3 -m http.server 8080 --bind 127.0.0.1 --directory ~/tcpdump-demo/site > server.log 2>&1 &
echo $! > server.pid
sleep 1

The & runs the server in the background of this terminal, and server.pid keeps its process ID so you can stop it at the end.

Check it worked

Bash
curl -s http://127.0.0.1:8080/

curl prints hello from tcpdump-demo. Use 127.0.0.1 rather than localhost throughout: localhost can resolve to the IPv6 address ::1 first, and the server is only listening on IPv4.

Step 3 · Capture one request

You need two terminals for this. In the second SSH session, start a capture on the loopback interface that only shows TCP traffic to or from port 8080:

Bash
sudo tcpdump -i lo -n 'tcp port 8080'

It prints listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes and waits. -i lo picks the interface and -n stops tcpdump turning addresses and port numbers into names, which keeps the output short and avoids slow DNS lookups. You will often see -nn in other guides; on tcpdump 4.99 one -n already covers both addresses and ports, and -nn still works.

In the first session, send one request:

Bash
curl -s http://127.0.0.1:8080/

Back in the second session, press Ctrl+C. tcpdump prints a summary, such as 11 packets captured, and the lines above it look something like this (each one is a single long line on your screen):

Text
10:42:07.315120 IP 127.0.0.1.50312 > 127.0.0.1.8080: Flags [S], seq 2711958360, win 65495, options [mss 65495,sackOK,TS val 391207 ecr 0,nop,wscale 7], length 0
10:42:07.315131 IP 127.0.0.1.8080 > 127.0.0.1.50312: Flags [S.], seq 1188232401, ack 2711958361, win 65483, options [mss 65495,sackOK,TS val 391207 ecr 391207,nop,wscale 7], length 0
10:42:07.315141 IP 127.0.0.1.50312 > 127.0.0.1.8080: Flags [.], ack 1, win 512, options [nop,nop,TS val 391207 ecr 391207], length 0
10:42:07.315190 IP 127.0.0.1.50312 > 127.0.0.1.8080: Flags [P.], seq 1:79, ack 1, win 512, options [nop,nop,TS val 391207 ecr 391207], length 78: HTTP: GET / HTTP/1.1
10:42:07.315195 IP 127.0.0.1.8080 > 127.0.0.1.50312: Flags [.], ack 79, win 512, options [nop,nop,TS val 391207 ecr 391207], length 0
10:42:07.316402 IP 127.0.0.1.8080 > 127.0.0.1.50312: Flags [P.], seq 1:190, ack 79, win 512, options [nop,nop,TS val 391208 ecr 391207], length 189: HTTP: HTTP/1.0 200 OK
10:42:07.316420 IP 127.0.0.1.50312 > 127.0.0.1.8080: Flags [.], ack 190, win 511, options [nop,nop,TS val 391208 ecr 391208], length 0
10:42:07.316455 IP 127.0.0.1.8080 > 127.0.0.1.50312: Flags [P.], seq 190:214, ack 79, win 512, options [nop,nop,TS val 391208 ecr 391208], length 24: HTTP
10:42:07.316480 IP 127.0.0.1.8080 > 127.0.0.1.50312: Flags [F.], seq 214, ack 79, win 512, options [nop,nop,TS val 391208 ecr 391208], length 0
10:42:07.316590 IP 127.0.0.1.50312 > 127.0.0.1.8080: Flags [F.], seq 79, ack 215, win 512, options [nop,nop,TS val 391208 ecr 391208], length 0
10:42:07.316601 IP 127.0.0.1.8080 > 127.0.0.1.50312: Flags [.], ack 80, win 512, options [nop,nop,TS val 391208 ecr 391208], length 0

Each line has the same shape: time, protocol (IP), source address and port, >, destination address and port, then the TCP details. The port after the last dot is the port: 127.0.0.1.50312 is port 50312, a temporary port the kernel picked for curl, and 127.0.0.1.8080 is the server.

Check it worked

Your capture has about ten lines. The first one goes from the high port to 8080 and shows Flags [S], and at least two near the end show Flags [F.] (or [FP.], when a FIN travels with the last piece of data). If you see nothing at all, check that you used -i lo and that curl talked to 127.0.0.1, not a public address.

Step 4 · Read the handshake, the data and the teardown

The Flags field tells you what each packet is for. tcpdump writes one letter per TCP flag: S for SYN, F for FIN, P for PSH, R for RST, U for URG, E and W for the ECN flags, and a dot for ACK. So [S.] is SYN plus ACK, and [.] is an ACK on its own.

Sequence diagram with curl on the left and the Python server on the right: three handshake arrows labelled S, S. and dot, five data arrows in capture order: curl's GET request, the server's ACK of it, the 200 OK headers, curl's ACK of the headers, and the page body, and three teardown arrows labelled F., F. and a final ACK
The capture from the previous step, one arrow per line

Read your capture in three groups:

  • Handshake, the first three lines, all length 0. curl sends [S] with its starting sequence number, the server answers [S.] with its own and acknowledges curl's (ack is curl's number plus one), and curl's [.] completes it. From here the connection is open. On loopback this takes microseconds; across the internet it costs one full round trip before any data moves, which is why the handshake shows up in curl's time_connect.
  • Data, lines with length above 0. curl's [P.] carries the 78-byte HTTP request: seq 1:79 means bytes 1 to 78 of what curl sends. The server's [P.] lines carry the response headers and the page. Each side then sends [.] with an ack that says how many bytes it has received. After the handshake tcpdump shows sequence numbers relative to the start, which is why they drop to 1; add -S if you want the raw numbers.
  • Teardown, the last lines. A FIN means "I have nothing more to send". Each side sends its own FIN and each acknowledges the other's, so a clean close is a FIN from one side, a FIN from the other and a final ACK. Here the server closes first, because Python's server speaks HTTP/1.0 and closes the connection after every response. With a keep-alive server such as Nginx, curl usually closes first.

The other fields are worth knowing but rarely the problem: win is how much more data the sender of that line can accept right now, and options carries the maximum segment size (mss), timestamps (TS val, ecr) and window scaling agreed during the handshake.

Check it worked

Find these in your own output and write down their line numbers: the line with [S], the one with [S.], the [P.] line from curl that carries the request, and the last [F.]. The ack on the [S.] line is exactly one more than the seq on the [S] line, and every line before the request has length 0.

Step 5 · See what a closed port sends back

Nothing is listening on port 8081. In the second session, capture it:

Bash
sudo tcpdump -i lo -n 'tcp port 8081'

In the first session, try to connect:

Bash
curl -sS http://127.0.0.1:8081/ || echo "curl exit code: $?"

curl fails at once with Failed to connect to 127.0.0.1 port 8081 and exit code 7. Press Ctrl+C in the second session. There are only two lines:

Text
10:44:51.802114 IP 127.0.0.1.50318 > 127.0.0.1.8081: Flags [S], seq 3418824117, win 65495, options [mss 65495,sackOK,TS val 555694 ecr 0,nop,wscale 7], length 0
10:44:51.802127 IP 127.0.0.1.8081 > 127.0.0.1.50318: Flags [R.], seq 0, ack 3418824118, win 0, length 0
Two arrows: curl sends a SYN to port 8081, and the Linux kernel answers with an RST and ACK because no program is listening on that port
Connection refused, as two packets

The SYN reached the machine, no program had that port open, so the kernel itself answered [R.]: reset, with an ACK of the SYN. That is what "connection refused" means. Compare it with the two other patterns you will meet in production:

  • SYN, then the same SYN again a second later, then again after two, four, eight seconds, and nothing back. A firewall on the way is dropping the packets. The client sees a timeout, not a refusal.
  • Handshake completes, the request goes out, then silence or an RST partway through. The port is open but the program behind it stalled or crashed, or something in the middle (a load balancer, a proxy) cut the connection.

Check it worked

Your capture shows one [S] line towards 8081 and one [R.] line back, and curl printed curl exit code: 7. If curl hangs instead of failing at once, something is listening on 8081 or a local firewall rule is dropping loopback traffic; check with ss -tlnp 'sport = :8081'.

Step 6 · Make the output readable with timestamps, payloads and filters

Three options change what each line shows:

  • -tttt prints the full date and time on each line, which you need when you compare a capture with log files.
  • -A prints each packet's contents as text under its header line. For plain HTTP that shows the request and response; for HTTPS it shows encrypted noise, which is the point of TLS.
  • -c <n> stops after n packets, so a capture ends on its own.

Filters decide which packets you see at all. tcpdump passes the expression after the options to the kernel as a BPF filter, so packets that do not match are dropped before tcpdump even sees them, which keeps captures cheap on a busy server. Quote the whole expression so the shell leaves it alone. The ones you will use most:

| Filter | Matches |
|---|---|
| port 8080 | Traffic to or from port 8080, TCP or UDP |
| tcp port 8080 | Only TCP on port 8080 |
| host 127.0.0.1 | Traffic to or from that address |
| src port 8080 | Only packets sent from port 8080 (the server's replies) |
| dst host 10.0.0.5 | Only packets going to that address |
| tcp[tcpflags] & tcp-syn != 0 | Any packet with SYN set: new connections and their replies |
| tcp[tcpflags] & tcp-rst != 0 | Only resets: refused or aborted connections |
| not port 22 | Everything except SSH |

Combine them with and, or, not and brackets. tcp[tcpflags] reads the flags byte of the TCP header, and tcp-syn, tcp-fin, tcp-rst, tcp-push and tcp-ack are names for its bits. Join several bits with | inside brackets to match any of them, as the next command does.

Show only the packets that open and close a connection, and stop after four. In the second session:

Bash
sudo tcpdump -i lo -n -tttt -c 4 'tcp port 8080 and tcp[tcpflags] & (tcp-syn|tcp-fin|tcp-rst) != 0'

In the first session:

Bash
curl -s http://127.0.0.1:8080/

tcpdump exits by itself with 4 packets captured: the [S], the [S.] and the two FINs, each starting with a date such as 2026-11-18 10:46:30.118204.

Now look inside the packets that carry data. PSH is set on segments that carry application data, so this shows the request and the response and skips the bare ACKs. In the second session:

Bash
sudo tcpdump -i lo -n -A 'tcp port 8080 and tcp[tcpflags] & tcp-push != 0'

Run the same curl in the first session, then press Ctrl+C. Under the header lines you can read GET / HTTP/1.1, Host: 127.0.0.1:8080, User-Agent: curl/..., then HTTP/1.0 200 OK, the response headers and hello from tcpdump-demo. A few unreadable characters before each one are the IP and TCP headers printed as text.

On a server's public interface, always exclude your own SSH session. Otherwise every line tcpdump prints is sent to you over SSH, which is captured and printed, which is sent again, and the output floods. Find the interface name with ip route show default, then for example:

Bash
sudo tcpdump -i ens3 -n -c 50 'tcp port 443 and not port 22'

Check it worked

The flags capture stopped on its own after exactly four lines, each starting with today's date. The -A capture shows the words GET / HTTP/1.1 and hello from tcpdump-demo in plain text.

Step 7 · Save a capture to a file and read it back

Printing to the screen is fine for a quick look. For anything you want to study, compare or share, write the raw packets to a pcap file with -w and read it later with -r.

One detail matters first. When tcpdump runs as root, it switches to an unprivileged tcpdump user as soon as it has opened the interface, before it opens the output file. That user cannot write in your home folder, so a plain -w ~/file.pcap fails with Permission denied. -Z tells tcpdump which user to switch to; giving it your own user name makes the file yours. Keep the .pcap extension too: Ubuntu's AppArmor profile for tcpdump only allows writing files with capture extensions, or files in your home folder.

In the second session, start a capture to a file:

Bash
sudo tcpdump -i lo -n -w ~/tcpdump-demo/web.pcap -Z "$(id -un)" 'tcp port 8080'

It prints listening on lo... and then nothing else, because packets now go to the file. In the first session, send two requests:

Bash
curl -s http://127.0.0.1:8080/
curl -s http://127.0.0.1:8080/

Press Ctrl+C in the second session. It reports how many packets it captured, something like 22 packets captured.

Reading a file needs no sudo. Print it exactly as if it were live, then apply a filter to the saved packets. tcp[tcpflags] == tcp-syn matches packets where SYN is the only flag set, which means one line per new connection:

Bash
ls -l ~/tcpdump-demo/web.pcap
tcpdump -n -r ~/tcpdump-demo/web.pcap
tcpdump -n -r ~/tcpdump-demo/web.pcap 'tcp[tcpflags] == tcp-syn'

The file is a standard format. Copy it to your laptop with scp and open it in Wireshark when you want a graphical view, or keep it next to an incident report as evidence of what the network did.

Check it worked

ls -l shows web.pcap owned by your user, the full read shows two complete conversations, and the SYN-only filter prints exactly two lines, one per curl.

Step 8 · Rotate capture files and clean up

A capture left running on a busy server can fill the disk. -C and -W together turn the output into a ring of fixed-size files: -C 1 starts a new file every 1,000,000 bytes (tcpdump counts in millions of bytes, not mebibytes), and -W 3 keeps three files, overwriting the oldest when the fourth would start. With -W, tcpdump numbers the files with enough leading zeros to sort correctly, so ring.pcap becomes ring.pcap0, ring.pcap1 and ring.pcap2.

Make a 3 MB file to download so there is enough traffic to rotate:

Bash
head -c 3000000 /dev/urandom > ~/tcpdump-demo/site/big.bin

In the second session:

Bash
sudo tcpdump -i lo -n -w ~/tcpdump-demo/ring.pcap -C 1 -W 3 -Z "$(id -un)" 'tcp port 8080'

In the first session, download it twice, about 6 MB of traffic:

Bash
curl -s -o /dev/null http://127.0.0.1:8080/big.bin
curl -s -o /dev/null http://127.0.0.1:8080/big.bin

Press Ctrl+C in the second session, then list the files:

Bash
ls -l ~/tcpdump-demo/ring.pcap*

There are three files, none much over 1 MB (the one tcpdump was writing when you stopped it is smaller), even though more than 6 MB crossed the interface: the ring kept only the most recent traffic.

When files have to travel over a slow or metered link, such as a prepaid mobile connection, capture headers only. -s 128 keeps the first 128 bytes of each packet, which is enough for the IP and TCP headers and every flag you have read in this tutorial, and makes the files a small fraction of the size. Add it to any -w command; tcpdump marks cut packets as truncated when you read them.

Check it worked

ls shows three ring.pcap files, none much over 1 MB, and each one opens with tcpdump -n -r.

Stop the web server and remove everything this tutorial created, including the capture files:

Bash
kill "$(cat ~/tcpdump-demo/server.pid)"
cd ~
rm -r ~/tcpdump-demo

curl -s http://127.0.0.1:8080/ now fails, and ls ~/tcpdump-demo reports that the folder does not exist.

Next steps

You can now tell a refused connection from a dropped one, see whether a request left the server and what came back, and keep a bounded capture running while you wait for a fault.

  • Find which program owns the port you were watching: /tutorials/find-what-is-listening-on-a-port-with-ss
  • Put the handshake in context with DNS, TLS and timing: /tutorials/how-a-request-reaches-your-server
  • See how HTTP/2 and HTTP/3 change what goes over the connection: /tutorials/compare-http-1-1-http-2-and-http-3
  • Follow packets past your own server: /tutorials/trace-the-network-path-with-traceroute-and-mtr

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.