How to add static route with netplan on Ubuntu 22.04 Jammy Jellyfish Linux

The purpose of this tutorial is to cover the step by step instructions to add a new static route via Netplan on Ubuntu 22.04 Jammy Jellyfish.

Static routes are necessary when you have two or more networks that your computer needs to send traffic to. This is not a normal scenario for a home network, but is rather common in offices, schools, etc.

Normally, all network traffic is routed to the default gateway, which is a router that will then determine where to send the data to next. When adding more static routes, it adds more gateways. It is like telling your computer to send data destined to a certain network to a different router than the default.

In this tutorial you will learn:

  • How to add permanent static route
  • How to apply new netplan configuration
  • How to check static routes from command line
Netplan configuration to add a static route on Ubuntu 22.04 Jammy Jellyfish
Netplan configuration to add a static route on Ubuntu 22.04 Jammy Jellyfish
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu 22.04 Jammy Jellyfish
Software Netplan (installed by default)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to add static route with Netplan on Ubuntu 22.04 step by step instructions



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!

  1. The first step is to open a command line terminal and use nano or your preferred text editor to open the main netplan configuration file with root permissions:
    $ sudo nano /etc/netplan/50-cloud-init.yaml
    
  2. We recommend running the following command to show your currently configured routes before proceeding. Note the output somewhere, because it will come in handy later if we need to do any troubleshooting.
    $ ip route s
    default via 10.0.2.2 dev enp0s3 proto dhcp metric 100 
    10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15 metric 100 
    169.254.0.0/16 dev enp0s3 scope link metric 1000
    
  3. Find the configuration stanza related to the network interface to which you wish to add the static route. In this example we will add the the static route to the destination network subnet 172.16.0.0/24 via the network gateway 192.168.1.100 on the interface enp0s3. You may copy and paste this example and edit it as needed:
    # This file is generated from information provided by
    # the datasource.  Changes to it will not persist across an instance.
    # To disable cloud-init's network configuration capabilities, write a file
    # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
    # network: {config: disabled}
    
    network:
        ethernets:
            enp0s3:
                dhcp4: false
                addresses: [192.168.1.202/24]
                nameservers:
                  addresses: [8.8.8.8,8.8.4.4,192.168.1.1]
                routes:
                - to: 172.16.0.0/24
                  via: 192.168.1.100
        version: 2
  4. Once you made all required changes to add the static route all the new netplan configuration, exit the file and save changes. Then, use the below command to apply the configuration:
    $ sudo netplan apply
    
  5. Check all static routes available on your Ubuntu 22.04 system:
    $ ip route s
    default via 10.0.2.2 dev enp0s3 proto static metric 100 
    10.0.2.2 dev enp0s3 proto static scope link metric 100 
    169.254.0.0/16 dev enp0s3 scope link metric 1000 
    172.16.0.0/24 via 192.168.1.100 dev enp0s3 proto static metric 100
    192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.202 metric 100
    

    You should see the static route that you configured in your Netplan configuration, which we have put in bold text in the output above.

  6. Applying this configuration may have also made your default route disappear, which is what happened to us when testing this configuration. This should be immediately noticeable if you can’t access the internet anymore or if you are cut off from previously accessible networks. If this is the case, use the information gained from the output in step 2 to add your default route back to the Netplan configuration. For us, we need to set the default gateway to 10.0.2.2. Here is what that configuration looks like:
    # This file is generated from information provided by
    # the datasource.  Changes to it will not persist across an instance.
    # To disable cloud-init's network configuration capabilities, write a file
    # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
    # network: {config: disabled}
    
    network:
        ethernets:
            enp0s3:
                dhcp4: false
                addresses: [192.168.1.202/24]
                nameservers:
                  addresses: [8.8.8.8,8.8.4.4,192.168.1.1]
                routes:
                - to: 172.16.0.0/24
                  via: 192.168.1.100
                - to: default
                  via: 10.0.2.2
        version: 2


Closing Thoughts

In this tutorial, you saw how to add a new static route to Ubuntu 22.04 Jammy Jellyfish via applying a Netplan configuration. We have shown you the YAML syntax necessary to add a new static route and maintain your default gateway for all other traffic that does not match a route.