Netplan is a new command-line network configuration utility introduced in Ubuntu 17.10 to manage and configure network settings easily in Ubuntu systems. It allows you to configure a network interface using YAML abstraction. It works in conjunction with the NetworkManager and systemd-networkd networking daemons (referred to as renderers, you can choose which one of these to use) as interfaces to the kernel.
It reads network configuration described in /etc/netplan/*.yaml and you can store configurations for all your network interfaces in these files.
In this article, we will explain how to configure a network static or dynamic IP address for a network interface in Ubuntu 18.04 using Netplan utility.
List All Active Network Interfaces on Ubuntu
First, you need to identify the network interface you are going to configure. You can list all attached network interfaces on your system using the ifconfig command as shown.
$ ifconfig -a
From the output of the above command, we have 3 interfaces attached to the Ubuntu system: 2 ethernet interfaces and the loop back interface. However, the
enp0s8 ethernet interface has not been configured and has no static IP address.
Set Static IP Address in Ubuntu 18.04
In this example, we will configure a static IP for the
enp0s8 ethernet network interface. Open the netplan configuration file using your text editor as shown.
Important: In case a YAML file is not created by the distribution installer, you can generate the required configuration for the renderers with this command.
$ sudo netplan generate
In addition, auto generated files may have different filenames on desktop, servers, cloud instantiations etc (for example 01-network-manager-all.yaml or 01-netcfg.yaml), but all files under /etc/netplan/*.yaml will be read by netplan.
$ sudo vim /etc/netplan/01-netcfg.yaml
Then add the following configuration under the
enp0s8: dhcp4: no dhcp6: no addresses: [192.168.56.110/24, ] gateway4: 192.168.56.1 nameservers: addresses: [188.8.131.52, 184.108.40.206]
- enp0s8 – network interface name.
- dhcp4 and dhcp6 – dhcp properties of an interface for IPv4 and IPv6 receptively.
- addresses – sequence of static addresses to the interface.
- gateway4 – IPv4 address for default gateway.
- nameservers – sequence of IP addresses for nameserver.
Once you have added, your configuration file should now have the following content, as shown in the following screenshot. The first interface
enp0s3 is configured to use DHCP and
enp0s8 will use a static IP address.
The addresses property of an interface expects a sequence entry for example [192.168.14.2/24, “2001:1::1/64”] or [192.168.56.110/24, ] (see netplan man page for more information).
# This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: enp0s3: dhcp4: yes enp0s8: dhcp4: no dhcp6: no addresses: [192.168.56.110/24, ] gateway4: 192.168.56.1 nameservers: addresses: [220.127.116.11, 18.104.22.168]
Save the file and exit. Then apply the recent network changes using following netplan command.
$ sudo netplan apply
Now verify all the available network interfaces once more time, the
enp0s8 ethernet interface should now be connected to the local network, and have an IP addresses as shown in the following screenshot.
$ ifconfig -a
Set Dynamic DHCP IP Address in Ubuntu
To configure the
enp0s8 ethernet interface to receive an IP address dynamically through DHCP, simply use the following configuration.
# This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: enp0s8: dhcp4: yes dhcp6: yes
Save the file and exit. Then apply the recent network changes and verify the IP address using following commands.
$ sudo netplan apply $ ifconfig -a
From now on your system will get an IP address dynamically from a router.
You can find more information and configuration options by consulting the netplan man page.
$ man netplan
Congratulations! You’ve successfully configured a network static IP addresses to your Ubuntu servers. If you have any queries, share them with us via the comment form below.