I have 2 network interfaces on my "CentOS 7 Minimal" box. I need to setup policy based routing, so that if a request comes in on one interface, the reply gets sent out the same interface. Doing this with iproute2 in a non-persistent way is not terribly difficult:
echo '200 isp2' >> /etc/iproute2/rt_tables ip route add default via 126.96.36.199 table isp2 ip rule add from 192.168.1.50 table isp2
Where 188.8.131.52 is my router, and 192.168.1.50 is my linux's IP. The problem is making this persistent.
From what I've read, there's no iproute2 persistence file - the way to make things persistent is to add them to your network startup scripts.
Normally, RedHat recommends creating the file /etc/sysconfig/network-scripts/route-ethX to add routes, but this does not appear to be able to add routes to a secondary routing table.
The solution seems to be to add the "ip route" and "ip rule" lines from above to /etc/sysconfig/network instead. This almost works, except for one thing: Every time the network is restarted, it keeps adding duplicate "ip rule" rules, dozens at a time.
After restarting my network 2 or 3 times with "systemctl restart network", my
ip rule output looks like this:
0: from all lookup local 32670: from 192.168.1.50 lookup isp2 32671: from 192.168.1.50 lookup isp2 ... 32764: from 192.168.1.50 lookup isp2 32765: from 192.168.1.50 lookup isp2 32766: from all lookup main 32767: from all lookup default
What is the proper way to add persistent policy-based-routing rules on CentOS 7?
ip route list table isp2 command shows a single entry, as expected, so that's not a problem.
Edit: According to the RedHat 7 manual:
in Red Hat Enterprise Linux the use of the global /etc/sysconfig/network file is deprecated
This is in favour of interface-specific config files, which don't appear to be able to add routes to a different routing table, and can't add
ip rule rules.