AS

Networking Basics: Switching, Routing, Gateways, and IP Forwarding (Linux)

Overview

  • This module covers fundamental networking concepts: switching, routing, gateways, and DNS configurations on Linux. Understanding how devices communicate over a network builds from switching within a single LAN to routing between multiple networks via gateways.
  • Key idea: a switch connects devices on the same network; a router connects different networks and forwards traffic between them.
  • DNS configurations are mentioned as a future topic in the course notes.

Interfaces and IP addressing

  • To inspect available interfaces on a Linux host, use: ip link.
    • Example output shows an interface such as eth0 with flags like BROADCAST, MULTICAST, UP, LOWER_UP, etc.
  • Subnet concept: assign IPs from a given subnet. Example subnet: 192.168.1.0/24.
  • IP address assignment (example):
    • System A: ip addr add exttt{ }192.168.1.10/24 exttt{ }dev exttt{ }eth0
    • System B: ip addr add exttt{ }192.168.1.11/24 exttt{ }dev exttt{ }eth0
  • Connectivity test: after IPs are configured, systems should be able to communicate via the switch. Example ping:
    • ping exttt{ }192.168.1.11
    • Expected reply example: Reply from 192.168.1.11: bytes=32 time=4ms TTL=117
  • Additional network segment example: another network 192.168.2.0/24 with hosts like 192.168.2.10 and 192.168.2.11.

Routing basics: connecting multiple networks

  • A switch by itself cannot route between different networks. To enable communication between networks (e.g., 192.168.1.0 and 192.168.2.0), a router is needed.
  • Router role: connects separate networks by using at least two network interfaces. Typical IPs on the two networks could be:
    • Interface on network 1: 192.168.1.1
    • Interface on network 2: 192.168.2.1
  • Routing concept: devices need proper routes to reach destinations on other networks.
  • Routing table check (example): use route to view the kernel IP routing table.
    • Example snippet (simplified):
      Destination Gateway Genmask Flags Metric Iface
  • Static route addition (example): to reach the 192.168.2.0/24 network from a host on the 192.168.1.0 network via the router at 192.168.1.1, add: ip route add 192.168.2.0/24 via 192.168.1.1
    • After adding, the host’s routing table shows that traffic to 192.168.2.0/24 is forwarded through the router.
  • Any system that communicates across networks (e.g., system C on 192.168.2.0 talking to system B on 192.168.1.0) must have a corresponding route set.

Default gateway and Internet access

  • Default gateway concept: simplifies routing for Internet access by using a single default route rather than many specific routes.
  • To reach the Internet (e.g., Google at a Google IP like 172.217.194.0/24) the router connected to the Internet should be configured as the default gateway.
  • Example of setting a default route:
    • Command: ip route add default via 192.168.2.1
  • Routing table example after setting a default route:
    Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 192.168.2.1 255.255.255.0 UG 0 0 0 eth0 172.217.194.0 192.168.2.1 255.255.255.0 UG 0 0 0 eth0 default 192.168.2.1 0.0.0.0 UG 0 0 0 eth0
  • Notes:
    • The keywords default and the entry 0.0.0.0 act as catch-all destinations for packets that do not match more specific routes.

Routing with multiple routers

  • In environments with more than one router, you can create explicit routing entries so that certain destinations use specific gateways.
    • Example: route all traffic to 192.168.1.0/24 via a router at 192.168.2.2, while other traffic uses the default gateway 192.168.2.1:
      ip route add 192.168.1.0/24 via 192.168.2.2
  • Resulting routing table would reflect a specific route for 192.168.1.0/24 via 192.168.2.2, with the default route remaining via 192.168.2.1.

Setting Up Linux as a Router (IP forwarding)

  • Imagine three hosts: A, B, and C.
    • A and B are on 192.168.1.0, B and C are on 192.168.2.0.
    • Host B has two interfaces: eth0 (192.168.1.6) and eth1 (192.168.2.6).
    • Host A: 192.168.1.5; Host C: 192.168.2.5.
  • Initial ping from A to C (ping 192.168.2.5) may fail with: "Connect: Network is unreachable" because A doesn’t know to reach the 192.168.2.0 network via B.
  • Add routes on A and C to use B as gateway:
    • On A: ip route add 192.168.2.0/24 via 192.168.1.6
    • On C: ip route add 192.168.1.0/24 via 192.168.2.6
  • After routes are added, ping from A to C may still fail initially because IP forwarding is disabled by default on Linux.
  • IP forwarding (enabling packet forwarding between interfaces) is controlled by the file: /proc/sys/net/ipv4/ip_forward.
    • Check status: cat /proc/sys/net/ipv4/ip_forward → expected output: 0 (disabled).
    • Temporarily enable forwarding:
      echo 1 > /proc/sys/net/ipv4/ip_forward
    • After enabling, a ping from A to C should succeed: e.g., ping 192.168.2.5 ->
      − Example of reply: Reply from 192.168.2.5: bytes=32 time=4ms TTL=117
  • Important note: This change is not persistent across reboots.
    • To enable IP forwarding permanently, modify the configuration in /etc/sysctl.conf (e.g., set net.ipv4.ip_forward=1).

Key networking commands (summary)

  • ip link – List and modify network interfaces on the host
  • ip addr – Display the IP addresses assigned to interfaces
  • ip addr add – Assign an IP address to an interface (temporary unless added to configuration files)
  • ip route or route – View the current routing table
  • ip route add – Add entries to the routing table
  • cat /proc/sys/net/ipv4/ip_forward – Check if IP forwarding is enabled
  • echo 1 > /proc/sys/net/ipv4/ip_forward – Temporarily enable IP packet forwarding (permanence requires config changes)

Closing notes

  • With these fundamentals, you have a solid understanding of basic networking, IP addressing, and routing.
  • In the next lesson, DNS configurations and additional network management techniques will be explored.