The OSI Model
By the end of this lesson you will be able to name the seven OSI layers in order, say what each one does, give its data unit and a protocol or device that works there, and use the layers to troubleshoot.
Sending data across a network involves many jobs: turning bits into signals, finding the next device, finding the right program, encrypting, and more. The OSI model splits these jobs into seven layers so that each one can be understood, built and fixed on its own.
Why use layers at all?
Imagine Kabir in Jaipur sending a birthday gift to Ananya in Kochi by courier. Kabir wraps the gift and writes the address. The courier office puts it in a bag for Kerala. A truck carries the bag to the airport. A plane flies it south. Nobody in this chain needs to know everyone else's job. The pilot does not care what is in the box. Kabir does not care which plane is used.
Network layers work the same way:
- Each layer does one job and offers a service to the layer above it.
- Each layer talks to its peer layer on the other device using a protocol.
- You can change one layer without touching the others. Your browser works the same over Wi-Fi, Ethernet or 5G, because only the lower layers change.
OSI stands for Open Systems Interconnection. It was published by ISO (the International Organization for Standardization) as a reference model. The internet itself runs on the simpler TCP/IP model, which you will meet in the next lesson. But OSI is still the common language for talking about networks, and exams love it.
The seven layers
Layers are numbered from the bottom. A memory trick for 1 to 7: Please Do Not Take Samosa Pakoras Away.
| No. | Layer | Main job | Data unit | Examples |
|---|---|---|---|---|
| 7 | Application | Services for user programs | Data | HTTP, DNS, SMTP, FTP |
| 6 | Presentation | Format, encode, compress, encrypt | Data | Character encodings (UTF-8), TLS encryption |
| 5 | Session | Start, manage and end conversations | Data | Session set-up and checkpoints |
| 4 | Transport | End-to-end delivery between programs | Segment (TCP) or datagram (UDP) | TCP, UDP |
| 3 | Network | Logical addressing and routing across networks | Packet | IP, ICMP, routers |
| 2 | Data link | Delivery to the next device on the same link; framing, MAC addresses, error detection | Frame | Ethernet, Wi-Fi (802.11), switches |
| 1 | Physical | Sending raw bits as signals | Bit | Cables, fibre, radio, hubs, repeaters |
The data unit at each layer has a general name: PDU (protocol data unit). Knowing the PDU names (bits, frames, packets, segments) is essential; questions use them all the time.
Each layer in plain words
Let us walk through the courier story layer by layer.
- Physical (layer 1). The road, the truck and the plane. It moves bits as electrical pulses, light flashes or radio waves. It cares about voltage, timing and connectors, not meaning.
- Data link (layer 2). The courier van between two neighbouring offices. It delivers a frame to the next device on the same link using MAC addresses, and checks whether the frame was damaged on the way.
- Network (layer 3). The address and the route planning. It gives every host a logical IP address and chooses the path across many networks. Routers live here.
- Transport (layer 4). Making sure the whole parcel arrives complete and in order, and reaches the right person in the house. It uses port numbers to reach the right program, and TCP adds reliability.
- Session (layer 5). Keeping a conversation going: starting it, keeping track, and ending it neatly.
- Presentation (layer 6). Making sure both sides understand the data format: character encoding, compression and encryption.
- Application (layer 7). The services that programs use, such as web browsing (HTTP) or email (SMTP). The browser itself is not layer 7; the protocol it uses is.
Walking a message down and up
When you send data, it goes down the layers on your device, across the medium, and up the layers on the receiver. Each layer on the way down adds its own control information. This is called encapsulation, and the next lesson covers it in detail.
layers = ["Application", "Presentation", "Session", "Transport",
"Network", "Data link", "Physical"]
print("Sender (going down):")
for number, name in zip(range(7, 0, -1), layers):
print(f" {number} {name}")
print("Receiver (going up):")
for number, name in zip(range(1, 8), reversed(layers)):
print(f" {number} {name}")Sender (going down): 7 Application 6 Presentation 5 Session 4 Transport 3 Network 2 Data link 1 Physical Receiver (going up): 1 Physical 2 Data link 3 Network 4 Transport 5 Session 6 Presentation 7 Application
Routers in the middle only go up to layer 3 and back down. Switches only go up to layer 2. They do not need to open the parcel further.
Which layer is it?
Exam questions often name a device or a protocol and ask for its layer. A dictionary makes a handy revision tool.
layer_of = {
"hub": 1, "repeater": 1, "fibre cable": 1,
"switch": 2, "bridge": 2, "ethernet": 2, "mac address": 2,
"router": 3, "ip": 3, "icmp": 3,
"tcp": 4, "udp": 4, "port number": 4,
"http": 7, "dns": 7, "smtp": 7,
}
names = {1: "Physical", 2: "Data link", 3: "Network", 4: "Transport", 7: "Application"}
for item in ["switch", "tcp", "router", "dns", "hub"]:
n = layer_of[item]
print(f"{item:>8} -> layer {n} ({names[n]})")switch -> layer 2 (Data link) tcp -> layer 4 (Transport) router -> layer 3 (Network) dns -> layer 7 (Application) hub -> layer 1 (Physical)
Using layers to troubleshoot
When "the internet is not working", network engineers check from the bottom up. Each step rules out one layer.
| Step | Check | Layer |
|---|---|---|
| 1 | Is the cable plugged in, or is Wi-Fi connected? | Physical, data link |
| 2 | Does the device have an IP address and gateway? | Network |
| 3 | Can you ping the gateway, then an outside IP address? | Network |
| 4 | Do names work, for example nslookup example.com? | Application (DNS) |
| 5 | Does the website itself open? | Application |
Priya's laptop in her Pune hostel can ping an outside IP address but cannot open any site by name. Steps 1 to 3 pass, step 4 fails: the problem is DNS, not the Wi-Fi. Thinking in layers saved her from restarting everything. You will use these tools in ICMP & Network Tools.
Common mistakes
- Mixing up the order. Number from the bottom: layer 1 is Physical, layer 7 is Application.
- Saying the browser is the application layer. HTTP, the protocol, works at layer 7. The browser is a program that uses it.
- Mixing up PDUs. Bits at layer 1, frames at layer 2, packets at layer 3, segments at layer 4.
- Putting switches at layer 3 by default. Ordinary switches are layer 2. Only layer 3 switches also route.
Key points
- The OSI model splits networking into seven layers, each with one job and a service for the layer above.
- From the bottom: Physical, Data link, Network, Transport, Session, Presentation, Application.
- PDUs: bits (1), frames (2), packets (3), segments or datagrams (4), data (5 to 7).
- Hubs work at layer 1, switches at layer 2, routers at layer 3.
- Data goes down the layers on the sender and up on the receiver.
- Troubleshoot from the bottom layer upwards.
Exercise
Extend the "Which layer is it?" program. Add "access point": 2, "wifi": 2, "ftp": 7 and "ipv6": 3. Then loop through a list of six items of your choice and count how many belong to each layer, printing the counts in layer order.
Show solution
from collections import Counter
layer_of = {
"hub": 1, "switch": 2, "access point": 2, "wifi": 2,
"router": 3, "ip": 3, "ipv6": 3, "tcp": 4, "udp": 4,
"http": 7, "dns": 7, "ftp": 7,
}
items = ["wifi", "ipv6", "ftp", "switch", "udp", "router"]
counts = Counter(layer_of[i] for i in items)
for layer in sorted(counts):
print(f"Layer {layer}: {counts[layer]}")Layer 2: 2 Layer 3: 2 Layer 4: 1 Layer 7: 1
Counter counts how many times each layer number appears. Sorting the keys prints the layers from the bottom up.
Quick Quiz
1. Which OSI layer is responsible for routing packets between networks?
The network layer (layer 3) handles IP addressing and routing.
2. What is the data unit at the data link layer called?
Layer 2 PDUs are frames; packets are layer 3 and segments are layer 4.
3. Which layer uses port numbers to deliver data to the correct program?
TCP and UDP at layer 4 use port numbers to reach the right application.
4. A user can ping 8.8.8.8 but cannot open websites by name. Which is the most likely problem?
The network layer works (ping succeeds), so name lookup at the application layer is the likely fault.
Learn Computer Networks with a mentor
Free lessons get you started. Our Computer Science Fundamentals training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
