How To Persist ip Rule And Route Whenever Server Rebooted?
How To Persist ip Rule And Route Whenever Server Rebooted?
https://unix.stackexchange.com/questions/365380/how-to-persist-ip-rule-and-route-whenever-server-rebooted
Asked 8 years, 6 months ago Modified 3 years, 10 months ago Viewed 43k times 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.
linuxcentosnetworkingiptablesnetworkmanager
Share Improve this question Follow edited Mar 10, 2019 at 4:27 Rui F Ribeiro’s user avatar Rui F Ribeiro 58k2828 gold badges156156 silver badges239239 bronze badges asked May 16, 2017 at 12:34 αԋɱҽԃ αмєяιcαη’s user avatar αԋɱҽԃ αмєяιcαη 1,26777 gold badges2424 silver badges4444 bronze badges
access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/… –
Rui F Ribeiro
Commented May 16, 2017 at 13:54
RHEL7 Networking Guide: Configuring Static Routes in ifcfg files –
sebasth
Commented Sep 21, 2017 at 20:05
Add a comment 3 Answers Sorted by: 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. Share Improve this answer Follow edited Jun 18, 2021 at 9:12 Eddie C.’s user avatar Eddie C. 45744 silver badges77 bronze badges answered May 16, 2017 at 13:08 Christopher’s user avatar Christopher 16.4k77 gold badges5656 silver badges6666 bronze badges
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? –
αԋɱҽԃ αмєяιcαη
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. –
Christopher
Commented May 16, 2017 at 13:25
RHEL8 part - network-scripts installation, doesn't make it work for me. –
Jakov Sosic
Commented Dec 7, 2021 at 10:36
Add a comment 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
Share Improve this answer Follow edited Apr 2, 2021 at 17:53 αғsнιη’s user avatar αғsнιη 41.9k1717 gold badges7575 silver badges118118 bronze badges answered Apr 2, 2021 at 3:45 UFO999’s user avatar UFO999 3111 bronze badge Add a comment 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
Share Improve this answer Follow answered Jan 20, 2022 at 14:56 KrNeki’s user avatar KrNeki 533