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.
/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
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: [188.8.131.52] 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.
ip route show
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.