Zamil's CSE Directory

computer networking

Routers and NAT: Connecting Local Networks to the Internet

Learn how routers direct network traffic and how Network Address Translation (NAT) allows many devices to share a single public IP address.

#networking#routers#nat#internet
networking, routers, nat, internet guides

In the previous chapter we explored how the web works using HTTP and HTTPS.

But another important question remains:

How do all of the devices in your home connect to the internet at the same time?

Your laptop, phone, game console, smart TV, and many other devices may all be connected to the same network. Yet they are able to communicate with servers across the internet simultaneously.

This is made possible by two key networking concepts:

  • Routers
  • Network Address Translation (NAT)

Routers direct traffic between networks, while NAT allows many devices to share a single public internet address.

Together, they make modern home and office networking possible.


What is a Router?

A router is a network device responsible for directing packets between different networks.

Routers examine incoming packets and decide where they should go next.

Earlier we learned that packets often travel through multiple routers as they move across the internet.

For example:

graph LR
  A[Your Computer]
  B[Home Router]
  C[ISP Network]
  D[Internet Routers]
  E[Web Server]

  A --> B --> C --> D --> E

Each router along the path examines the packet’s destination IP address and forwards it toward the appropriate network.

Without routers, separate networks would not be able to communicate with each other.


Your Home Router

Most homes and small offices contain a home router.

This device connects two different networks:

  • your local network (home devices)
  • the internet

A simplified view looks like this:

graph LR
  A[Laptop]
  B[Phone]
  C[Smart TV]
  D[Home Router]
  E[Internet]

  A --> D
  B --> D
  C --> D
  D --> E

Your router acts as the gateway between your private network and the wider internet.

All outgoing internet traffic from your devices passes through this router.


Private vs Public IP Addresses

To understand NAT, we first need to understand two types of IP addresses.

Public IP Addresses

A public IP address is globally unique and reachable across the internet.

Web servers and internet-facing systems typically use public IP addresses.

Example:

203.0.113.42

This address can be reached by devices anywhere on the internet.


Private IP Addresses

Devices inside home or office networks typically use private IP addresses.

Common private address ranges include:

192.168.0.0 – 192.168.255.255
10.0.0.0 – 10.255.255.255
172.16.0.0 – 172.31.255.255

For example, your laptop might have an address like:

192.168.1.25

Private addresses are not accessible directly from the internet.

They only work inside local networks.


The Address Shortage Problem

The internet uses IPv4 addresses, which are limited in number.

There are roughly 4.3 billion possible IPv4 addresses.

But the world contains far more internet-connected devices than that.

If every device required its own public address, we would quickly run out.

This is where Network Address Translation (NAT) becomes important.


What is NAT?

Network Address Translation (NAT) is a technique used by routers to allow many devices to share a single public IP address.

When a device inside your network sends a packet to the internet:

  1. the packet first reaches the router
  2. the router replaces the private source address with its public IP
  3. the packet is forwarded to the internet

When the response returns:

  1. the router receives the packet
  2. it looks up which device originally made the request
  3. the packet is forwarded back to the correct device

From the perspective of the outside internet, all requests appear to come from the router.


How NAT Works in Practice

Imagine a home network with several devices:

graph LR
  A[Laptop 192.168.1.10]
  B[Phone 192.168.1.11]
  C[Tablet 192.168.1.12]
  D[Router Public IP 198.51.100.5]
  E[Web Server]

  A --> D
  B --> D
  C --> D
  D --> E

Each device uses a private IP address.

When one device requests a webpage:

  1. the router records the request
  2. the router replaces the source IP with its public address
  3. the server sends the response back to the router
  4. the router forwards the response to the correct device

This translation allows many devices to share one public address safely.


NAT and Port Mapping

To keep track of multiple connections, NAT often uses port numbers.

When a device sends a request, the router records:

  • the device’s private IP
  • the device’s source port
  • the router’s public port used for the request

Example:

192.168.1.10:53021 → 198.51.100.5:45001

The router keeps a translation table that maps internal connections to external ones.

This allows it to route incoming responses back to the correct device.


Benefits of NAT

NAT provides several important advantages.

Address Conservation

NAT allows many devices to share a single public IP address.

This greatly reduces the demand for IPv4 addresses.


Basic Security

Because internal devices are not directly reachable from the internet, NAT also provides a basic level of protection.

External systems cannot easily initiate connections to devices inside the network.


Simpler Network Management

Private networks can freely reuse the same private IP ranges without conflicting with other networks.

This simplifies network configuration in homes and organizations.


Limitations of NAT

Although NAT is widely used, it also introduces some challenges.

For example:

  • direct connections between devices can be harder to establish
  • some applications require special configuration (such as port forwarding)
  • NAT can add complexity to certain networking protocols

Modern networking systems sometimes use techniques to work around these limitations.


Key Ideas to Remember

Routers and NAT play a central role in modern networking.

Important ideas from this chapter include:

  • A router directs packets between networks.
  • Home routers connect local networks to the internet.
  • Devices inside local networks usually use private IP addresses.
  • The internet uses public IP addresses for globally reachable systems.
  • NAT allows many devices to share a single public IP address.
  • NAT works by translating private addresses into a router’s public address.

Together, routers and NAT allow billions of devices to connect to the internet using limited address space.


What Comes Next

So far we have explored the foundations of networking — packets, protocols, routing, and internet communication.

However, modern systems are often much more complex than a single computer connecting to a server.

Large-scale applications rely on technologies designed to manage massive distributed systems.

In the next chapter we will explore modern networking systems, including the tools and architectures that power today’s large-scale internet services.