Networking Basics: Switching, Routing, Gateways, and Linux IP Forwarding
Network Foundations: Switches, Routers, Gateways
- A network is formed when multiple hosts are connected to a switch, which creates a local network containing those systems.
- The switch can forward packets only within the same network (layer 2 switching); it cannot connect different networks by itself.
- A gateway/router connects two or more networks and enables communication between them.
- DNS and DNS configuration on Linux are topics covered in the next lecture.
Interfaces and IP Addressing
- To connect a host to a switch, an interface on the host is needed (physical or virtual).
- On a host, view interfaces with the command: \text{ip link}.
- Example interface used for the connection: \text{eth0} (the interface name may vary by system).
- Assign IP addresses on the same network (subnet) to hosts on the same network:
- Network 1: 192.168.1.0/24 with hosts at 192.168.1.10 and 192.168.1.11.
- Network 2: 192.168.2.0/24 with hosts at 192.168.2.10 and 192.168.2.11.
- IP addresses are configured with the command: \text{ip addr add }
/ \text{ dev } . The transcript uses a shorthand that resembles \text{ip addr} (often written as ip a) and then \text{ip addr add} to set addresses. - Once links are up and IPs are assigned, hosts can communicate with others on the same network via the switch.
Switching vs Routing: Two Networks and a Router
- In the two-network example:
- Network 1: 192.168.1.0/24 with host B at 192.168.1.11 and a router on this network at 192.168.1.1.
- Network 2: 192.168.2.0/24 with host C at 192.168.2.10 and a router on this network at 192.168.2.1.
- A router is an intelligent device with at least one IP on each connected network, enabling communication between the networks.
- Routing decisions are driven by the routing table in the kernel.
Gateway and Routing Tables
- The gateway is the door to reach other networks or the Internet. Systems need to know where this door is to forward traffic.
- To view the kernel's routing table on a system, use the legacy command: \text{route}. Initially, there may be no routes configured besides the local network.
- If a host cannot reach a different network (e.g., from 192.168.1.0/24 to 192.168.2.0/24), you must configure a route via the gateway on the corresponding network.
- On a host in Network 1, add a route to reach 192.168.2.0/24 via the gateway at 192.168.1.1:
- \text{ip route add } 192.168.2.0/24 \text{ via } 192.168.1.1
- After this, check with: \text{ip route} to verify the new entry.
- On a host in Network 2, add the complementary route to reach 192.168.1.0/24 via the gateway at 192.168.2.1:
- \text{ip route add } 192.168.1.0/24 \text{ via } 192.168.2.1
- If you need Internet access, the router connects to the Internet (e.g., under a Google address space like 172.217.194.0/24) and a route is added for that network via the router.
- To simplify, you can use a default route for any unknown network:
- \text{ip route add default via } 192.168.1.1
- This is equivalent to the default gateway of value 0.0.0.0/0 in routing terms.
- A default route means any destination not in the local routing table will be sent to the specified gateway.
- If there are multiple routers (e.g., one for internal networks and another for Internet), you’ll need explicit routes for the internal networks and a default route for all others.
- If Internet access is not working, starting with the routing table and the default gateway is a good debugging place.
A Minimal Linux Router: Three-Host Example
- Setup:
- A, B, C are three hosts. A and B are on network 192.168.1.0/24; B and C are on network 192.168.2.0/24.
- B has two interfaces: one on each network (e.g., eth0 and eth1).
- Addresses:
- A: 192.168.1.5 on network 1
- B: 192.168.1.6 on network 1 and 192.168.2.6 on network 2
- C: 192.168.2.5 on network 2
- How to make A reach C:
- Initially, ping from A to C (e.g., 192.168.2.5) yields "network is unreachable".
- Add a route on A to reach the 192.168.2.0/24 network via B’s IP on network 1: \text{ip route add } 192.168.2.0/24 \text{ via } 192.168.1.6
- How to make C reach A:
- Add a route on C to reach the 192.168.1.0/24 network via B’s IP on network 2: \text{ip route add } 192.168.1.0/24 \text{ via } 192.168.2.6
- Packet forwarding is disabled by default in Linux for security reasons. To allow routing between interfaces (A↔B↔C):
- Enable IP forwarding at runtime: \text{echo 1 > } /proc/sys/net/ipv4/ipforward or set the value in /proc/sys/net/ipv4/ipforward to 1.
- Persist across reboots by editing /etc/sysctl.conf (or a dedicated conf file under /etc/sysctl.d/) with the line: net.ipv4.ip_forward=1 and then apply with sysctl -p.
Quick Reference: Key Commands and Persistence
Interfaces and addresses:
- List interfaces: \text{ip link}
- View addresses: \text{ip addr}
- Add/assign an address: \text{ip addr add }
/ \text{ dev } - Bring an interface up: \text{ip link set }
\text{ up} - Note: IP address changes may not persist across reboots unless configured in the system network config (e.g., /etc/network/interfaces on Debian/Ubuntu).
Routing:
- View routes: \text{route -n} (legacy) or \text{ip route show} (modern)
- Add routes: \text{ip route add }
/ \text{ via } - Default route: \text{ip route add default via }
or 0.0.0.0/0\text{ via } - Persistence: depends on distro; may involve editing /etc/network/interfaces, a /etc/sysctl.d/ file, or distribution-specific network manager configs.
IP forwarding status:
- Check: \text{cat } /proc/sys/net/ipv4/ip_forward (0 = off, 1 = on)
- Enable temporarily: \text{echo 1 > } /proc/sys/net/ipv4/ip_forward
- Enable persistently: add net.ipv4.ip_forward=1 to /etc/sysctl.conf or a dedicated file under /etc/sysctl.d/ and reload with sysctl -p.
Practical Takeaways and Real-World Relevance
- The switch handles local traffic within a single subnet; routers are needed to connect different subnets and to reach the Internet.
- Gateways simplify traffic management by providing a single exit point for traffic destined for other networks.
- Default routes simplify configuration when there are many possible external destinations; they direct unknown traffic to a single gateway, which then handles the path via the Internet edge.
- Turning a Linux host into a router requires enabling IP forwarding and correctly configuring routes on all collaborating hosts; persistence matters for reboots.
- DNS configuration will be covered in the next lecture, tying hostname resolution to IP addressing and routing.