Routing is an essential aspect of network management on Linux systems, enabling seamless communication between different network interfaces and external destinations. Properly configuring IP routing can enhance network performance, optimize traffic flow, and prevent connectivity complications that might arise from mismanagement. In this extensive guide, we’ll explore the steps needed to effectively configure routing on Linux using both the ip command and the legacy route command.

Understanding IP Routing in Linux
Linux systems utilize routing tables to determine the path of network packets. An accurate routing configuration is crucial for ensuring that data travels through the most efficient routes, avoiding lag and potential downtime. This guide will help you set up and manage routing tables, covering basic routes to more advanced policy-based routing.
Configuring Routing with the ip Command
Step 1: Display the Current Routing Table
Before making changes, it’s important to review your existing routing table to understand your current routes and avoid conflicts. Execute the following command in your terminal:
1ip route show
This command lists all the existing routes, including default gateways and interface-specific paths. Analyzing this output reduces the likelihood of introducing errors during configuration.
Step 2: Adding a New Route
If you want to direct traffic destined for a certain subnet through a selected gateway, use the following example command to add a new route:
sudo ip route add 192.168.10.0/24 via 10.0.0.1 dev eth1
This instructs your Linux system to send packets destined for the specified subnet through the designated gateway and network interface. Remember to adjust the subnet, gateway, and interface names based on your network environment.
Step 3: Change the Default Gateway
To set a default gateway for all outgoing traffic, replace the previous default gateway with the command:
sudo ip route replace default via 192.168.1.1 dev eth0
This command ensures that packets without an explicit routing path are directed through the new default gateway. Note that only one default route should be active at any time to prevent routing conflicts.
Step 4: Make Routing Changes Persistent
Routing configurations set using the ip command are temporary and reset upon reboot. To save these changes permanently, edit your network configuration files. For most distributions, you’ll 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 (PBR) allows you to create routing rules based on various criteria, such as source addresses, providing flexibility in multi-homed environments.
Step 1: Create a Custom Routing Table
Start by creating a personalized routing table. Open /etc/iproute2/rt_tables and add an entry like:
100 customtable
This allocates the name “customtable” to table number 100 for easy reference.
Step 2: Add Routes to the Custom Table
Next, add routes that will be utilized by this custom table. For instance:
sudo ip route add 10.20.30.0/24 via 192.168.2.1 dev eth2 table customtable
This command directs all traffic to the specified subnet via its designated gateway within your custom routing table.
Step 3: Define Routing Rules
To utilize your custom table for specific sources, you can establish a routing rule. For instance:
sudo ip rule add from 192.168.100.5/32 table customtable
This indicates that traffic from the specific source address will follow the defined routes in “customtable.”
Step 4: View and Manage Routing Rules
Use the following command to review your applied rules and ensure they’re in the correct sequence:
1ip rule show
The order of these rules is crucial for routing behavior, so adjust priorities as necessary using the priority option.
Configuring Routing with the route Command (Legacy Method)
While the route command is considered a legacy tool, it might still be useful in certain situations. Here’s how to manage routes using it:
Step 1: Display Current Routes
Run this command to view your current routes in numeric format:
route -n
Step 2: Add a Network Route
To add a new route for a specific network, use the syntax:
sudo route add -net 192.168.10.0 netmask 255.255.255.0 gw 10.0.0.1 dev eth1
This performs a similar function to ip route add but uses older syntax.
Note: Just like with ip, any persistent routes configured with route must also be documented in your network configuration files as previously explained.
Conclusion
Mastering IP routing on Linux empowers administrators to manage network traffic effectively, ensuring optimal performance and connectivity. By following the detailed steps outlined in this guide, you should now have the tools and knowledge needed to configure and personalize routing settings on your Linux servers and desktops. Regularly revisiting and updating your routing tables will help you maintain a robust and reliable network infrastructure that adapts to changing needs.
And if you'd like to go a step further in supporting us, you can treat us to a virtual coffee ☕️. Thank you for your support ❤️!
We do not support or promote any form of piracy, copyright infringement, or illegal use of software, video content, or digital resources.
Any mention of third-party sites, tools, or platforms is purely for informational purposes. It is the responsibility of each reader to comply with the laws in their country, as well as the terms of use of the services mentioned.
We strongly encourage the use of legal, open-source, or official solutions in a responsible manner.


Comments