Categories: TutorialsUbuntu

Adding persistent static routes on Ubuntu 18.04 and higher using netplan

When you need to access network devices located on a different network segment than the one you are on, you need to have a route set up so the networking stack knows how to get to the other network segment. This generally just points to your main gateway, but you may want to set up additional static routes, where you don’t want the traffic going through your main default gateway.

With Ubuntu versions prior to 18.04, you had to manually edit the /etc/network/interfaces file to set up persistent static routes.
(See “Adding persistent static routes on Ubuntu

Sponsored
for Ubuntu versions prior to 18.04)

With the introduction of Ubuntu 18.04, along came the netplan YAML based network configuration tool.

The netplan configuration files are located in the /etc/netplan folder.

Find the netplan config file

Run the following command to list the netplan configuration files.

$ ls /etc/netplan

You should see something like the following:

$ ls /etc/netplan
01-netcfg.yaml
$

or

$ ls /etc/netplan
50-cloud-init.yaml
$

In this guide we will be using the “01-netcfg.yaml” file, as that is the default for a bare metal installation of Ubuntu 18.04.

Edit the netplan configuration file

Open up your netplan config file in a text editor.

$ sudo nano /etc/netplan/01-netcfg.yaml

You will be presented with a default configuration similar to this:
(network interface names will differ machine to machine)

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: yes

Add the following lines to the bottom of the config section for the network interface you want the route to be assigned to.

      routes:
      - to: 192.168.44.0/24
        via: 192.168.0.1

The file should now look something like this:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: true
      routes:
      - to: 192.168.44.0/24
        via: 192.168.0.1

Note: With YAML configuration files it is critical to get the spacing/indentation/layout correct.

Sponsored

Make sure the “routes:” statement is aligned 2 spaces to the right of the parent interface name you are assigning the route to.

Save the configuration, and exit the editor.

Applying the configuration change

You can either apply the changes straight away, with the following command:

$ sudo netplan apply

or, if you want to test it first, and potentially roll back any changes, you can use the following command:

$ sudo netplan try

This option apply the changes, and provide a 120 timeout where by if you don’t accept the changes, they will revert back. This is useful to prevent you from locking yourself out of the system, if the network change didn’t work the way you were intending.
The message that will be displayed will look like this:

$ sudo netplan try
Do you want to keep these settings?


Press ENTER before the timeout to accept the new configuration


Changes will revert in 120 seconds

Simply press the ENTER key on the keyboard before the timeout expires, and the changes will have been applied.

Checking the routes have been added

You can confirm your routes have been added by issuing the following command:

$ ip route show 

You should see your newly added route appearing in the list of routes. It should appear something like this:

192.168.44.0/24 via 192.168.0.1 dev eno1 proto static

If you cant see your static route in the list of routes, you may have an issue with your configuration syntax, or the logic of your route isn’t correct. Netplan should provide any warnings for syntax errors. Check the syslog log file for further errors.

Check the syslog file for network related errors using the following command:

$ grep networkd /var/log/syslog

The post Adding persistent static routes on Ubuntu 18.04 and higher using netplan appeared first on Networking HowTos.

Ubuntu Server Admin

Recent Posts

How is Livepatch safeguarded against bad actors?

Canonical Livepatch is a security patching automation tool which supports reboot-less security updates for the…

4 hours ago

Accelerating data science with Apache Spark and GPUs

Apache Spark has always been very well known for distributing computation among multiple nodes using…

4 hours ago

Cut data center energy costs with bare metal automation

Data centers are popping up everywhere. With the rapid growth of AI, cloud services, streaming…

1 day ago

Build the future of *craft: announcing Starcraft Bounties!

Our commitment to building a thriving open source community is stronger than ever. We believe…

1 day ago

NodeJS 18 LTS EOL extended from April 2025 to May 2032 on Ubuntu

The clock was ticking: Node.js 18’s upstream End of Life (EOL) The OpenJS Foundation is…

1 day ago

Native integration now available for Pure Storage and Canonical LXD

June 25th, 2025 – Canonical, the company behind Ubuntu, and Pure Storage, the IT pioneer…

2 days ago