How to Add Static Route Using Netplan on Ubuntu

Written by: Bagus Facsi Aginsa
Published at: 13 Oct 2021

In ubuntu 18.04 or later, we have an application called netplan to manage our network. The configuration file is located in /etc/netplan directory. In this tutorial, We will be using netplan to add the static route to make it persistent.

Check Prerequisite

  1. Netplan is installed by default on Ubuntu 18.04 or Later. So you just need an Ubuntu OS to configure this.
  2. Sudo privileges, make sure you have root access in your VM. The firewall must be set by the root user. So, before we start, make sure you execute sudo su.

Use Case

To demonstrate how we can add static route using netplan, we will use this use case:

                  ___                        _________
                 |   |                      |         |
  internet ------| A |--- ---| server1 |
                 |___|                      |_________|
                 router                          |
                     _________                  _|_
                    |         |                |   |
                    | server2 |----------------| B |                
                    |_________|                |___|

In this use case, we will assume that your main server is server1. Your server connects to the internet using the network via eth0. But your server must also be connected to server2 that can only be routed from network via eth1.

In this case, you must add a static route configuration on your server to specifically route the traffic to server2.

Your eth0 ip address is

Your eth1 ip address is

The router A ip address is

The router B ip address is

The server2 ip address is

Netplan Configuration

To configure the network in our server, first, open the netplan configuration file:

nano /etc/netplan/50-cloud-init.yaml

In my server the file name is 50-cloud-init.yaml, maybe it is different in your case. Just open the yaml file located on the /etc/netplan directory.

This is how the configuration is implemented to suits the use case we mention above:

            - to:
    version: 2

Note that on the eth1 we do not configure gateway4. It is a general rule that we must not use more than 1 gateway configuration as it will confuse the server.

In this configuration, we use the routes directive on the eth1 configuration block. This will make all the traffic routed via the gateway ( except the traffic to:, it will be routed via:

Apply Configuration

After editing the file, use this command to apply the new configuration:

netplan apply

If successful, then your new configuration is applied on the Server. If fail, netplan will show you where is the error.

Check Static Route

To check your configuration, you can execute this command

ip route list

The output will be something like this:

default dev eth0 scope link dev eth0 proto kernel scope link src dev eth1 proto kernel scope link src via dev eth1 proto static

You can see that the configuration we apply on netplan is written on the ip routing table

Temporary Add Static Route

If you want to add static route temporary, you don’t need to use netplan. Use the ip route add command instead. This is the command to add a static route:

ip route add via dev eth1 proto static

This configuration will not persist on the routing table and will be lost when the server reboots.

That’s it! Congratulation you just add a static route using netplan as well as ip route add on Ubuntu!