Since Ubuntu 17.10, the canonical team uses the ‘Netplan‘ as default network configuration utility, which replaces the old version through the ‘/etc/network/interfaces’ configuration file.
Netplan is a utility that allows you to configure the network using the YAML script on the Linux system. Using the Netplan, you can easily group your network configuration based on their type, including ethernets, wifis, modems, and bridges. Also, it allows you to add a new IP address permanently without a reboot.
It supports two different networking backend, the NetworkManager that used by the Ubuntu Desktop, and the systemd-networkd that used by Ubuntu Server.
What you will learn:
- How to setup static IP address on Ubuntu 20.04 using Netplan
- How to use the ‘netplan’ and ‘ip’ commands for managing network
What you need:
- Ubuntu 20.04 Server
- Root privileges
- IP Address and Gateway
Before going any further, log in to your server and run the sudo command below to get the root privileges.
Next, you will learn how to set up the static IP address on Ubuntu 20.04 server using Netplan.
Step 1 – Check Network Interfaces using “ip” Command
Firstly, check all available network interfaces on your Ubuntu system using the following command.
ip link show
Now you will get the result like the picture below.
As can be seen, there are three different network interfaces:
- lo as a default network interface on every Linux system.
- ens3 interface is “UP“, with the mac address “56:00:02:d5:cb:97“
- ens7 interface is “DOWN“, with the mac address “5a:00:02:d5:cb:97“
Step 2 – Add Static IP Address on Ubuntu 20.04
In this case, you will learn how to set up the static IP address for the interface ‘ens7‘ with the IP address ‘172.16.0.3‘, the network gateway ‘172.16.0.1‘, and using the Cloudflare DNS server ‘188.8.131.52‘ and ‘184.108.40.206‘.
Also, you will learn how to use the ‘match’ mapping option to select the physical devices by various hardware properties, such as name, mac address, and driver.
Go to the ‘/etc/netplan’ directory and create a new YAML configuration file ’10-ens7.yaml’ using vim editor.
cd /etc/netplan/ vim 10-ens7.yaml
Paste the following configuration into it.
network: version: 2 renderer: networkd ethernets: ens7: addresses: [172.16.0.3/12] dhcp4: no match: macaddress: 5a:00:02:d5:cb:97 gateway4: 172.16.0.1 nameservers: addresses: [220.127.116.11, 18.104.22.168]
Save and close.
As a result, you’ve added the static IP address for the interface ‘ens7‘.
Step 3 – Apply New Static IP Address using Netplan
Before applying the new configuration, you can verify the Netplan configuration using the netplan command below.
sudo netplan try
Now press ‘Enter’ on your keyboard to test the Netplan configuration. Once your Netplan YAML configuration is correct, you will get the following response.
Do you want to keep these settings? Press ENTER before the timeout to accept the new configuration. Changes will revert in 113 seconds. Configuration accepted.
As a result, the configuration is correct, and you’re to apply it to the system.
Next, apply the new Netplan configuration for static IP address using the command below.
sudo netplan apply
Now make sure you’ve no error.
As a result, you’ve applied the new Netplan configuration to the Ubuntu system, and you will get the New IP Address on the interface ‘ens7’.
Step 4 – Verify IP Address on Ubuntu 20.04
To verify the static IP address configuration on the Ubuntu system, you can use the ‘ip’ command line, or using the built-in ‘netplan’ command.
– Verify static IP Address using “ip” command
By default, the ip command is available on all Linux distros. It’s a command utility that allows you to show and manipulate routing, network devices, interfaces, tunnels.
Check and verify the new static IP address fo interface ‘ens7’ using the ip command below.
ip addr show ens7
As a result, you will get details status, IP address, netmask, and mac address, of the ‘ens7’ network interface.
As can be seen, the interface ‘ens7‘ is UP with the new IP address ‘172.16.0.3‘, as defined as the configuration on top.
– Additional: Verify DHCP using “netplan” command
The Netplan comes with the default command ‘netplan’ that allows you to check DHCP leases IP address.
If you’re using DHCP on your Netplan configuration, check and verify the IP address configuration using the netplan command below.
sudo netplan ip leases your-interface
As a result, you will get details configuration of the ‘ens3’ interface as below.
As can be seen, the interface ‘ens7‘ has the correct IP address as the configuration on top. The IP address ‘172.16.0.3‘ with the default gateway ‘172.16.0.1‘.
Finally, you’ve successfully set up the static IP address on Ubuntu 20.04 server using Netplan.