RHEL 8 and later -- and all derived distributions, like CentOS and AlmaLinux -- no longer use the legacy /etc/sysconfig/network-scripts
directory. The correct solution is to modify the NetworkManager configuration.
NetworkManager can add both static routes and policy routing rules. There is some relevant documentation here.
On a default CentOS 8(stream) install, my NetworkManager configuration looks like this:
[root@localhost ~]# nmcli c show
NAME UUID TYPE DEVICE
System eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 ethernet eth0
If I want to add a policy route to eth0
, I would do something like this:
nmcli c mod 'System eth0' \
ipv4.addresses 10.0.0.10/24 \
ipv4.routes '0.0.0.0/0 10.0.0.1 table=1' \
ipv4.route-rules 'priority 100 from 10.0.0.0/24 table 1'
In this example I'm adding a static ip addresses that is different from the one that gets assigned dynamically using DHCP; this may not be necessary in your situation depending on what your local configuration looks like.
After restarting the interface:
nmcli c down 'System eth0'; nmcli c up 'System eth0'
I now have:
[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 52:54:00:c1:f0:62 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.10/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.122.164/24 brd 192.168.122.255 scope global dynamic noprefixroute eth0
valid_lft 3505sec preferred_lft 3505sec
inet6 fe80::5054:ff:fec1:f062/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ip rule show
0: from all lookup local
100: from 10.0.0.0/24 lookup 1
32766: from all lookup main
32767: from all lookup default
[root@localhost ~]# ip route show table 1
default via 10.0.0.1 dev eth0 proto static metric 100
10.0.0.1 dev eth0 proto static scope link metric 100