Networking is a stack of small jobs. One layer moves bits over a wire, another finds the next hop, another makes sure nothing is lost, another speaks HTTP. Each layer only talks to the layer directly above and below it, so you can swap Wi-Fi for fiber without rewriting your browser. Two models describe the stack: OSI, a seven-layer teaching model, and TCP/IP, the four-layer model the internet actually runs on.
The seven OSI layers
| # | Layer | Job | Unit of data | Examples |
|---|---|---|---|---|
| 7 | Application | The service the user or program wants | Data | HTTP, DNS, SMTP, SSH |
| 6 | Presentation | Format, encoding, encryption, compression | Data | TLS, UTF-8, JSON, gzip |
| 5 | Session | Opening, keeping and closing conversations | Data | RPC sessions, TLS session resumption |
| 4 | Transport | Process to process delivery, ports, reliability | Segment (TCP) / datagram (UDP) | TCP, UDP, QUIC |
| 3 | Network | Logical addressing and routing between networks | Packet | IPv4, IPv6, ICMP |
| 2 | Data link | Delivery on one local link, MAC addresses | Frame | Ethernet, Wi-Fi (802.11), PPP |
| 1 | Physical | Bits as signals on a medium | Bit | Copper, fiber, radio, connectors |
A classic mnemonic from the bottom up is “Please Do Not Throw Sausage Pizza Away”.
The four TCP/IP layers
| TCP/IP layer | Maps to OSI | Main protocols |
|---|---|---|
| Application | 5, 6 and 7 | HTTP, DNS, TLS, SSH, SMTP |
| Transport | 4 | TCP, UDP, QUIC |
| Internet | 3 | IP, ICMP |
| Link (network access) | 1 and 2 | Ethernet, Wi-Fi, ARP |
TCP/IP came from working code; OSI came from a standards committee. TCP/IP won the internet, but engineers still use OSI numbers every day: “it’s a layer 2 problem” means a switching or VLAN issue, and a “layer 7 load balancer” reads HTTP.
Where TLS lives: TLS encrypts data (a presentation job) and manages sessions (a session job), but runs as part of the application in TCP/IP terms. Do not lose sleep over exact placement; know what the protocol does.
Encapsulation
On the way down, each layer wraps what it got from above in its own header (and Ethernet adds a trailer too). On the way up, the receiver peels the wrappers off in reverse order.
Application [ HTTP request: GET /tutorials/ ... ]
Transport [ TCP header | HTTP request ] -> segment
Network [ IP header | TCP header | HTTP request ] -> packet
Data link [ Eth header | IP | TCP | HTTP request | Eth trailer ] -> frame
Physical 0110100101110101... -> bits
Each header answers one question:
- TCP header: which process? (source and destination ports) and which bytes? (sequence numbers)
- IP header: which host? (source and destination IP addresses)
- Ethernet header: which device on this link? (source and destination MAC addresses)
A key detail: the IP addresses stay the same from end to end (unless NAT rewrites them), but the MAC addresses change at every router, because each hop is a new local link.
Watch the layers in a capture
tcpdump -e prints the layer 2 header, and -nn keeps numbers instead of names:
$ sudo tcpdump -i enp3s0 -e -nn -c 1 'tcp port 443'
14:02:11.318842 3c:2c:4a:1b:9c:10 > 00:1a:2b:3c:4d:5e, ethertype IPv4 (0x0800), length 74:
192.168.1.42.51644 > 203.0.113.10.443: Flags [S], seq 2849021137, win 64240,
options [mss 1460,sackOK,TS val 391724 ecr 0,nop,wscale 7], length 0
Read it layer by layer:
| Part of the line | Layer | Meaning |
|---|---|---|
3c:2c:… > 00:1a:… | 2 | From my NIC to the router’s MAC |
ethertype IPv4 | 2 to 3 | The payload is an IPv4 packet |
192.168.1.42 > 203.0.113.10 | 3 | From my host to the web server |
.51644 > .443 | 4 | From a random client port to HTTPS |
Flags [S] | 4 | A TCP SYN, the first step of a connection |
For a full layer-by-layer view, save the capture with -w web.pcap and open it in Wireshark, which shows each header as an expandable tree.
Troubleshooting with layers
The layers give you a checklist. Work bottom up and stop at the first layer that fails:
| Layer | Question | Command |
|---|---|---|
| 1 | Is there a link? | ip link, ethtool enp3s0 |
| 2 | Can I reach the gateway’s MAC? | ip neigh |
| 3 | Do I have an address and a route? Can I reach the gateway, then the internet? | ip addr, ip route, ping |
| 4 | Is the port open? | ss -tln, nc -zv host 443 |
| 7 | Does the application answer correctly? | curl -v https://host/ |
DNS sits at layer 7, but it breaks so often that it gets its own step: if ping 1.1.1.1 works but ping example.com does not, it is DNS. Lesson 24 turns this into a full playbook.
Exercise
Which header changes at every router hop on the way to a website (without NAT)?
Show answer
Answer: C. Each hop is a new local link, so the router writes new layer 2 addresses. IP addresses and ports stay the same end to end unless NAT rewrites them.
Exercise
A colleague says “the firewall drops it at layer 4”. What is the firewall most likely matching on?
Show answer
Answer: A. Layer 4 is the transport layer, so the rule matches ports (and protocol). Layer 2 would be MAC addresses, layer 7 would be application content.
Exercise
What do we call the unit of data at the network layer?
Show answer
Answer: D. Bits at layer 1, frames at layer 2, packets at layer 3, segments (TCP) or datagrams (UDP) at layer 4.
Key takeaways
- OSI has seven layers; TCP/IP has four and runs the internet.
- Each layer adds a header on the way down and removes it on the way up.
- Ports pick the process, IP picks the host, MAC picks the next device on the link.
- MAC addresses change every hop; IP addresses do not (unless NAT).
- Troubleshoot bottom up: link, local delivery, routing, ports, application.
FAQ
Why learn OSI if the internet uses TCP/IP?
Because the vocabulary is everywhere. Vendors, job interviews and incident channels say “layer 2”, “layer 4” and “layer 7” constantly, and those numbers come from OSI.
Which layer does ARP belong to?
ARP maps IP addresses (layer 3) to MAC addresses (layer 2), so it sits between them. Most people call it a layer 2 protocol because its messages travel in Ethernet frames and never cross a router.
Is HTTPS a different layer from HTTP?
No. HTTPS is HTTP carried inside TLS. HTTP is still the application protocol; TLS adds encryption beneath it.
Do I need Wireshark if I have tcpdump?
tcpdump is enough to capture and filter on a server. Wireshark is far nicer for reading what you captured, so a common habit is to capture with tcpdump -w on the server and open the file in Wireshark on your laptop.