- 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
- View Network Routing Table Using the ip route Command in Linux Mint 20.3
- View Network Routing Table Using the “ip route” Command
- Adding New Routes
- Permanently Adding Routes in Linux
- Deleting Routes
- Adding a New Default Gateway
- Conclusion
- About the author
- Karim Buzdar
- How to add permanent static routes in Ubuntu Linux
- What is a Persistent route ?
- Making Static Routes Persistent in Ubuntu Linux
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
View Network Routing Table Using the ip route Command in Linux Mint 20.3
A routing table contains routing entries that determine where all packets go when they leave a router or a system. Usually, the Linux systems use dynamic routing, where the kernel decides which route out of multiple routes a packet should follow. However, you can also add static routes, which are not dynamically updated, if you want to forward specific traffic to a particular gateway or router.
In today’s article, we will cover how to view network routing tables using the “ip route” command in Linux Mint 20.3 OS.
Note: You need sudo privileges to make any changes in the routing table. However, viewing the routing table doesn’t need any sudo privileges.
View Network Routing Table Using the “ip route” Command
The “ip route” command in Linux is used to view and modify the routing table. Using this command, you can add new routes to a particular network or host. You can delete the routes if you no longer want them in your routing table.
To view the network routing table in your Linux Mint, open the Terminal and run the following command:
In the output, you will see a list of network destinations and gateways. The gateways are the addresses where the packets are forwarded when they are moving toward their destination. These routes are dynamic routes unless you have already added the static routes.
Following is the output of the “ip route” command in our system. The 192.168.42.0 is the local network attached to the network interface ens33. The 192.168.42.2 is the default gateway. Any traffic not intended for the local network and is not defined in the routing table is forwarded to this address.
Adding New Routes
Using the “ip route” command, you can also add a new route for a specific network. Use the following syntax to add a new route in your system’s routing table:
For instance, the following command adds the route for the 10.0.0.0/24 network through gateway 192.168.42.2 to route it through the ens33 network interface.
Permanently Adding Routes in Linux
The static route added by the “ip route” command is not a persistent route. A persistent route stays in place even when you reboot your system. To permanently add the static routes and to make them persistent, you will need to add the entry in the /etc/network/interfaces file.
Edit the /etc/network/interfaces file through the following command:
Add following entry for your static route:
Then, save and close the file and restart the network service:
Deleting Routes
To delete a route using the “ip route” command, use the previous syntax but replace the add option by del:
Adding a New Default Gateway
Sometimes, you must add a new default gateway to your Linux system. The “ip route” command also allows you to add a new default gateway. Use the following syntax:
Conclusion
In this post, we reviewed how to view the network routing table using the “ip route” command in Linux Mint 20.3 OS. We also covered how to permanently add routes in Linux Mint so that they persist after reboot. Remember, this is not the only way to view the network routing table in Linux. You can also view the routing table using the “netstat” and “route” commands.
About the author
Karim Buzdar
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.
How to add permanent static routes in Ubuntu Linux
In this tutorial, we are going to learn how to add a permanent static route in Ubuntu Linux.
For this tutorial, I am using Ubuntu Server 20.04, But you can use the following method to add a Persistent route in any previous Ubuntu version, including Ubuntu Desktop.
What is a Persistent route ?
In Ubuntu, permanent static routes, also called as Persistent routes are the static route entries that will not be deleted when the network restarts, or when the system restarts.
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.
So how can we make static routes permanent?
Making Static Routes Persistent in Ubuntu Linux
In Ubuntu Linux, to make Static Routes Persistent, we need to add route entries to the network interface file (YAML text files in the /etc/netplan folder) using the routes property.
The /etc/netplan directory is the location where network configuration files are stored on Ubuntu Linux. Under the /etc/netplan directory, you will find at least one network configuration file with .yaml extension.
Following is a sample Ubuntu network interface configuration file with permanent static route entries.
# This is the network config written by 'subiquity' network: ethernets: enp0s3: dhcp4: false addresses: [192.168.1.100/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8] routes: - to: 192.168.2.0/24 via: 192.168.1.100 metric: 100 - to: 192.168.10.100 via: 192.168.1.100 metric: 100 version: 2
As per the above example, we have added two permanent static route entries using the routes argument under the network interface enp0s3.
routes: - to: 192.168.2.0/24 via: 192.168.1.100 metric: 100 - to: 192.168.10.100 via: 192.168.1.100 metric: 100
The gateway to use for the 192.168.2.0/24 network is 192.168.1.100. We have also set a host route to a host 192.168.10.100
via the 192.168.1.100 IP.
You need to reload netplan configuration via netplan apply command, if you add a new route entry to the YAML file.
You can view the routing table using the ip route show command.
If you are working on an older version of Ubuntu (16.04 or earlier) that still uses /etc/network/interfaces file, you need to use the up lines make static routes permanent, as shown in the following example:
auto enp0s3 iface enp0s3 inet static address 192.168.1.10 netmask 255.255.255.0 gateway 192.168.1.1 up route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.100
So that is how we add permanent routes in Ubuntu Linux. This method is used to add Persistent routes in all Debian Based Linux Distributions.