How To Persist ip Rule And Route Whenever Server Rebooted?
Solution 1
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.
Solution 2
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
Related videos on Youtube
αԋɱҽԃ αмєяιcαη
White Hat Hacker Youtube/Facebook/Google Hall oF Flame Google Developer Cloud Area & CAPTCHA Core Team Developer SQL Expert. Linux And Windows Servers Administrator. Web Scraping. Sometimes Black Hat :) depending Mood!
Updated on September 18, 2022Comments
-
αԋɱҽԃ αмєяιcαη over 1 year
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.
-
Rui F Ribeiro about 7 years
-
sebasth over 6 years
-
-
αԋɱҽԃ αмєяιcαη about 7 yearsin 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?
-
Christopher about 7 yearsIt should, yes.
/etc/rc.d/rc.local
is a script that runs whichever commands we put in it. -
Jakov Sosic over 2 yearsRHEL8 part - network-scripts installation, doesn't make it work for me.