Lesson 1 of 14 · 11 min · How networks work
Network layers and what happens when you open a website
What you will learn
- Why networking is split into layers, and what the OSI and TCP/IP models are
- What each layer does and which protocols and devices belong to it
- How a message is wrapped (encapsulated) as it moves down the layers
- The complete journey of "type a URL, press Enter", which is a favourite interview question
In simple words
Sending data across a network is like sending a parcel. You put the item in a box, write an address on it, a courier carries it, and the other side unpacks it. Networks do the same in layers, each with one job. This lesson walks through those layers and the full journey of opening a website.
Words to know
- Protocol: an agreed set of rules for how two computers talk.
- Packet: a small piece of data with an address label. Big messages are cut into many packets.
- Layer: one stage of the journey, with one job, such as addressing or reliable delivery.
- Router: a device that passes packets on towards their destination network.
Why layers
Sending a web page across the world involves cables, radio, addresses, routes, reliable delivery, encryption and the web itself. No one program does all of it. Networking is therefore built as a stack of layers: each layer solves one problem, uses the services of the layer below, and offers a simple service to the layer above. A web server does not care whether the packets travel over fibre, Wi-Fi or a 4G network; the lower layers hide that. Layers can be changed independently, which is why the same HTTP works over every kind of link.
The two models
The OSI model has seven layers and is used as a vocabulary ("a layer 4 load balancer", "a layer 7 firewall"). The TCP/IP model has four layers and describes what the internet actually uses. They line up like this:
| OSI layer | TCP/IP layer | What it does | Examples |
|---|---|---|---|
| 7 Application | Application | the protocols programs speak | HTTP, DNS, SSH, SMTP, TLS handshake |
| 6 Presentation | data format, encryption | (TLS, in practice handled by the application) | |
| 5 Session | managing a conversation | (in practice handled by the application) | |
| 4 Transport | Transport | delivery between programs; ports; reliability | TCP, UDP |
| 3 Network | Internet | addressing and routing between networks | IP, ICMP, routers |
| 2 Data link | Link | delivery between neighbours on the same network | Ethernet, Wi-Fi, MAC addresses, switches |
| 1 Physical | signals on the wire or air | cables, fibre, radio |
When engineers talk about "layer 4" they mean TCP/UDP and ports; "layer 7" means the application protocol such as HTTP; "layer 3" means IP addresses and routing; "layer 2" means MAC addresses and switches. A layer 4 load balancer forwards connections by IP and port; a layer 7 load balancer reads the HTTP request and can route by URL or header.
What each layer is responsible for
- Link (L2): moves a frame between two devices on the same local network, using MAC addresses (hardware addresses like
02:42:ac:11:00:02). A switch forwards frames inside a network.ip linkshows your interfaces and MACs. - Internet / Network (L3): gives every machine an IP address and moves packets across many networks. A router connects networks and decides the next hop for each packet using its routing table. IP is "best effort": packets can be lost, duplicated or reordered.
ICMP(used byping) carries control and error messages. - Transport (L4): gets data from a program on one machine to a program on another. It adds port numbers to identify the program. TCP adds reliability: a connection, acknowledgements, retransmission and ordering. UDP is a bare, fast "send this datagram", with no guarantees.
- Application (L7): the language of the service: HTTP for the web, DNS for names, SSH for remote shells.
Encapsulation: wrapping on the way down
When an application sends data, each layer adds its own header (the information it needs) in front of the payload:
Application: [ HTTP request: GET /index.html ]
Transport: [ TCP hdr: src port 52314, dst port 80 ][ HTTP request ]
Network: [ IP hdr: src 192.168.1.10, dst 93.184.216.34 ][ TCP ... ][ HTTP ]
Link: [ Ethernet: src MAC, dst MAC (next hop) ][ IP ... ][ TCP ... ][ HTTP ... ][ trailer ]
On the receiving side each layer removes its header and passes the rest up. Two details matter in practice:
- The IP addresses stay the same from source to destination, but the MAC addresses change at every hop: they only mean "next device on this link".
- Each link has a maximum frame size, the MTU (1500 bytes on typical Ethernet). Larger data is split into several packets; mismatched MTUs cause odd "small requests work, large ones hang" problems, common with VPNs and tunnels.
The journey: you open https://example.com
This is the complete answer to the classic interview question. Each step uses a different layer:
- The browser parses the URL: scheme
https, hostexample.com, port 443 (the default for https), path/. - DNS lookup (application layer, usually UDP port 53): the browser asks the operating system, which checks caches and
/etc/hosts, then asks the configured DNS resolver. The resolver finds the IP address, say93.184.216.34. - Routing decision (network layer): the OS checks whether that IP is on the local network. It is not, so the packet goes to the default gateway (your router). The OS needs the gateway's MAC address, which it gets with ARP ("who has 192.168.1.1?") if not cached.
- Crossing the internet: your router uses NAT (explained in the next lesson) and forwards the packet; each router along the path looks at the destination IP and forwards it one hop at a time.
- TCP handshake (transport layer): the client sends
SYN, the server answersSYN-ACK, the client repliesACK. A connection to port 443 exists. - TLS handshake: client and server agree on a protocol version and ciphers, the server proves its identity with a certificate that the browser verifies, and they derive keys. Everything after this is encrypted.
- HTTP request: the browser sends
GET / HTTP/1.1(or the HTTP/2 equivalent) with headers such asHost: example.com. - The server responds: a status line (
200 OK), headers and the HTML body. A site behind a CDN or load balancer is usually answered by a nearby edge server or a reverse proxy that talks to a backend. - Rendering: the browser parses the HTML and makes further requests for CSS, scripts and images, often reusing the same connection.
- Closing: the connection stays open for reuse for a while (keep-alive), then closes with
FINpackets.
You can observe several of these steps yourself:
curl -sv https://example.com/ -o /dev/null 2>&1 | grep -E "^\* Trying|Connected|TLS|^> GET|^< HTTP"
shows the address tried, the connection, the TLS version, the request line and the response status.
Looking at your own layers
ip -br link # layer 2: interfaces and MAC addresses
ip -br addr # layer 3: IP addresses
ip route # layer 3: where packets go next
ss -tan # layer 4: TCP connections and ports
Real output of the first two on a container:
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0@if84 UP 172.17.0.2/16
Common mistakes
- Mixing up layers when describing a problem. "The firewall blocks the website" could be L3 (IP), L4 (port) or L7 (URL rules). Name the layer.
- Thinking the MAC address travels end to end. It changes at every hop.
- Thinking IP guarantees delivery. Reliability comes from TCP or from the application.
- Believing HTTPS is a different network. It is HTTP inside TLS inside TCP.
Try it yourself
- Use
ip -br link,ip -br addr,ip routeandss -tanon your machine and label each with its layer. - Run
curl -v http://example.com/ -o /dev/nulland mark in the output the DNS result, the TCP connection, the request and the response. - Write the ten-step journey above in your own words without looking, then compare.
- Explain to a friend what "a layer 7 load balancer" can do that "a layer 4 load balancer" cannot.
Check yourself
Which layer do TCP and UDP belong to, and what do they add to IP?
The transport layer. They add port numbers, which identify the program on a host. TCP also adds connections and reliable ordered delivery; UDP adds almost nothing.
Does the destination MAC address stay the same from your laptop to a web server on another continent?
No. MAC addresses are only meaningful on one link, so they are rewritten at every hop. The IP addresses stay the same (apart from NAT).
What does a switch do compared with a router?
A switch forwards frames inside one network using MAC addresses (layer 2). A router connects different networks and forwards packets using IP addresses (layer 3).
In which order do DNS, the TCP handshake and the TLS handshake happen?
DNS first (to get the IP), then the TCP handshake, then the TLS handshake, then the HTTP request.
Interview questions
What happens when you type a URL in the browser and press Enter?
DNS resolution, route to the gateway (ARP for its MAC), packets forwarded hop by hop, TCP three-way handshake, TLS handshake, HTTP request, server response (possibly via CDN or a reverse proxy to a backend), rendering and further requests, connection reuse or close.
What is the difference between the OSI and TCP/IP models?
OSI has seven conceptual layers and is a teaching and vocabulary model. TCP/IP has four layers (link, internet, transport, application) and describes the protocols actually used on the internet; OSI's top three layers are one application layer in TCP/IP.
What is MTU and why can it cause problems?
The largest packet a link can carry. If a packet is bigger than a link's MTU it must be fragmented or dropped; broken path MTU discovery makes small requests work while large ones hang, typically over VPNs or tunnels.
Summary
- Layers split networking into manageable jobs: link (frames, MAC), network (IP, routing), transport (TCP/UDP, ports), application (HTTP, DNS, SSH).
- Data is wrapped with a header at each layer on the way down; MAC addresses change per hop, IP addresses do not.
- "Layer 4" means ports and TCP/UDP; "layer 7" means the application protocol.
- Opening a website means DNS, routing, TCP handshake, TLS handshake, HTTP, response.
Going deeper
- RFC 1122: Requirements for Internet Hosts, Communication Layers: the standard describing the TCP/IP layering.
- RFC 9293: Transmission Control Protocol: the current TCP specification.
- MDN: How the web works / a typical HTTP session: the HTTP side of the journey.
See how much of this you can explain
A free mock interview asks you new questions on Networking for DevOps for DevOps Engineer and scores every answer, with what to fix.