- What is the best way to add a permanent route?
- 2 Answers 2
- Update for RHEL8
- What is a Permanent or Persistent static route
- 1. Edit /etc/network/interfaces file
- 2. Create own file in /etc/network/if-up.d directory
- How to Permanently add Static Route in Linux
- Adding temporary static routes
- Adding permanent static routes
- Conclusion
What is the best way to add a permanent route?
You are right sorry I didn’t specify that it was on a linux Debian system. Thanks for reminding it to me.
You should use iproute2 commands like ip route add
2 Answers 2
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.
What is a Permanent or Persistent static route
In Linux, permanent static routes also called as Persistent routes are the static route entries that will not be deleted when the network restart or when the system restart.
Typically in a Linux System, route add and ip route add commands are used to add static routes to the routing table. But those static route entries get deleted from the routing table when either network or system restart.
If you want to add a route to the network 192.168.1.0 through gateway 192.168.221.1 and print result, you can execute the following commands.
~] ip route add 192.168.1.0/24 via 192.168.221.1
And print result of previous command:
~] ip route show default via 84.244.68.1 dev ens192 onlink 84.244.68.0/24 dev ens192 proto kernel scope link src 84.244.68.206 192.168.1.0/24 via 192.168.221.1 dev ens192 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.206
Restart networking service
~] systemctl restart networking
After print result we can see that static rule is deleted:
~] ip route show default via 84.244.68.1 dev ens192 onlink 84.244.68.0/24 dev ens192 proto kernel scope link src 84.244.68.206 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.206
So how we can make static routes permanent? We have a several option how to do it.
1. Edit /etc/network/interfaces file
The first option is edit /etc/network/interfaces file.
Following is the sample Debian (Ubuntu) network interface configuration file with permanent static route entries.
# The primary network interface auto ens192 allow-hotplug ens192 iface ens192 inet static address 192.168.221.54/24 gateway 192.168.221.1 dns-nameservers 82.99.137.41 212.158.133.41 dns-search secar.cz up ip route del 192.168.0.0/24 via 192.168.221.1 dev ens192 up ip route add 192.168.0.0/24 via 192.168.221.1 dev ens192 up ip route del 192.168.1.0/24 via 192.168.221.1 dev ens192 up ip route add 192.168.1.0/24 via 192.168.221.1 dev ens192
When next hop (192.168.221.1) is in network subnet with direct attached interface, the dev [interface] in ip route command is optional.
Restart network with /etc/init.d/networking restart or with systemd restart networking command and print the result:
~] ip route show default via 192.168.221.1 dev ens192 onlink 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.54 192.168.0.0/24 via 192.168.221.1 dev ens192 192.168.1.0/24 via 192.168.221.1 dev ens192
2. Create own file in /etc/network/if-up.d directory
Another way to create a static network route is to create a script file in a directory /etc/network/if-up.d. For me, this is the preferred way to create static routes in debian.
Change working directory to /etc/network/if-up.d, create file my_route, change permissions to 751 with chmod 751 my_route and insert this content:
#!/bin/sh if [ "$IFACE" = "ens192" ]; then ip route add 192.168.0.0/24 via 192.168.221.1 ip route add 192.168.1.0/24 via 192.168.221.1 fi
~] ip route show default via 192.168.221.1 dev ens192 onlink 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.54 192.168.0.0/24 via 192.168.221.1 dev ens192 192.168.1.0/24 via 192.168.221.1 dev ens192
How to Permanently add Static Route in Linux
Static routing is the process of manually entering the routes to the routing table to reach a particular destination. Basically, two commands are used in Linux to add routes. The first command is the old traditional route add and second is the IP route command.
In this tutorial, we learn how to add permanent static routes in Linux distributions such as CentOS Stream and Ubuntu.
Adding temporary static routes
In order to add a static route temporarily on your Linux machine, you can use either route or ip command.
You can list the current routing table as follows.
Output Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.206.1 0.0.0.0 UG 100 0 0 eno1 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eno1 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 192.168.206.0 0.0.0.0 255.255.255.0 U 100 0 0 eno1
If you want to add a route to the network 198.161.1.0 through gateway 192.168.206.1, you can execute the following command.
sudo route add -net 198.161.1.0 netmask 255.255.255.0 gw 192.168.206.1 eno1
Alternatively, you can use ip command as follows:
sudo ip route add 198.161.1.0/24 via 198.168.206.1 dev eno1
Adding permanent static routes
In the above section, we saw how to add routes in Linux. But the system will forget those routes on next reboot.
So we have to add routes to config file to make it persistent.
On RHEL or CentOS, you need to modify the interface file in ‘/etc/sysconfig/network-scripts’.
For example, here, we have to add routes on network interface ens192. Hence, the file we need to modify will be ‘/etc/sysconfig/network-scripts/route-ens192’.
The basic syntax of static route in this file is:
target-address — Destination network address.
gateway-address — Router address to reach the destination address.
dev — Indicates which interface to reach the destination, interface naming schemes are eno[1-N], ens[1-N], and or eth[0-N].
So, in order to add the above route in ‘route-ens192’ file, append the following line.
‘10.9.8.0/24 via 10.9.8.100 dev ens192’
Then, you need to restart the network service.
sudo service network restart
Finally, verify that the new routes are visible in the routing table using the following command.
$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.9.8.100 0.0.0.0 UG 100 0 0 ens192 10.0.0.0 0.0.0.0 255.0.0.0 U 100 0 0 ens192 10.9.8.0 10.9.8.100 255.255.255.0 UG 100 0 0 ens192
On Ubuntu Linux, if you want to add a permanent static route to your system, you have to add route entry to the ‘/etc/network/interfaces’. Use your favorite editor (nano, vim. ) to open the network interface file and insert the following lines to it.
ip route add -net 10.9.7.0/24 gw 10.9.8.100 dev ens160
source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto ens160 iface ens160 inet static address 10.9.8.41 netmask 255.255.255.0 gateway 10.9.8.100 ip route add -net 10.9.7.0/24 gw 10.9.8.100 dev ens160
In order to make the change take effect, you need to restart the networking service by running:
sudo /etc/init.d/networking restart
Verify that the route has been configured correctly:
$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.9.8.100 0.0.0.0 UG 0 0 0 ens160 10.9.7.0 10.9.8.100 255.255.255.0 UG 0 0 0 ens160 10.9.8.0 0.0.0.0 255.255.255.0 U 0 0 0 ens160 10.233.70.0 10.9.8.45 255.255.255.0 UG 0 0 0 tunl0 10.233.90.0 0.0.0.0 255.255.255.0 U 0 0 0 *
From Ubuntu 18.04 LTS and higher version, the operating system uses the netplan YAML files for network configuration — it is located in the ‘/etc/netplan’ directory. For example:
$ ls /etc/netplan 01-network-manager-all.yaml
In order to add persistent static route, you have to add the following lines to the netplan configuration file ‘/etc/netplan/01-network-manager-all.yaml’
routes: - to: 192.168.205.0/24 via: 192.168.206.1
# Let NetworkManager manage all devices on this system network: version: 2 renderer: NetworkManager ethernets: eno1: dhcp4: true routes: - to: 192.168.205.0/24 via: 192.168.206.1
Apply the change by running:
You can verify the route configuration by running the command route -n or ip route .
Conclusion
In this tutorial, we learned how to add a permanent route by adding to respective config files in Linux. Thanks for reading and please leave your suggestion in the below comment section.
If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks