How to Add Custom Routing Rules for Multiple NICs on Linux
This guide explains how to configure additional routing tables and policy‑based routes on a Linux server with two network interfaces, covering IP address inspection, routing table edits, ip rule commands, and persistence across reboots.
Besides the default route, you can configure additional routes on Linux.
For example, a server may have two interfaces (eth0 and eth1). By default, all traffic is routed through eth0 regardless of the IP address configured on eth1.
To route inbound and outbound traffic via eth1, you need to add extra routes for eth1.
eth0 is configured with IP 19.86.101.54, netmask 255.255.255.0, gateway 19.86.101.1
eth1 is configured with IP 19.86.100.176, netmask 255.255.255.0, gateway 19.86.100.1
You can view the current IP addresses with the ifconfig command:
# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:50:56:8E:0B:EC
inet addr:19.86.101.54 Bcast:19.86.101.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3735 errors:0 dropped:0 overruns:0 frame:0
TX packets:336 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:295679 (288.7 Kb) TX bytes:50312 (49.1 Kb)
eth1 Link encap:Ethernet HWaddr 00:50:56:8E:27:0D
inet addr:19.86.100.176 Bcast:19.86.100.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:14 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:840 (840.0 b) TX bytes:0 (0.0 b)The netstat -rn output shows the default gateway points to eth0:
# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 19.86.101.1 0.0.0.0 UG 0 0 0 eth0
19.86.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
19.86.101.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0To make eth1 reachable from external networks, create a new policy routing table in /etc/iproute2/rt_tables. The initial file may look like this:
# cat /etc/iproute2/rt_tables
#
# reserved values
#
255 local
254 main
253 default
0 unspec
#
# local
#
#1 inr.ruhep
#Show current rules with:
# ip rule show
0: from all lookup local
32766: from all lookup main
32767: from all lookup defaultFirst, back up the table file:
cd /etc/iproute2
cp rt_tables rt_tables.origThen add a new entry for a custom table named admin: echo "1 admin" >> /etc/iproute2/rt_tables Add routes to the admin table:
ip route add 19.86.100.0/24 dev eth1 src 19.86.100.176 table admin
ip route add default via 19.86.100.1 dev eth1 table adminExplanation:
The first command adds the subnet 19.86.100.0/24 to the admin table with source IP 19.86.100.176 on device eth1.
The second command adds a default route via 19.86.100.1 to the admin table, ensuring all traffic defined in this table uses eth1.
Now tell the kernel to use this table for traffic to/from the eth1 address:
ip rule add from 19.86.100.176/24 table admin
ip rule add to 19.86.100.176/24 table admin
ip route flush cacheThese rules place the new entries before the default main table (32766) because rules are evaluated in ascending order.
Verify the changes:
# ip rule show
0: from all lookup local
32764: from all to 19.86.100.176/24 lookup admin
32765: from 19.86.100.176/24 lookup admin
32766: from all lookup main
32767: from all lookup defaultAt this point you should be able to ping 19.86.100.176 from an external network and see that traffic uses eth1 as expected.
To keep the configuration after a reboot, add the commands to /etc/init.d/boot.local (SUSE) or /etc/rc.d/rc.local (RedHat/CentOS).
If you need another IP on a different subnet, repeat the steps with a new table name (e.g., admin‑new).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
