Typically, the default setting of Ubuntu is to obtain an IP address automatically via DHCP server, which is good for desktop system, because it does not require any changes.
However, it is always recommended to assign a static IP address to the Ubuntu servers, as the static IP address will be persistent across the reboots.
Ubuntu 17.10 and later uses ‘Netplan’ as the default network management tool. Therefore, configuring the IP address on the Ubuntu 20.04 system is different than the older version of Ubuntu.
Refer the following guide to add secondary IP on Ubuntu system:
In this guide, we will describe how to configure static IP address in Ubuntu 20.04, which also applies to Ubuntu 18.04.
What is Netplan?
Netplan is a utility developed by Canonical (Ubuntu) that is designed to easily configure the network on a Linux system.
It is based on the YAML based configuration, which greatly simplifies the network configuration process.
To configure a network interface, simply create a YAML description of the required network interfaces, and Netplan will generate all the necessary configuration for the chosen renderer tool.
You can find the Netplan network configuration files at '/etc/netplan/*.yaml'
. Netplan currently supports the following back-end renderers like 'NetworkManager'
& 'Systemd-networkd'
.
NetworkManager is mostly used on Desktop machines, whereas the Systemd-networkd is used on servers.
Disabling cloud-init configuration
Make sure that the network interface is not managed by ‘cloud-init’. To disable this, append the following line to the file- ‘/etc/cloud/cloud.cfg.d/subiquity-disable-cloudinit-networking.cfg’.
$ sudo echo "network: {config: disabled}" >> /etc/cloud/cloud.cfg.d/subiquity-disable-cloudinit-networking.cfg
Once you have added, you can confirm this by running the following command:
$ cat /etc/cloud/cloud.cfg.d/subiquity-disable-cloudinit-networking.cfg
Identifying Interfaces & IP information
Ethernet interfaces are identified by the system using predictable network interface names. These names can appear as ‘eno1’ or ‘enp0s25’. However, the name may be different in some systems.
Use the ip command to quickly identify all the available Ethernet interfaces on your system. You can see that a dynamic IP is automatically assigned to the "enp0s3"
interface as per the below output :
$ ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff inet 192.168.1.101/24 brd 192.168.1.255 scope global dynamic enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe97:132e/64 scope link valid_lft forever preferred_lft forever
Configuring Static IP Address on Ubuntu server
Netplan configuration files are available at ‘/etc/netplan’ directory. You might find a default netplan configuration file namely ’01-netcfg.yaml’ or ’50-cloud-init.yaml’ or ’00-installer-config.yaml’, but in your system it may be different.
The default netplan configuration file would be similar to the one below, if IP address is configured via DHCP :
$ cat /etc/netplan/00-installer-config.yaml
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: yes
To assign a static IP address ‘192.168.1.151’ to ‘enp0s3’ interface, edit the file as follows. Once you make changes, save and close the file.
Since this is a Yaml file, you must follow the correct indentation when making changes to the file. If the syntax is incorrect, changes will not be applied.
$ vi /etc/netplan/00-installer-config.yaml network: version: 2 renderer: networkd ethernets: enp0s3: dhcp4: no addresses: [192.168.1.151/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 8.8.4.4]
Run the following command to apply the changes:
$ sudo netplan apply
Verify the new IP information by running the IP command:
$ ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff inet 192.168.1.151/24 brd 192.168.1.255 scope global enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe97:132e/64 scope link valid_lft forever preferred_lft forever
Wrapping Up
This article explained how to configure a static IP in Ubuntu 20.04 using Netplan. The same procedure can be used in other Ubuntu versions where Netplan is used as a network management tool.
If you found this article helpful, please do share with your friends and spread the knowledge. Please feel free to comment below if you have any queries/concerns. We will get back to you as soon as we can. Happy learning!