How to add a static route with netplan on Ubuntu 20.04 step-by-step instructions

  1. The first step is to open the main netplan configuration file using administrative privileges:
    $ sudoedit /etc/netplan/50-cloud-init.yaml


  2. 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 static route to the destination network subnet 172.16.0.0/24 via the network gateway 192.168.1.100 on the interface enp0s3. Example:

    # 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]
    gateway4: 192.168.1.1
    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

  3. Once you made all required changes to add the static route all the new netplan configurations using the bellow command:
    $ sudo netplan apply
    
  4. Check all static routes available on your Ubuntu system:
    $ ip route s
    default via 192.168.1.1 dev enp0s3 proto static 
    172.16.0.0/24 via 192.168.1.100 dev enp0s3 proto static 
    192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.202
Was this answer helpful? 0 Users Found This Useful (2 Votes)