- 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 Add and Delete Static Route in Linux using IP Command
- Viewing the existing routing table
- Adding a static route using IP command
- Permanently adding static route (RHEL, Fedora, CentOS)
- Permanently adding static route (Ubuntu / Debian)
- Deleting a static route
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 Add and Delete Static Route in Linux using IP Command
Part of the skill set for any Linux user, and particularly a systems administrator, is the ability to perform some network tweaks on a Linux system. This includes adding and deleting routes to enable the system to communicate with other systems o a local network. In this guide, we explore exactly how you can go about adding and deleting routes on a Linux system.
Viewing the existing routing table
Before we embark on adding or deleting routes, it’s prudent to check the existing default routes on a system. To do so, simply launch your terminal and issue the command:
$ ip route show Or $ ip route list
Similar statistics can be displayed using route command,
Also, you can use the good old netstat command, which is usually used for printing interface statistics as well as the routing table to achieve the same result.
With the default routing statistics in mind, let’s now move a step further and add some routes to our system.
Adding a static route using IP command
Suppose you want to take a backup of a Linux machine and push the backup file to another backup server in the subnet 10.0.2.0/24 . However, for one reason or the other, you cannot reach the backup server via the default gateway. In this case, you will have to create a new route for backup server subnet via another IP, say 192.168.43.223 via the interface enp0s3 .
The command for this will be
$ sudo ip route add 10.0.2.0/24 via 192.168.43.223 dev enp0s3
- 10.0.2.0 -> is the network you want to connect to
- /24 -> is the subnet mask
- 192.168.43.223 -> is the IP through which we will reach the server
- enp0s3 -> is the network interface
You can confirm whether new static route add been in route table using “ip route show” command.
To add the specific IP of the backup server, say 10.0.2.15 run the command:
$ sudo ip route add 10.0.2.15 via 192.168.43.223 dev enp0s3
Once again, you can check the routing changes to see if the changes exist using the ip route show command:
Permanently adding static route (RHEL, Fedora, CentOS)
The routes we have just added are temporary and will not survive a reboot. To make the routes persistent, you need to manually add them.
In the /etc/sysconfig/network-scripts/ directory, create an interface file route-interface where the interface attribute is your network interface name. In our case, this will be route-enp0s3 .
$ vim /etc/sysconfig/network-scripts/route-enps03
Next, we will add the routes as shown:
10.0.2.0/32 via 192.168.43.1 10.0.2.15 via 192.168.43.1
Save the file and exit. Then restart NetworkManager Service
$ sudo systemctl restart NetworkManager
Permanently adding static route (Ubuntu / Debian)
For Debian distributions, edit the file /etc/network/interfaces
$ sudo vim /etc/network/interfaces
Append the following line:
up route add -net 10.0.2.0 netmask 255.255.255.0 gw 192.168.43.1 dev enp0s3
Save and exit the file. Finally, for the changes to come into effect, run below commands
$ sudo ifdown enp0s3 && sudo ifup enp0s3
Deleting a static route
To delete a specific route, use the ip route del command. For example, to remove the route address we just added, run the command:
$ sudo ip route del 10.0.2.0/24 via 192.168.43.223 dev enp0s3
To delete a single IP route in a subnet run the command
$ sudo ip route del 10.0.2.15 via 192.168.43.223 dev enp0s3
To delete default route run:
$ sudo ip route del default
To add a default route run below ‘ip route add’ command,
$ sudo ip route add default via dev interface
$ sudo ip route add default via 192.168.43.1 dev eth0
We hope that this tutorial was informative and provided you with insights into how you can go about adding and deleting static route in Linux.
Also Read : 12 ip Command Examples for Linux Users