Skip to content

Linux Foundations / Follow traffic and find the fault ยท Step 17 of 46

Lesson: What is a router?Switch
โ† Linux Foundations

Network operations

What is a router?

Learn what a router does, how it differs from a switch, and how a Linux machine can act as one.

A router is a device that moves packets from one network to another. Your laptop can talk directly to the printer on the same home network, but to reach a web server elsewhere it needs help. That help is the router.

Networks and their edges

A network, in this sense, is a block of IP addresses that can reach each other directly, such as 192.168.1.0/24. The /24 says that the first 24 bits (here 192.168.1) identify the network. A router has an interface in two or more networks and passes traffic between them.

Routers and switches

A switch forwards Ethernet frames inside one local network, using MAC addresses. A router forwards IP packets between networks, using IP addresses. The box your provider gave you is usually several things in one: a router, a switch, a wireless access point and often a firewall.

The default gateway

When a host wants to reach an address outside its own network, it hands the packet to a router called the default gateway. On Linux you can see it with:

$ ip route show default
default via 192.168.1.1 dev eth0

Read this as: for anything without a more specific route, send it to 192.168.1.1 through the interface eth0. Interface names differ between systems, so you may see enp3s0 or wlan0 instead.

Linux as a router

A Linux machine with two network interfaces can route too. By default most systems do not forward IPv4 packets that are meant for someone else. A kernel setting controls this:

$ sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0

0 means off. An administrator can turn it on until the next reboot with:

$ sudo sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1

Some tools, such as container engines, switch it on for you, so your machine may already show 1. To keep the setting after a reboot, put it in a file under /etc/sysctl.d/. Forwarding is only one part of the job: the other machines also need routes that point at this one, and any firewall must allow the traffic.

If the addressing ideas are new to you, the networking basics guide covers them.

Try it

Run ip -brief address to list your interfaces and addresses. Then run ip route show default and find your gateway. Is the gateway inside the same network as your own address? Finally, check sysctl net.ipv4.ip_forward and decide whether your machine is currently forwarding.

Check your understanding

What is a router?

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.