Linux servers and desktops rely on routing tables to determine how network packets move between interfaces and destinations. Incorrect routing configuration leads to connectivity problems, inefficient network paths, and service downtime. Adjusting routing settings allows you to control which network interfaces handle specific traffic, set up gateways, and implement advanced policies for complex environments.
Configuring Routing with ip Command
Step 1: Display current routing table to understand existing routes and identify necessary changes. Use the following command in your terminal:
ip route show
This command lists all routes, including default gateways and interface-specific paths. Reviewing this output helps you avoid conflicts when adding new routes.
Step 2: Add a new route to direct traffic destined for a specific subnet through a chosen gateway. For example, to route all traffic for 192.168.10.0/24
via gateway 10.0.0.1
on interface eth1
:
sudo ip route add 192.168.10.0/24 via 10.0.0.1 dev eth1
This command tells Linux to send packets for the specified subnet through the provided gateway and interface. Adjust the subnet, gateway, and interface names as needed for your environment.
Step 3: Change the system’s default gateway if you want all outgoing traffic to use a specific route. To set a new default gateway:
sudo ip route replace default via 192.168.1.1 dev eth0
This command replaces any existing default route with the new gateway and interface. Only one default route should exist at a time to avoid routing conflicts.
Step 4: Make routing changes persistent across reboots. Routes set with ip
are temporary and reset after a reboot. To save them permanently, edit your network configuration files. On most distributions, add static routes to /etc/network/interfaces
(Debian/Ubuntu) or /etc/sysconfig/network-scripts/route-ethX
(CentOS/RHEL), replacing ethX
with your actual interface name.
Setting Up Policy-Based Routing with ip rule
Policy-based routing lets you define routing rules based on source addresses, interfaces, or other criteria, rather than just destination IPs. This is useful in multi-homed systems or advanced network setups.
Step 1: Create a custom routing table by editing /etc/iproute2/rt_tables
and adding a new entry, such as:
100 customtable
This assigns the name customtable
to table number 100 for easy reference.
Step 2: Add routes to the custom table. For example, to route all traffic for 10.20.30.0/24
through gateway 192.168.2.1
on eth2
:
sudo ip route add 10.20.30.0/24 via 192.168.2.1 dev eth2 table customtable
This places the route in your custom routing table, not the main table.
Step 3: Define a routing rule to use your custom table for traffic matching specific criteria. For example, to route all traffic originating from 192.168.100.5
through your custom table:
sudo ip rule add from 192.168.100.5/32 table customtable
This rule tells Linux to consult the customtable
for traffic from the specified source address. Multiple rules can be stacked for complex scenarios.
Step 4: View all policy rules and ensure they are applied in the correct order. Use:
ip rule show
Rules are evaluated in sequence, so order can affect routing behavior. Adjust priorities as needed using the priority
option.
Configuring Routing with route Command (Legacy Method)
The route
command is an older tool for managing routes, still available on many systems but generally replaced by ip
. If required, you can use it as follows:
Step 1: Display current routes:
route -n
This provides a numeric table of current routes.
Step 2: Add a route to a specific network:
sudo route add -net 192.168.10.0 netmask 255.255.255.0 gw 10.0.0.1 dev eth1
This command works similarly to the ip route add
example, but uses older syntax.
route
must also be added to network configuration files, as described previously.With proper routing configuration, Linux systems direct traffic efficiently, support multi-homed setups, and avoid common networking pitfalls. Regularly review and update routing tables to adapt to network changes and maintain optimal connectivity.
Member discussion