Thursday 22 August 2019

Static Routes in Ubuntu 18.04.1

How to set permanent static routes in Ubuntu 18.04

New: netplan

For a somewhat strange Linux cluster configuration, I needed to set a few static routes. Other than suggested in some HOWTOs, the configuration is not in /etc/network/interface but (as it also says in the interface file)  in the /etc/netplan directory. In my case, in a file named 01-netcfg.yaml, with the following structure:

----8x---- cut here ----- 8X-------

network:
        version: 2
        renderer: networkd
        ethernets:
                ens170:
                        dhcp4: no
                        addresses: [192.168.10.145/24]
                        gateway4: 192.168.10.1
                        nameservers:
                                addresses: [192.168.1.254,192.168.1.253]
                                search: [mydomain.com]
                        routes:
                        - to: 192.168.20.90/32
                          via: 192.168.10.1
                        - to: 192.168.20.91/32
                          via: 192.168.10.1

                ens180:
                        dhcp4: no
                        addresses: [192.168.20.145/24]
                        routes:
                        - to: 192.168.20.0(24
                          via: 192.168.20.1


----8x---- cut here ----- 8X-------
As you can see, the machine has two interfaces on different subnets, but needs to talk to two machines on the other interface's VLAN via the primary interface. Therefore the static host routes.
Don't ask why. It is a long&sad story that does not make a lot of sense.

But there you have it:
In the routes section just add the - to:  and via: statements to specify the target subnet and the gateway to be used to get there.

Engage!

To activate the change, issue the
netplan apply
command (with sudo, of course if needed.)