Lessons

Routing and Switching Basics

Level: Intermediate Module: TCP/IP & Networking 5 min read Lesson 14 of 66

Overview

  • What you’ll learn: How routers and switches operate, the difference between Layer 2 switching and Layer 3 routing, how routing tables work, configuring static routes on Linux, and enabling IP forwarding to use Linux as a router.
  • Prerequisites: Lesson 13 – Socket Programming Fundamentals
  • Estimated reading time: 20 minutes

Introduction

When a packet leaves your computer, how does it know where to go? The answer lies in routing — the process of selecting paths in a network along which to send data. Routers are the devices (or software) that make these forwarding decisions, using routing tables to determine the best next hop for each packet.

At a lower level, switches operate within a single network segment, forwarding frames based on MAC addresses. Understanding both routing and switching is essential for designing, configuring, and troubleshooting networks.

In this lesson, you will learn the fundamentals of Layer 2 switching and Layer 3 routing, explore how Linux maintains and uses routing tables, configure static routes, and set up a Linux machine as a simple router.

Layer 2 Switching vs Layer 3 Routing

Network traffic is forwarded at two primary layers of the OSI model. Switches operate at Layer 2 (Data Link) and forward Ethernet frames based on MAC addresses. They maintain a MAC address table (also called a CAM table) that maps MAC addresses to physical ports. Routers operate at Layer 3 (Network) and forward IP packets based on destination IP addresses using routing tables.

# View the ARP table (Layer 2 — MAC-to-IP mappings)
$ ip neigh show
192.168.1.1 dev ens33 lladdr 00:50:56:c0:00:08 REACHABLE
192.168.1.5 dev ens33 lladdr 00:0c:29:12:34:56 STALE
192.168.1.254 dev ens33 lladdr 00:50:56:f1:ab:cd REACHABLE

# View the routing table (Layer 3 — IP forwarding decisions)
$ ip route show
default via 192.168.1.1 dev ens33 proto dhcp metric 100
192.168.1.0/24 dev ens33 proto kernel scope link src 192.168.1.10 metric 100
10.0.0.0/8 via 192.168.1.254 dev ens33 metric 200

# View bridge (virtual switch) information
$ bridge fdb show
00:0c:29:ab:cd:ef dev ens33 master br0
ff:ff:ff:ff:ff:ff dev ens33 master br0

How Routing Works

When a host needs to send a packet, it first checks whether the destination is on the local network (same subnet). If it is, the packet is delivered directly via Layer 2. If the destination is on a different network, the packet is sent to the default gateway — typically a router — which then forwards it toward the destination.

Routers make forwarding decisions by consulting their routing table. They match the destination IP address against the table entries using longest prefix match — the most specific route wins. If no specific route matches, the default route (0.0.0.0/0) is used.

# Trace the route a packet takes to reach a destination
$ traceroute -n 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  192.168.1.1      1.234 ms   1.123 ms   1.089 ms
 2  10.0.0.1         5.678 ms   5.432 ms   5.321 ms
 3  172.16.0.1      12.345 ms  12.234 ms  12.123 ms
 4  8.8.8.8         15.678 ms  15.432 ms  15.321 ms

# View the routing table in detail
$ ip route show table all | head -20
default via 192.168.1.1 dev ens33 proto dhcp metric 100
192.168.1.0/24 dev ens33 proto kernel scope link src 192.168.1.10

# Check which route a specific destination would use
$ ip route get 10.0.5.100
10.0.5.100 via 192.168.1.254 dev ens33 src 192.168.1.10 uid 1000
    cache

Configuring Static Routes

Static routes are manually configured entries in the routing table. They are useful for small networks, specific traffic paths, or when you need to override dynamic routing decisions.

# Add a static route: traffic for 10.10.0.0/16 goes via 192.168.1.254
$ sudo ip route add 10.10.0.0/16 via 192.168.1.254 dev ens33

# Add a route to a specific host
$ sudo ip route add 203.0.113.50/32 via 192.168.1.1 dev ens33

# Delete a route
$ sudo ip route del 10.10.0.0/16 via 192.168.1.254

# Replace an existing route
$ sudo ip route replace 10.10.0.0/16 via 192.168.1.253 dev ens33

# Verify the routing table
$ ip route show
default via 192.168.1.1 dev ens33 proto dhcp metric 100
10.10.0.0/16 via 192.168.1.254 dev ens33
192.168.1.0/24 dev ens33 proto kernel scope link src 192.168.1.10

# Make static routes persistent with Netplan (Ubuntu)
# Edit /etc/netplan/01-netcfg.yaml:
#   ethernets:
#     ens33:
#       routes:
#         - to: 10.10.0.0/16
#           via: 192.168.1.254

Linux as a Router

Any Linux machine with two or more network interfaces can act as a router by enabling IP forwarding. This tells the kernel to forward packets between interfaces instead of dropping them.

# Check if IP forwarding is enabled (0 = disabled, 1 = enabled)
$ sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0

# Enable IP forwarding temporarily
$ sudo sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1

# Enable IP forwarding permanently
$ echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.d/99-router.conf
$ sudo sysctl --system

# Example: Linux router with two interfaces
# ens33: 192.168.1.10/24 (internal network)
# ens34: 203.0.113.10/24 (external network)
#
# Enable forwarding and NAT:
$ sudo sysctl -w net.ipv4.ip_forward=1
$ sudo iptables -t nat -A POSTROUTING -o ens34 -j MASQUERADE
$ sudo iptables -A FORWARD -i ens33 -o ens34 -j ACCEPT
$ sudo iptables -A FORWARD -i ens34 -o ens33 -m state --state RELATED,ESTABLISHED -j ACCEPT

# Verify forwarding is working
$ cat /proc/sys/net/ipv4/ip_forward
1

Key Takeaways

  • Switches forward frames at Layer 2 using MAC addresses; routers forward packets at Layer 3 using IP addresses and routing tables.
  • The routing table uses longest prefix match to select the best route; if no specific route matches, the default gateway is used.
  • Static routes are configured with ip route add and can be made persistent through Netplan configuration on Ubuntu.
  • Linux can function as a router by enabling IP forwarding (net.ipv4.ip_forward=1) and configuring NAT with iptables.
  • Tools like traceroute, ip route get, and ip neigh show help diagnose routing and switching issues.

What’s Next

In Lesson 15, you will learn Network Configuration with Netplan — Ubuntu’s declarative YAML-based network configuration system for managing interfaces, addresses, routes, and DNS settings.

繁體中文

概述

  • 學習目標:路由器和交換器的運作方式、第 2 層交換與第 3 層路由的區別、路由表的運作方式、在 Linux 上設定靜態路由,以及啟用 IP 轉發以將 Linux 用作路由器。
  • 先決條件:第 13 課 – Socket 程式設計基礎
  • 預計閱讀時間:20 分鐘

簡介

當封包離開您的電腦時,它如何知道要去哪裡?答案在於路由——在網路中選擇傳送資料路徑的過程。路由器是做出這些轉發決策的裝置(或軟體),使用路由表來確定每個封包的最佳下一跳。

在較低層級,交換器在單一網路區段內運作,根據 MAC 位址轉發訊框。了解路由和交換對於設計、設定和排除網路故障都是必不可少的。

在本課程中,您將學習第 2 層交換和第 3 層路由的基礎知識,探索 Linux 如何維護和使用路由表,設定靜態路由,並將 Linux 機器設定為簡單的路由器。

第 2 層交換與第 3 層路由

網路流量在 OSI 模型的兩個主要層級進行轉發。交換器在第 2 層(資料鏈結層)運作,根據 MAC 位址轉發乙太網路訊框。路由器在第 3 層(網路層)運作,使用路由表根據目的地 IP 位址轉發 IP 封包。

路由的運作方式

當主機需要發送封包時,它首先檢查目的地是否在本地網路上(同一子網)。如果是,封包透過第 2 層直接交付。如果目的地在不同的網路上,封包會發送到預設閘道——通常是路由器——然後路由器將其轉發到目的地。

路由器透過查詢路由表來做出轉發決策。它們使用最長前綴匹配將目的地 IP 位址與表中的條目進行匹配——最具體的路由優先。如果沒有特定路由匹配,則使用預設路由(0.0.0.0/0)。

設定靜態路由

靜態路由是路由表中手動設定的條目。它們對於小型網路、特定的流量路徑,或當您需要覆蓋動態路由決策時非常有用。

Linux 作為路由器

任何具有兩個或更多網路介面的 Linux 機器都可以透過啟用 IP 轉發來充當路由器。這告訴核心在介面之間轉發封包,而不是丟棄它們。

重要觀念

  • 交換器在第 2 層使用 MAC 位址轉發訊框;路由器在第 3 層使用 IP 位址和路由表轉發封包。
  • 路由表使用最長前綴匹配來選擇最佳路由;如果沒有特定路由匹配,則使用預設閘道。
  • 靜態路由使用 ip route add 設定,可以透過 Ubuntu 上的 Netplan 設定使其持久化。
  • Linux 可以透過啟用 IP 轉發(net.ipv4.ip_forward=1)並使用 iptables 設定 NAT 來充當路由器。
  • tracerouteip route getip neigh show 等工具有助於診斷路由和交換問題。

下一步

在第 15 課中,您將學習使用 Netplan 進行網路設定——Ubuntu 基於 YAML 的宣告式網路設定系統,用於管理介面、位址、路由和 DNS 設定。

日本語

概要

  • 学習内容:ルーターとスイッチの動作方法、レイヤー2スイッチングとレイヤー3ルーティングの違い、ルーティングテーブルの仕組み、Linuxでの静的ルートの設定、IPフォワーディングを有効にしてLinuxをルーターとして使用する方法。
  • 前提条件:レッスン13 – ソケットプログラミングの基礎
  • 推定読了時間:20分

はじめに

パケットがコンピュータを離れるとき、どこに行くべきかをどのように知るのでしょうか?答えはルーティングにあります——データを送信するためのネットワーク内のパスを選択するプロセスです。ルーターはこれらの転送決定を行うデバイス(またはソフトウェア)であり、ルーティングテーブルを使用して各パケットの最適なネクストホップを決定します。

より低いレベルでは、スイッチは単一のネットワークセグメント内で動作し、MACアドレスに基づいてフレームを転送します。ルーティングとスイッチングの両方を理解することは、ネットワークの設計、設定、トラブルシューティングに不可欠です。

このレッスンでは、レイヤー2スイッチングとレイヤー3ルーティングの基礎を学び、Linuxがルーティングテーブルをどのように維持・使用するかを探索し、静的ルートを設定し、Linuxマシンをシンプルなルーターとしてセットアップします。

レイヤー2スイッチング vs レイヤー3ルーティング

ネットワークトラフィックはOSIモデルの2つの主要なレイヤーで転送されます。スイッチはレイヤー2(データリンク層)で動作し、MACアドレスに基づいてイーサネットフレームを転送します。ルーターはレイヤー3(ネットワーク層)で動作し、ルーティングテーブルを使用して宛先IPアドレスに基づいてIPパケットを転送します。

ルーティングの仕組み

ホストがパケットを送信する必要があるとき、まず宛先がローカルネットワーク上(同じサブネット)にあるかどうかを確認します。ある場合、パケットはレイヤー2経由で直接配信されます。宛先が異なるネットワーク上にある場合、パケットはデフォルトゲートウェイ(通常はルーター)に送信され、ルーターが宛先に向けて転送します。

ルーターはルーティングテーブルを参照して転送決定を行います。最長プレフィックスマッチを使用して宛先IPアドレスをテーブルエントリと照合します——最も具体的なルートが優先されます。

静的ルートの設定

静的ルートはルーティングテーブルに手動で設定されたエントリです。小規模なネットワーク、特定のトラフィックパス、または動的ルーティングの決定をオーバーライドする必要がある場合に便利です。

ルーターとしてのLinux

2つ以上のネットワークインターフェースを持つLinuxマシンは、IPフォワーディングを有効にすることでルーターとして機能できます。これにより、カーネルはパケットをドロップする代わりにインターフェース間で転送するようになります。

重要ポイント

  • スイッチはレイヤー2でMACアドレスを使用してフレームを転送し、ルーターはレイヤー3でIPアドレスとルーティングテーブルを使用してパケットを転送します。
  • ルーティングテーブルは最長プレフィックスマッチを使用して最適なルートを選択し、特定のルートが一致しない場合はデフォルトゲートウェイが使用されます。
  • 静的ルートはip route addで設定し、UbuntuのNetplan設定で永続化できます。
  • Linuxは、IPフォワーディング(net.ipv4.ip_forward=1)を有効にし、iptablesでNATを設定することでルーターとして機能できます。
  • tracerouteip route getip neigh showなどのツールは、ルーティングとスイッチングの問題の診断に役立ちます。

次のステップ

レッスン15では、Netplanによるネットワーク設定を学びます——インターフェース、アドレス、ルート、DNS設定を管理するためのUbuntuの宣言型YAMLベースのネットワーク設定システムです。

You Missed