How to Configure Networking In Linux With Netplan

It’s out with the old configuration steps and in with the new

Abstract drawing on a grid background.

Ubuntu Server 18.04 arrived this year and it has made some pretty significant changes to how admins work with the platform. One very important change (one that might trip up many an old-school user) is how networking is configured. Gone is the old method of configuring networking in /etc/network/interfaces. To configure networking in the latest iteration, say hello to Netplan.

What is Netplan?

Netplan is a utility for configuring networking on a Linux system, which uses YAML descriptions for each network interface found on a server or desktop. Netplan reads the YAML description files found within /etc/netplan. During the early booting stages of the operating system, Netplan will then generate (from the user-created YAML files) the necessary configuration files in the /run directory, so that control of each networking device is handed off to a particular networking daemon. In other words, you create a YAML description file for a network interface that Netplan can read and use to make that particular interface work.

Don’t panic. Although network configuration on a Netplan-enabled server is vastly different than what it was in previous iterations, it’s not all that challenging. It is, however, specific. Let’s take a look at how to configure networking, via Netplan, on a Ubuntu Server 18.04 installation. As you might expect, configuring networking on a Linux server is typically done via the command line, so get ready to do a bit of typing.

Configuring a Static IP Address with Netplan

Locating your network interface name with the ip a command.

Since we’re talking about a server installation, we want to configure the server for a static IP address. There is one thing you need to know about the Netplan YAML file. You must adhere to proper code indent for each line of the block. That doesn’t mean you must indent specific lines to a specific point. What it means is if you indent the first line of a block three spaces, you must indent the rest of the block three spaces. If you do not adhere to this, Netplan will error out.

With that said, let’s configure.

Before we make any changes, we need to know the name of our network interface. To do that, issue the command:

You should see the name of the network interface listed (such as ens5 or ens3). With that interface name in hand, you’re ready to configure.

Out of the box, you’ll probably find a single YAML file in /etc/netplan. That file will be named either 01-netcfg.yaml or 50-cloud-init.yaml. We want to edit the 01-netcfg.yaml file. If it doesn’t exist, create it with the command:

sudo touch 01-netcfg.yaml

If the file does exist, edit it with the command:

sudo nano 01-netcfg.yaml

Your default file may look like this:

network:
version: 2
renderer: networkd
ethernets:
ens5:
dhcp4: true

The above configuration is set for DHCP. Because this is a server, we want to make use of a static IP address (so the IP address never changes). What we must edit is everything below the ens5 line. The first thing to do is set dhcp4 to false, like so:

dhcp4: false
A Netplan YAML file to configure a static IP address.

Next we’re going to add entries for addresses, gateway, and DNS nameservers. Say, for instance, you want to set the server to IP address 192.168.1.206, with a gateway of 192.168.1.254, and DNS servers of 8.8.4.4 and 8.8.8.8. Those entries will be added below the dhcp4 entry and look like so:

addresses: [192.168.1.206/24]
gateway4: 192.168.1.254
nameservers:
addresses: [8.8.4.4,8.8.8.8]
Remember, you must be consistent with your indenting, otherwise Netplan will error when reading the file. Also note that you do not set the netmask in the same fashion as was done via the interfaces file. Instead of an address and netmask being set like so:
address = 192.168.1.206netmask = 255.255.255.0

Both configurations are handled with the single line:

addresses: [192.168.1.206/24]

Save and close that file with the keyboard combination of Ctrl+x. Once you’ve saved that file, you must make Netplan aware of the changes. To have Netplan reread and apply the configuration files, issue the command:

sudo netplan apply
Debugging the netplan command output.

You shouldn’t see any errors (or output at all). If you do see errors, add the debug option (which will give you output as Netplan attempts to apply the configuration file) like so:

sudo netplan --debug apply

Using the --debug option should give you enough information to help you troubleshoot your YAML file. If you receive no output, issue the command:

ip a

The output of the above command should reflect the changes you just made. Your server now has a static IP address, thanks to Netplan.