How to add static route with netplan on Ubuntu 20.04 Focal Fossa Linux - LinuxConfig.org

In this tutorial you will learn how to permanently add a static route on Ubuntu 20.04 Focal Fossa Linux using netplan.
This is a companion discussion topic for the original entry at https://linuxconfig.org/how-to-add-static-route-with-netplan-on-ubuntu-20-04-focal-fossa-linux

I have 20.04 Focal Fossa, which I recently upgraded from 18.04, which began originally as a 16.04 machine!

My /etc/netplan folder is empty.
image

Thinking I might still be using legacy pre-18 /etc/network I checked my interfaces file, but this only has local setup:
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

I need to add this route:
route add -host 192.168.152.20 gw 192.168.151.70 wlan0

Any suggestions?

Hi TerribleTadpole,

Welcome to our forums.

Since you upgraded from 16.04, just to make it clear, do you use NetworkManager on this system? If not, what service handles the networking stack?

I haven’t had to customise the network on this machine before. Running status --list-all shows:

 [ + ]  network-manager
 [ + ]  networking

The thing that throws me is that neither /etc/network/interfaces nor /etc/netplan/ contain what the answers on the Internet say they will, so I have nowhere to put the desired configuration.

So let’s assume it is NetworkManager that handles networking. Can you please post the output of the following command:

$ sudo systemctl status NetworkManager.service

This would verify the assumption.

As follows:

â—Ź NetworkManager.service - Network Manager
     Loaded: loaded (/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2020-10-23 22:50:09 AEDT; 5 days ago
       Docs: man:NetworkManager(8)
   Main PID: 1140 (NetworkManager)
      Tasks: 3 (limit: 19009)
     Memory: 17.7M
     CGroup: /system.slice/NetworkManager.service
             └─1140 /usr/sbin/NetworkManager --no-daemon

(apologies for the long delay)

Ooh! Got it! Thanks to @sandmann for helping me find for certain how my network was being managed. The answer was this command:

sudo nmcli connection modify Gargravarr +ipv4.routes "192.168.152.20/32 192.168.151.70"

To explain:

  • Gargravarr is the name of my home WiFi connection in my WiFi connections list. I found a complete list of all connection names in /etc/NetworkManager/system-connections
  • +ipv4.routes means to add the next parameter to the IPv4 static routes in the named connection
  • “192.168.152.20/32 192.168.151.70” is my static route specifying host address first then the gateway address to reach that host.

To load the updated configuration I used

sudo nmcli connection up Gargravarr

The nice thing about this is that if I disconnect from Gargravarr then the static route disappears, and then reappears later if I reconnect.

1 Like