How to add permanent static routes in Ubuntu Linux

In this tutorial we are going to learn how to add permanent static route in Ubuntu Linux.

For this tutorial I am using Ubuntu Server 16.04, But you can use the following method to add 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 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.

So how we can make static routes permanent?

Making Static Routes Persistent in Ubuntu Linux

In Ubuntu Linux, in order to make Static Routes Persistent, we need to add route entries to the network interface file (/etc/network/interfaces) using up lines.

Example

Following is the sample Ubuntu network interface configuration file with permanent static route entries.

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.15
up route add -net 192.168.10.0 netmask 255.255.255.0 dev enp2s0

As per the above example, we have added two permanent static route entries using the “up” argument.

the "up" lines are executed when the interface is brought up. So when network restart, those route entries will be added to the routing table automatically.

You need to restart Ubuntu networking, if you add a new route entry to the /etc/network/interfaces file.

service networking restart

You can view the routing table using the ip route show command.

ip route show

Examples - Persistent routes in Ubuntu Linux

up route add -net 192.168.10.0 netmask 255.255.255.128 gw 192.168.1.1

Gateway for the network 192.168.10.0 is 192.168.1.1.

up route add -net 192.56.76.0 netmask 255.255.255.0 dev eth1

Network 192.56.76.0 route through the network interface eth1.

up route add -host 192.168.10.100 gw 192.168.1.1

As per the above example, host 192.168.1.100 will route through the gateway 192.168.1.1.

So that is how we add permanent routes in Ubuntu Linux. This method uses to add Persistent route in all Debian Based Linux Distributions.

submit to reddit