6

I'm using Centos 7 Server And I Would Like To Save ip Rule And Route Whenever Server Rebooted.

ip rule add from x.x.x.x table 128 ip route add table 128 to y.y.y.y/y dev eth0 ip route add table 128 default via z.z.z.z 

The mentioned Rule and Route lose once i reboot the server which means i need to run the 3 commands each time server rebooted.

I need to make ip rule and route persist whenever server is rebooted.

2

3 Answers 3

8

Take a look at /etc/rc.d/rc.local. The file states

Please note that you must run chmod +x /etc/rc.d/rc.local to ensure that this script will be executed during boot.

So:

chmod +x /etc/rc.d/rc.local 

Then place your commands above the last line

touch /var/lock/subsys/local 

There is better way using relevant configuration files. Rules and routes can be specified using corresponding file names. All the relevant configuration files are given below. (The device names may differ.)

/etc/iproute2/rt_tables /etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1 /etc/sysconfig/network-scripts/route-eth0 /etc/sysconfig/network-scripts/route-eth1 /etc/sysconfig/network-scripts/rule-eth0 /etc/sysconfig/network-scripts/rule-eth1 

To create a named routing table, use /etc/iproute2/rt_tables. I added 128 mynet.

# # reserved values # 255 local 254 main 253 default 0 unspec # # local # 128 mynet 

The EL 7.x /etc/sysconfig/network file. The default route is GATEWAY.

NETWORKING=yes HOSTNAME=hostname.sld.tld GATEWAY=10.10.10.1 

THE EL 7.x /etc/sysconfig/network-scripts/ifcfg-eth0 file, without HWADDR and "UUID". This configures a static IP address for eth0 without using NetworkManager.

DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=no BOOTPROTOCOL=none IPADDR=10.10.10.140 NETMASK=255.255.255.0 NETWORK=10.10.10.0 BROADCAST=10.10.10.255 

THE EL 7.x /etc/sysconfig/network-scripts/ifcfg-eth1 file, without HWADDR and UUID. This configures a static IP address for eth1 without using NetworkManager.

DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=no BOOTPROTOCOL=none IPADDR=192.168.100.140 NETMASK=255.255.255.0 NETWORK=192.168.100.0 BROADCAST=192.168.100.255 

The EL 7.x /etc/sysconfig/network-scripts/route-eth1 file. The default route was already specified in /etc/sysconfig/network.

192.168.100.0/24 dev eth1 table mynet default via 192.168.100.1 dev eth1 table mynet 

The EL 7.x /etc/sysconfig/network-scripts/rule-eth1 file:

from 192.168.100.0/24 lookup mynet 

Update for RHEL8

This method described above works with RHEL 6 & RHEL 7 as well as the derivatives, but for RHEL 8 and derivatives, one must first install network-scripts to use the method described above.

dnf install network-scripts 

The installation produces a warning that network-scripts will be removed in one of the next major releases of RHEL and that NetworkManager provides ifup/ifdown scripts as well.

3
  • in case if i gonna use the first option only are it's gonna save ip rule and ip route command even if server rebooted? and if yes, shall i insert the 3 commands as it is? Commented May 16, 2017 at 13:16
  • It should, yes. /etc/rc.d/rc.local is a script that runs whichever commands we put in it. Commented May 16, 2017 at 13:25
  • RHEL8 part - network-scripts installation, doesn't make it work for me. Commented Dec 7, 2021 at 10:36
3

If you need to use iproute2, so

# ip rule save > /[somepath]/your-ruleset.bin 

will save your rules. And next time you should just

# ip rule restore < /[somepath]/your-ruleset.bin 
3

I can't comment, but want to complement the accepted answer. It is not strictly necessary to install network-scripts package in RHEL8.

The following files still get picked up by NetworkManager:

/etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1 /etc/sysconfig/network-scripts/route-eth0 /etc/sysconfig/network-scripts/route-eth1 

The following files no longer get picked up by NetworkManager

/etc/sysconfig/network-scripts/rule-eth0 /etc/sysconfig/network-scripts/rule-eth1 

However, you can define rules in the ifcfg-eth0 scripts like such:

DEVICE=eth0 TYPE=Ethernet ONBOOT=yes ROUTING_RULE_1="from 192.168.100.0/24 table 5" ... 

To load settings from the files to NetworkManager, execute:

$ sudo nmcli connection reload 

You will then see the routing rules if you run:

$ nmcli connection show eth0 $ nmcli connection show eth0 | grep rules 

To apply NetworkManager configuration to devices and make them active, execute:

$ sudo nmcli device reapply eth0 

Now you should have rules and routie visible with:

$ ip route list all $ ip rule list 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.