I need to add a route that won’t be deleted after reboot. I read these two ways of doing it :
Add
ip route add -net 172.X.X.0/24 gw 172.X.X.X dev ethXto the file/etc/network/interfaces
or
Create the file /etc/network/if-up.d/route with:
#!/bin/sh route add -net 172.X.X.0/24 gw 172.X.X.X dev ethX
and make it executable :
chmod +x /etc/network/if-up.d/route
So I’m confused. What is the best way of doing it?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You mentioned /etc/network/interfaces, so it’s a Debian system…
Create a named routing table. As an example, I have used the name, “mgmt,” below.
echo '200 mgmt' >> /etc/iproute2/rt_tables
Above, the kernel supports many routing tables and refers to these by unique integers numbered 0-255. A name, mgmt, is also defined for the table.
Below, a look at a default /etc/iproute2/rt_tables follows, showing that some numbers are reserved. The choice in this answer of 200 is arbitrary; one might use any number that is not already in use, 1-252.
# # reserved values # 255 local 254 main 253 default 0 unspec # # local #
Below, a Debian 7/8 interfaces file defines eth0 and eth1. eth1 is the 172 network. eth0 could use DHCP as well. 172.16.100.10 is the IP address to assign to eth1. 172.16.100.1 is the IP address of the router.
source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The production network interface auto eth0 allow-hotplug eth0 # iface eth0 inet dhcp # Remove the stanzas below if using DHCP. iface eth0 inet static address 10.10.10.140 netmask 255.255.255.0 gateway 10.10.10.1 # The management network interface auto eth1 allow-hotplug eth1 iface eth1 inet static address 172.16.100.10 netmask 255.255.255.0 post-up ip route add 172.16.100.0/24 dev eth1 src 172.16.100.10 table mgmt post-up ip route add default via 172.16.100.1 dev eth1 table mgmt post-up ip rule add from 172.16.100.10/32 table mgmt post-up ip rule add to 172.16.100.10/32 table mgmt
Reboot or restart networking.
Update – Expounding on EL
I noticed in a comment that you were “wondering for RHEL as well.”
In Enterprise Linux (“EL” – RHEL/CentOS/et al), create a named routing table as mentioned, above.
The EL /etc/sysconfig/network file:
NETWORKING=yes HOSTNAME=host.sld.tld GATEWAY=10.10.10.1
The EL /etc/sysconfig/network-scripts/ifcfg-eth0 file, using a static configuration (without NetworkManager and not specifying “HWADDR” and “UUID” for the example, below) follows.
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 /etc/sysconfig/network-scripts/ifcfg-eth1 file (without NetworkManager and not specifying “HWADDR” and “UUID” for the example, below) follows.
DEVICE=eth1 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=no BOOTPROTOCOL=none IPADDR=172.16.100.10 NETMASK=255.255.255.0 NETWORK=172.16.100.0 BROADCAST=172.16.100.255
The EL /etc/sysconfig/network-scripts/route-eth1 file:
172.16.100.0/24 dev eth1 table mgmt default via 172.16.100.1 dev eth1 table mgmt
The EL /etc/sysconfig/network-scripts/rule-eth1 file:
from 172.16.100.0/24 lookup mgmt
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.
Method 2
On Debian based distro you can add a static route permanently as follows:
echo "up route add -net 172.X.X.X/24 gw 172.X.X.X dev ethX" | sudo tee --append /etc/network/interfaces
On RHEL based distro:
echo "172.X.X.X/24 via 172.X.X.X" | sudo tee --append /etc/sysconfig/network-scripts/route-ethX
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0