EthicsComputerCourses
Join training +91 90532 10052

The OSI Model

Computer Networks · Lesson 4 of 32 Beginner 7 min read Updated

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.LayerMain jobData unitExamples
7ApplicationServices for user programsDataHTTP, DNS, SMTP, FTP
6PresentationFormat, encode, compress, encryptDataCharacter encodings (UTF-8), TLS encryption
5SessionStart, manage and end conversationsDataSession set-up and checkpoints
4TransportEnd-to-end delivery between programsSegment (TCP) or datagram (UDP)TCP, UDP
3NetworkLogical addressing and routing across networksPacketIP, ICMP, routers
2Data linkDelivery to the next device on the same link; framing, MAC addresses, error detectionFrameEthernet, Wi-Fi (802.11), switches
1PhysicalSending raw bits as signalsBitCables, 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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Session (layer 5). Keeping a conversation going: starting it, keeping track, and ending it neatly.
  6. Presentation (layer 6). Making sure both sides understand the data format: character encoding, compression and encryption.
  7. 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.

PythonRun it yourself
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}")
Output
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.

PythonRun it yourself
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]})")
Output
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.

StepCheckLayer
1Is the cable plugged in, or is Wi-Fi connected?Physical, data link
2Does the device have an IP address and gateway?Network
3Can you ping the gateway, then an outside IP address?Network
4Do names work, for example nslookup example.com?Application (DNS)
5Does 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
Python
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]}")
Output
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?

2. What is the data unit at the data link layer called?

3. Which layer uses port numbers to deliver data to the correct program?

4. A user can ping 8.8.8.8 but cannot open websites by name. Which is the most likely problem?

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