Ubuntu Server Set Static IP Address with Netplan

Ubuntu Server is one of the most popular open source sever operating systems. Ubuntu Server can be used in production without any hassle. In my previous article, I discussed how to install Ubuntu Server’s latest version (Ubuntu Server 20.04) with LVM. In this article, we are going to see how to assign static IP address in Ubuntu Server with Netplan network management tool.

Netplan

Since Ubuntu Server 17.10, Netplan is being used as the default network management tool instead of ifconfig and its configuration file/etc/network/interfaces which was used to configure the network in the previous version.

YAML syntax (with a .yaml file extension) is used to write Netplan configuration files. So, we need to create a YAML description to configure network interfaces with Netplan. Netplan will generate the required configuration files for the chosen renderer tool.

There are two renderers in Netplan, NetworkManager and Systemd-networkd. NetworkManager is used on Desktop platform while the Systemd-networkd is used on server operating system.


Configuring Static IP address on Ubuntu Server

Before going to assign static IP address with Netplan, we have to know the name of the ethernet interface. To know interface name, issue the ip link command as shown below.

ip link

All the available network interfaces will be shown with the above command. In this example, interface name is ens160.

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 
ink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 
link/ether 00:50:56:9d:d9:fe brd ff:ff:ff:ff:ff:ff

Netplan configuration files are stored in the /etc/netplan directory. Usually one (.yaml) configuration file can be found in this directory. The name of the file may differ from setup to setup. In my setup, the configuration file name is 00-installer-config.yaml.

To assign a static IP address on the network interface, open the YAML configuration file with vim text editor. If you find any previous configuration, remove everything and put the below configuration options and then save the configuration file.

sudo vim /etc/netplan/00-installer-config.yaml 
# This is the network config written by 'subiquity'
network:
renderer: networkd
ethernets:
ens160:
addresses: [172.22.2.100/24]
gateway4: 172.22.2.1
nameservers:
search: [webserver]
addresses: [8.8.8.8, 8.8.4.4]

version: 2

After saving the configuration file, issue the following command to apply the configuration.

sudo netplan apply

If everything is OK, you will now be able to get internet access.

Explanation of YAML Configuration Options

At first the above configuration options may appear a little bit confusing. So, let’s explain the code in a short.

Netplan configuration file starts with the network key. There are two required elements of the network key. The first one is the version of the network configuration format and the second one is the device type. The device type can be ethernets, bonds, bridges, or vlans. The network key has also another option named renderer. For the Ubuntu server, the renderer will be networkd.

The device type (ethernets) can have one or more network interfaces. In this example, we are assigning only one static IP address on ens160.

Each device type contains addresses, gateway4 and nameservers options to assign IP address, gateway and DNS servers which are self introductory.

When editing Yaml files, make sure you follow the YAML code indent standards. If the syntax is not correct, the changes will not be applied.

How to configure static IP address in Ubuntu Server has been discussed in this article. I hope you will now be able to assign static IP address on Ubuntu Server interface. However, if you face any confusion to assign static IP address, feel free to discuss in comment or contact me from Contact page. I will try my best to stay with you.

Why not a Cup of COFFEE if the solution?

ubuntu-server-set-static-ip-address-with-netplan

ABU SAYEED

I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Follow Me: Facebook, Twitter and Linkedin.

Your name can also be listed here. Have an IT topic? Submit it here to become a System Zone author.

Leave a Reply

Your email address will not be published. Required fields are marked *

*