DEVOPS, SRE AND CLOUD · PART 2 · NETWORKING FOR DEVOPS · BASICS · 28 OF 187

OSI and TCP/IP Layers Explained for DevOps

The mental model every network debug starts from: the OSI and TCP/IP layers, what each layer adds (Ethernet frame, IP packet, TCP segment, HTTP message), encapsulation watched with tcpdump as our 14-byte "hello shoprung" message left the machine, which tool and which failure belong to which layer, and how to say "layer 4 vs layer 7" in an interview.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Link
  • IP
  • TCP/UDP
  • application
  • headers wrap data
  • MTU limits size
  • errors name the layer

Notes

Four layers

The internet works in layers. The application layer decides what to say, for example an HTTP request. The transport layer decides which program on the machine gets it, using port numbers, and whether delivery is reliable, TCP, or best effort, UDP. The network layer, IP, gets the packet to the right machine, even across many networks. The link layer moves it one hop, across one cable or Wi-Fi network, using MAC addresses.

TCP/IP model
  • Application what to say HTTP, DNS, SSH, TLS
  • Transport which program, reliable or not: TCP, UDP
  • Network which machine, across networks: IP
  • Link the next hop on this cable: Ethernet, MAC

OSI numbers

You will also hear the older OSI model, with seven layers. Real networks follow the four layer TCP IP model, but the OSI numbers stuck as vocabulary. Layer seven is the application, layer four is transport, layer three is IP, layer two is Ethernet. So a layer four load balancer looks at IP addresses and ports, and a layer seven load balancer reads the HTTP request itself. Layers five and six barely exist as separate things.

OSI 7 layers
  • 7 Application HTTP, gRPC
  • 6 Presentation (TLS, encoding)
  • 5 Session
  • 4 Transport TCP, UDP
  • 3 Network IP, ICMP
  • 2 Data link Ethernet, ARP
  • 1 Physical cables, radio
  • real stacks merge 5, 6, 7

Encapsulation

When a program sends data, each layer wraps it in its own header, like envelopes inside envelopes. TCP adds ports and sequence numbers and the result is a segment. IP adds source and destination addresses, making a packet. Ethernet adds MAC addresses, making a frame, which goes on the wire. The other machine unwraps them in reverse: Ethernet, then IP, then TCP, and finally hands the data to the right program.

One message, wrapped
  • data "hello shoprung"
  • + TCP hdr ports, seq, flags = segment
  • + IP hdr source, dest IP = packet
  • + Eth hdr MAC addresses = frame
  • the receiver unwraps in reverse order

Watch it

Let's see it. We ran a tiny TCP echo server on port nine thousand and captured the traffic with tcpdump. Our client sent the fourteen bytes hello shoprung. tcpdump printed IP, the network layer, then source and destination addresses with their port numbers, the transport layer, then the TCP flags and a length of fourteen: our data. One line shows three layers wrapped around one message.

tcpdump -i lo port 9000 (ours)
IP 127.0.0.1.51334 >
  127.0.0.1.9000: Flags [P.],
  length 14
"hello shoprung"

IP   = network layer
.51334 .9000 = TCP ports
Flags, length = TCP header
14 = our bytes of data

Size limits

Every header costs bytes, and every link has a maximum packet size, the MTU. Our machine's interface had an MTU of fourteen hundred bytes. An IP version four header is twenty bytes and a basic TCP header is twenty more, so each packet carried at most about thirteen hundred and sixty bytes of data. A larger HTTP response is split into many packets and reassembled by TCP. Episode seventeen shows what happens when MTUs disagree.

Our interface
$ ip link show eth0
  mtu 1400
IPv4 header   20 bytes
TCP header    20 bytes
  (+ options)
room for data per packet
  = MTU - 40 = 1360 here
bigger messages are split
  into many packets

Debug by layer

The layers give you a checklist. At layer seven, curl, dig and openssl test the application, DNS and TLS. At layer four, ss and netcat show whether the port is open and something is listening. At layer three, ip route, ping and mtr test whether the address is reachable at all. At layer two, ip link and ip neigh show whether the link is up and ARP works. Work through them in order and you stop guessing.

Which tool checks what
  • L7 curl -v, dig, openssl s_client: app, DNS, TLS
  • L4 ss -tlnp, nc -zv: is the port open and listening?
  • L3 ip route, ping, mtr: can I reach that IP?
  • L2 ip link, ip neigh: is the link up, is ARP working?
  • go bottom-up or top-down

Map the error

Most error messages tell you the layer. Could not resolve host is DNS. Connection refused means the machine answered but nothing listens on that port, layer four. Connection timed out means packets got no answer at all, usually a firewall or routing problem. Certificate verify failed is TLS. And a five oh two from a proxy means the proxy worked but could not reach the app behind it. Every one of these appears in this part with a real example.

Error → layer
  • "could not resolve host" → DNS (application)
  • "connection refused" → L4: nothing listening
  • "connection timed out" → L3/L4: dropped, firewall
  • "certificate verify failed" → TLS
  • "502 Bad Gateway" → L7 proxy could not reach the app

Common mistakes

Mistakes. Saying the network is down without naming which layer fails. Testing only with ping, when many networks block ping but allow web traffic. Debugging the application code before checking DNS and whether the port is open. Mixing up layer four and layer seven load balancing. And forgetting that packet size limits exist.

Avoid these
  • "the network is down" with no layer named
  • testing with ping only (ICMP may be blocked)
  • debugging the app before checking DNS and the port
  • confusing L4 and L7 load balancing
  • ignoring the MTU

Say it like this

In the interview: real networks use four layers, link, internet, transport and application, and each wraps the data above it in its own header. The OSI numbers are the shared vocabulary: layer three is IP, layer four is TCP and UDP ports, layer seven is HTTP. Then show you can use it: when something is unreachable, I check DNS, the route, whether the port is open, then TLS and the application.

In the interview
  • "Real stacks use four layers: link, IP, TCP or UDP, and the application. Each wraps the data above in a header. OSI
  • numbers name them: L3 is IP, L4
  • is ports, L7 is HTTP. When I
  • debug, I check DNS, then the route, the port, then TLS."

Interview questions

Q1What are the layers of the TCP/IP model?

Link (Ethernet, Wi-Fi), Internet (IP), Transport (TCP, UDP) and Application (HTTP, DNS, SSH). OSI splits these into seven numbered layers.

Q2What is encapsulation?

Each layer wraps the data from the layer above in its own header: TCP makes a segment, IP a packet, Ethernet a frame; the receiver unwraps them in reverse.

Q3What is the difference between a layer 4 and a layer 7 load balancer?

Layer 4 routes by IP address and port without reading the content; layer 7 reads the HTTP request (host, path, headers) and can route, rewrite or cache.

Q4What does "connection refused" tell you?

The target machine was reachable and replied with a reset: nothing is listening on that port, or a firewall rejects it actively.

Q5What does "connection timed out" usually mean?

No reply came back at all: packets were dropped by a firewall or security group, or there is no route to the host.

Q6What is the MTU?

The maximum transmission unit: the largest packet a link carries (1500 bytes on typical Ethernet). Larger data is split across packets.

Q7Why is ping not enough to test a service?

Ping uses ICMP, which is often blocked, and only proves layer 3 reachability; test the real port and protocol with nc, curl or openssl.