Opscraft
Foundations: Linux, scripting, Git & networking

The TCP handshake

Must-know8 lessons · 27 min · 3 tests ·

Every TCP connection — every HTTPS request, database query, kubectl call — starts with the three-way handshake and ends with a FIN or RST exchange. Understanding the handshake, the socket states it creates, and the kernel queues behind it lets you read tcpdump output, tell a firewall drop from a dead service in seconds, explain why TIME_WAIT and CLOSE_WAIT pile up, defend against SYN floods, and reason about latency. This topic pairs with TCP/UDP (transport fundamentals) and TLS (what happens right after the handshake).

Stage 1 · Foundations

Know these first

After this topic you can

  • Walk through the three-way handshake and connection close
  • Read connection states with ss
  • Explain SYN floods, TIME_WAIT and connection limits

Lessons

  1. 1The three-way handshake 3 min read
    • The exchange
    • Why sequence numbers are exchanged
    • Why three steps, not two
    • Options negotiated in the handshake
    • What the ACK number means
  2. 2Closing connections and socket states 5 min read
    • Graceful close: FIN
    • Abort: RST
    • All the states
    • TIME_WAIT: purpose and problems
    • CLOSE_WAIT: always an application bug
  3. 3Kernel queues, backlogs and SYN floods 3 min read
    • Two queues per listening socket
    • SYN floods
  4. 4Reading the handshake in packet captures 3 min read
    • tcpdump flag notation
    • Measuring handshake cost with curl
  5. 5Latency: handshakes, TLS and connection reuse 3 min read
    • Counting round trips
    • Idle timeout mismatches: the 502 classic
  6. 6Troubleshooting scenarios 3 min read
    • "Connection timed out" for clients, server looks fine
    • Clients see resets when pods terminate
    • Health checks pass, users fail
    • Thousands of TIME_WAIT on a reverse proxy
    • NAT gateway connection failures under load
    • "Cannot assign requested address"
    • Retransmissions are high on a link
  7. 7IP TTL, traceroute and MSS clamping 3 min read
    • IP TTL (not DNS TTL)
    • How traceroute works
    • MSS clamping and PMTUD
  8. 8Interview questions and model answers 4 min read
    • "Explain the TCP three-way handshake."
    • "What's the difference between FIN and RST?"
    • "Your server has thousands of sockets in CLOSE_WAIT. What does it mean?"
    • "Lots of TIME_WAIT on a client. Is that bad?"
    • "Clients get connection timeouts but the server shows no SYNs in tcpdump. Where's the problem?"
    • "What is a SYN flood and how do you defend against it?"
    • "Why does TLS add latency, and how does HTTP/3 help?"
    • "How does traceroute work?"