Routing
Routing is the process of selecting a path for traffic in a network, or between or across multiple networks.
Static vs Dynamic Routing
- Static Routing: Static routing is a form of routing that occurs when a router uses a manually-configured routing entry, rather than information from a dynamic routing protocol, to forward traffic. Static routes are simple to configure and are suitable for small, simple networks.
- Dynamic Routing: Dynamic routing is a networking technique that provides optimal data routing. Unlike static routing, dynamic routing enables routers to select paths according to real-time logical network layout changes. Dynamic routing uses protocols such as OSPF (Open Shortest Path First) and BGP (Border Gateway Protocol) to discover and maintain routes.
Default Gateway Setup
A default gateway is the node in a computer network that the router uses to send all traffic for which it does not have a specific route.
Setting the Default Gateway in Linux
You can set the default gateway using the ip route command:
# Add a default gateway
sudo ip route add default via 192.168.1.1
# Delete the default gateway
sudo ip route del default
To make the change persistent, you need to edit the appropriate network configuration file for your distribution (e.g., /etc/network/interfaces for Debian/Ubuntu, or /etc/sysconfig/network-scripts/ifcfg-<interface> for RHEL/CentOS).
Linux ip route Command Examples
The ip route command is a powerful tool for managing the routing table in Linux.
-
Show the routing table:
ip route show -
Add a route to a specific network:
# Route traffic for the 10.0.1.0/24 network through the gateway at 192.168.1.254
sudo ip route add 10.0.1.0/24 via 192.168.1.254 -
Add a route to a specific host:
# Route traffic for the host 10.0.2.10 through the eth1 interface
sudo ip route add 10.0.2.10/32 dev eth1 -
Delete a route:
sudo ip route del 10.0.1.0/24