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
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.

2
  • 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? May 16 '17 at 13:16
  • It should, yes. /etc/rc.d/rc.local is a script that runs whichever commands we put in it. May 16 '17 at 13:25
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.