What you’ll get
Capture the full lifecycle of a short HTTP request in tcpdump -nn: the three-way handshake, data exchange, and
connection teardown:
sudo tcpdump -nn -i eth0 'tcp port 8080'
# 19:14:30.734148 IP 10.0.10.20.58954 > 10.0.10.10.8080: Flags [S], seq 1163048860, win 64240, options [mss 1460,sackOK,…,wscale 7], length 0
# 19:14:30.734172 IP 10.0.10.10.8080 > 10.0.10.20.58954: Flags [S.], seq 2422013359, ack 1163048861, win 65160, options [mss 1460,sackOK,…,wscale 7], length 0
# 19:14:30.734185 IP 10.0.10.20.58954 > 10.0.10.10.8080: Flags [.], ack 1, win 502, length 0
# 19:14:30.734243 IP 10.0.10.20.58954 > 10.0.10.10.8080: Flags [P.], seq 1:79, ack 1, win 502, length 78: HTTP: GET / HTTP/1.1
# 19:14:30.734250 IP 10.0.10.10.8080 > 10.0.10.20.58954: Flags [.], ack 79, win 509, length 0
# 19:14:30.734347 IP 10.0.10.10.8080 > 10.0.10.20.58954: Flags [P.], seq 1:61, ack 79, win 509, length 60: HTTP: HTTP/1.1 200 OK
# 19:14:30.734357 IP 10.0.10.20.58954 > 10.0.10.10.8080: Flags [.], ack 61, win 502, length 0
# 19:14:30.734378 IP 10.0.10.10.8080 > 10.0.10.20.58954: Flags [F.], seq 61, ack 79, win 509, length 0
# 19:14:30.734428 IP 10.0.10.20.58954 > 10.0.10.10.8080: Flags [F.], seq 79, ack 62, win 502, length 0
# 19:14:30.734441 IP 10.0.10.10.8080 > 10.0.10.20.58954: Flags [.], ack 80, win 509, length 0
The second line — the server’s SYN-ACK reply — contains nearly every parameter that TCP relies on:
win drops from 64240 to 502 because wscale 7 takes effect: 502 × 128 = 64,256 bytes.TCP header, seq/ack counters, and the win window
TCP does not deliver discrete messages — it carries a continuous byte stream sliced into segments no larger
than MSS (1460 bytes for IPv4 at MTU 1500, ICMP and MTU).
seq(Sequence Number) — the byte offset of the first payload byte in this segment. After the client sends the 78-byte requestGET / HTTP/1.1(seq 1:79), its next byte starts at79.ack(Acknowledgment Number) — the next byte the receiver expects from the peer. Returningack 79says: “every byte up to 78 arrived intact.” If a packet drops in transit, the sender stops seeingackadvance and retransmits the missing segment.win(Window) andwscale— flow control. The base header allocates 16 bits for window size (capping at 65,535 bytes), which is too small for fast links with latency. During the handshake, both sides negotiatewscale(RFC 7323,net.ipv4.tcp_window_scaling = 1), multiplying all subsequentwinvalues by2^wscale. In the capture above,wscale 7gives a multiplier of128, sowin 509represents an effective buffer of509 × 128 = 65,152bytes.
Flags live in the 14th byte of the TCP header — zero-based offset 13 (tcp[13] in tcpdump BPF filters).
18 (0x12)TCP flags and the tcp[13] bitmask
Every TCP packet carries a combination of flags in tcp[13]. tcpdump prints them inside brackets (Flags […]):
| Flag | In tcpdump |
Weight (hex) | Meaning |
|---|---|---|---|
SYN |
S |
2 (0x02) |
Request to open a connection, synchronize initial seq |
ACK |
. |
16 (0x10) |
ack field is valid — acknowledges received bytes (set on almost every packet after the first SYN) |
PSH |
P |
8 (0x08) |
Flush buffered data to the application immediately instead of waiting for a full buffer |
FIN |
F |
1 (0x01) |
Graceful close of one half of the connection (“no more data from this side”) |
RST |
R |
4 (0x04) |
Immediate abort: closed port, crashed process, or firewall REJECT |
In tcpdump, you can isolate specific packets — such as bare SYN, SYN-ACK, or any RST — using a bitmask on
tcp[13]. Exact match == requires only the selected bits (tcp[13] == 0x12 matches SYN+ACK exclusively),
while & != 0 triggers when at least one target bit is set (tcp[13] & 4 != 0 catches any RST, whether
[R] or [R.]):
sudo tcpdump -nn 'tcp[13] == 0x12'# by name: sudo tcpdump -nn 'tcp[tcpflags] == (tcp-ack|tcp-syn)'Testing the filter on live traffic — only SYN-ACK and RST-ACK show up, skipping thousands of data packets:
sudo tcpdump -nn -i eth0 'tcp[13] == 0x12 or tcp[13] & 4 != 0'
# 19:14:31.479205 IP 10.0.10.10.8080 > 10.0.10.20.58962: Flags [S.], seq 2553686243, ack 2005566322, win 65160, …
# 19:14:31.490140 IP 10.0.10.10.9999 > 10.0.10.20.48500: Flags [R.], seq 0, ack 3815482293, win 0, length 0
Handshake, RST, and DROP: three patterns in tcpdump
The first few packets in a capture immediately show where a connection gets stuck:
- Client✓ SYN -> ACK -> GET
- Server :8080✓ SYN-ACK -> 200 OK
- Teardown✓ FIN-ACK both ways
- Result✓ TIME-WAIT on server
19:14:30.734148 IP 10.0.10.20.58954 > 10.0.10.10.8080: Flags [S], seq 1163048860, win 64240, length 0 19:14:30.734172 IP 10.0.10.10.8080 > 10.0.10.20.58954: Flags [S.], seq 2422013359, ack 1163048861, win 65160, length 0 19:14:30.734185 IP 10.0.10.20.58954 > 10.0.10.10.8080: Flags [.], ack 1, win 502, length 0
Three packets established the connection, both peers negotiated mss and wscale, exchanged HTTP, and closed cleanly with FIN.
ss -tnp and sudo tcpdump -nn 'tcp port 8080'
- Client✓ SYN to :9999
- Server kernel✕ nothing listening
- Reply✕ Flags [R.] instantly
- curl (exit 7)✕ Connection refused
19:14:30.755324 IP 10.0.10.20.48498 > 10.0.10.10.9999: Flags [S], seq 1434612965, win 64240, length 0 19:14:30.755343 IP 10.0.10.10.9999 > 10.0.10.20.48498: Flags [R.], seq 0, ack 1434612966, win 0, length 0
The packet reached the host, but no LISTEN socket exists on 10.0.10.10:9999 (or a firewall REJECT rule fired).
sudo ss -tulnp 'sport = :9999' on the server
- Client✓ SYN to :8081
- nftables (drop)✕ packet silently dropped
- Retries✕ same seq after 1 s, 2 s…
- curl (exit 28)✕ Connection timed out
19:14:32.119491 IP 10.0.10.20.51654 > 10.0.10.10.8081: Flags [S], seq 1131254975, win 64240, length 0 19:14:33.122316 IP 10.0.10.20.51654 > 10.0.10.10.8081: Flags [S], seq 1131254975, win 64240, length 0 19:14:34.146328 IP 10.0.10.20.51654 > 10.0.10.10.8081: Flags [S], seq 1131254975, win 64240, length 0
No reply arrives at all: the client retransmits SYN with the same seq at increasing intervals (tcp_syn_retries = 6) until it times out.
sudo nft list ruleset and the return route (ip route get)
TCP states in ss: why TIME-WAIT vs CLOSE-WAIT matters
Every TCP socket has a state. In day-to-day debugging, five states come up most often:
State in ss |
What is happening | Sign of trouble |
|---|---|---|
LISTEN |
Socket waiting for incoming SYN |
Normal for a server |
SYN-RECV |
SYN arrived, server sent SYN-ACK and waits for final ACK |
Hundreds of rows point to a missing return route or SYN flood |
ESTAB |
Connection is open and transferring data | Normal |
TIME-WAIT |
Local side closed first (FIN sent and acknowledged); waiting 60 s |
Usually normal (protects against late packets) |
CLOSE-WAIT |
Peer sent FIN, kernel acknowledged it, but local program has not called close() |
Almost always an application bug or socket leak |
The core difference between the last two:
-
TIME-WAITbelongs to the kernel, not the process. In the capture above, the HTTP server initiated teardown (Flags [F.]from10.0.10.10:8080), soTIME-WAITappears on the server side with an emptyProcesscolumn — the program already closed its socket:ss -tan state time-wait # Recv-Q Send-Q Local Address:Port Peer Address:Port Process # 0 0 10.0.10.10:8080 10.0.10.20:58954 -
CLOSE-WAITis held by a live process. When the client closes a connection and the server forgets to callclose(), the socket stays stuck inCLOSE-WAITwithRecv-Q = 1(one unreadFINqueued) and stays tied to that PID until the process exits:sudo ss -tnp state close-wait # Recv-Q Send-Q Local Address:Port Peer Address:Port Process # 1 0 10.0.10.10:8080 10.0.10.20:40786 users:(("python3",pid=6687,fd=7))As those sockets accumulate, the process hits its open-file limit (
EMFILE: Too many open files) and stops accepting new connections. Fix this in the application code or config, not in kernel tunables.
If it doesn’t work
curl or nc fails immediately with Connection refused in under a millisecond
- Cause
- The
SYNpacket reached the server, but nothing listens on that port (or the firewall usesREJECT), so the kernel replied withRST(Flags [R.]). - Fix
- Run
sudo ss -tulnpon the server to verify the service is running and listening on the expected interface rather than127.0.0.1(Ports and sockets).
A connection hangs for 10–60 seconds and fails with Connection timed out
- Cause
- A firewall silently drops
SYNpackets (DROP), or return packets are lost along the path. The client retriesSYN(net.ipv4.tcp_syn_retries = 6) with exponential backoff and gives up. - Fix
- Run
tcpdump -nn 'tcp[13] == 0x02 or tcp[13] == 0x12'to check whetherSYNleaves andSYN-ACKreturns. On the server, inspectnftablesrules (inputandforward).
ss -tan on the server piles up hundreds or thousands of CLOSE-WAIT sockets, then the app crashes with Too many open files
- Cause
- Socket leak in application code: the remote client already sent
FINand closed its side, but the server process never calledclose()and still holds the file descriptor. - Fix
- Find the leaking process with
sudo ss -tnp state close-wait. Restarting clears the sockets temporarily; for a permanent fix, close connections properly in the app code (timeouts,finally/with).
After a load test or on a reverse proxy, ss -tan shows thousands of lines in TIME-WAIT
- Cause
- Normal TCP behavior: the side that sends
FINfirst holds the address/port pair for 60 seconds (2×MSL) so late packets from the old session cannot land in a new connection. - Fix
TIME-WAITby itself is not a leak. It only hurts if you exhaust outbound ephemeral ports (about 28,000 per destinationIP:port): enable HTTP keep-alive and connection pooling instead of breaking the TCP stack.
Data transfer stalls abruptly and win 0 (ZeroWindow) shows up in tcpdump
- Cause
- The receiver's buffer is full: the application is not reading data from the socket fast enough (for instance, a stuck handler or blocked disk), so TCP pauses the sender.
- Fix
- Inspect
Recv-QandSend-Qinss -tnp: a persistently highRecv-Qpoints to a slow receiving application rather than packet loss on the network.