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.
- Netplan is installed by default on Ubuntu 18.04 or Later. So you just need an Ubuntu OS to configure this.
- 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
To demonstrate how we can add static route using netplan, we will use this use case:
___ _________ | | | | internet ------| A |--- 192.168.1.0/24 ---| server1 | |___| |_________| router | | 10.1.1.128/26 | _________ _|_ | | | | | server2 |----------------| B | |_________| |___| router
In this use case, we will assume that your main server is
server1. Your server connects to the internet using the
192.168.1.0/24 network via
eth0. But your server must also be connected to
server2 that can only be routed from
10.1.1.128/25 network via
In this case, you must add a static route configuration on your server to specifically route the traffic to
eth0 ip address is
eth1 ip address is
A ip address is
B ip address is
server2 ip address is
To configure the network in our server, first, open the netplan configuration file:
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
This is how the configuration is implemented to suits the use case we mention above:
network: ethernets: eth0: addresses: - 192.168.1.100/24 gateway4: 192.168.1.1 nameservers: addresses: - 188.8.131.52 eth1: addresses: - 10.1.1.130/26 routes: - to: 10.1.2.100 via: 10.1.1.129 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 (
192.168.1.1) except the traffic
to: 10.1.2.100, it will be routed
After editing the file, use this command to apply the new configuration:
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 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100 10.1.1.128/26 dev eth1 proto kernel scope link src 10.1.1.130 10.1.2.100 via 10.1.1.129 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 10.1.2.100 via 10.1.1.129 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!