Debian Permanent Static Routes

Debian Permanent Static Routes

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.

example:

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.

Example:

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:

/etc/network/if-up.d/my_route
#!/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

SUBSCRIBE FOR NEW ARTICLES

@
comments powered by Disqus