Ubuntu – What is netplan?


To totally unlock this section you need to Log-in

Ubuntu 18.04 LTS has switched to Netplan for configuring network interfaces. Netplan is based on YAML based configuration system that makes configuration process very simple.

Using netplan gives a central location to describe simple to complex networking configurations that function from Desktop to Server and from Cloud to IoT environments. Specifically, for systems with networkd, this relieves the user from having to configure up to three different files per device or configuration.

The Old Way

Until now, if you’re an Ubuntu/Debian user, you could either configure the network connection via the desktop GUI or from within the /etc/network/interfaces file, usually used by those that implement a server or simply want to use Terminal or a no-GUI version of the operating system.

The configuration is incredibly easy and never failed to work. The configuration within that above specified file looks something like the following:

auto enp10s0
iface enp10s0 inet static
address 192.168.1.162
netmask 255.255.255.0
gateway 192.168.1.100
dns-nameservers 1.0.0.1,1.1.1.1

After modified the interfaces file like above we usually save and close that file. Finally, restart networking with the command:

sudo systemctl restart networking

If we are not using a non-systemd distribution, you could restart networking the old fashioned way like so:

sudo /etc/init.d/networking restart

The network will restart and the newly configured interface is good to go. That’s how it’s been done for years.

The New Way

Create and Apply a configuration

Users should save configurations under /etc/netplan with a .yaml extension (e.g. /etc/netplan/config.yaml).

Then run sudo netplan apply and the configuration is parsed, written, and applied to the system. Since the file is written to disk the the configuration will persist between reboots.

If you tell Netplan to use NetworkManager, all interface configuration control is handed off to the GUI interface on the desktop. The NetworkManager program itself hasn't changed; it's the same GUI-based interface configuration system you've likely used for years.

If you tell Netplan to use networkd, systemd itself handles the interface configurations. Configuration is still done with Netplan files, but once "applied", Netplan creates the back-end configurations systemd requires. The Netplan files are vastly different from the old /etc/network/interfaces file, but it uses YAML syntax, and it's pretty easy to figure out.

As first step we need to find the available network cards on your system. To do this we can run any one of the below commands in the terminal to get a list of network interfaces available on your system:

ifconfig -a

OR

ip a

At this point we will see something like the following, as output:

Ubuntu - What is netplan?

After this step we will check the path of netplan available configuration files. The Netplan default configuration file is under the directory /etc/netplan. We can find that using the following command:

$ ls /etc/netplan/

To view the content of Netplan network configuration file, run the following command:

$ cat /etc/netplan/*.yaml

The layout of the file looks like this:

network:
    Version: 2
    Renderer: networkd
    ethernets:
       DEVICE_NAME:
          Dhcp4: yes/no
          Addresses: [IP/NETMASK]
          Gateway: GATEWAY
          Optional: true
          Nameservers:
             Addresses: [NAMESERVER, NAMESERVER]

Where:

  • DEVICE_NAME is the actual device name to be configured.
  • yes/no is an option to enable or disable dhcp4.
  • IP is the static IP address for the device.
  • NETMASK is the netmask for the IP address.
  • GATEWAY is the address for your gateway.
  • NAMESERVER is the comma-separated list of DNS nameservers.
  • OPTIONAL allow booting, if true, to occur without waiting for those interfaces to activate fully.

Here’s a sample .yaml file:

network:
    version: 2
    renderer: networkd
    ethernets:
       ens5:
       dhcp4: no
       addresses: [192.168.1.230/24]
       gateway4: 192.168.1.254
       nameservers:
          addresses: [8.8.4.4,8.8.8.8]

Edit the above to fit your networking needs. Save and close that file. Now you will need to open the configuration file in any editor: As we love using Nano editor to edit the configuration file, so we will run (in this example using nano, but we are free to use any other text editor we prefer, like vi or vim):

$ sudo nano /etc/netplan/*.yaml

After the proper modifications, run sudo netplan apply and the configuration is parsed, written, and applied to the system, as already said before.

Configuring DHCP

Even if you probably will not configure your server for DHCP, it’s always good to know how to do this. For example, you might not know what static IP addresses are currently available on your network. You could configure the device for DHCP, get an IP address, and then reconfigure that address as static.

To use DHCP with Netplan, the configuration file would look something like this:

network:
    version: 2
    renderer: networkd
    ethernets:
       ens5:
       Addresses: []
       dhcp4: yes
       optional: true

Take note the some of the most important parameters are the rendered and the dhcp4 boolean value specified in the above example. Finally, save and close that file. Let's apply the configuration file with:

sudo netplan try

Netplan should succeed and apply the DHCP configuration. You could then issue the ip a command, get the dynamically assigned address, and then reconfigure a static address.

Configure static IP address

To manually configure an IP address, use the above configuration file syntax and add the IP address, Gateway, and DNS server information. Here you can see my configuration file for static IP addressing:

network:
    version: 2
    renderer: networkd
    ethernets:
       ens5:
           dhcp4: no
           addresses: [192.168.1.100/24]
           gateway4: 192.168.1.1
           nameservers:
           addresses: [192.168.1.1,8.8.8.8]

To use NetworkManager, you would need to install the Network Manager, sudo apt install network-manager, and then use renderer: NetworkManager in the netplan configuration file. Below an example using NetworkManager renderer:

network:
    version: 2
    renderer: NetworkManager
    ethernets:
       ens5:
           dhcp4: no
           addresses: [192.168.1.100/24]
           gateway4: 192.168.1.1
           nameservers:
           addresses: [192.168.1.1,8.8.8.8]

Configure static IP address on Wifi interface

The below configuration is an example configuring a WLAN interface

  • wlan0: Wifi interface device name
  • MyWiFi: My Wifi SSID
  • MyPass: Wifi Password
network:
  version: 2
  renderer: NetworkManager
  wifis:
          wlan0:
               dhcp4: no
               addresses: [192.168.1.100/24]
               gateway4: 192.168.1.1
               nameservers:
                   addresses: [192.168.1.1,8.8.8.8]
               access-points:
                     MyWiFi:
                       password: MyPass

Configuration of a failover IP with Netplan

To configure a failover IP, you have to edit the file /etc/netplan/01-netcfg.yaml and configure a static networking for your server. The IP addresses have to be written with their CIDR notation. The netmask is /24 for the principal IP of the server and /32 for each failover IP. Your configuration file should look like in the following example:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp1s0f0:
      addresses: [163.172.123.123/24, 212.83.123.123/32]
      gateway4: 163.172.123.1
      nameservers:
        addresses: [ "62.210.16.6", "62.210.16.7" ]

Once you have edited and saved the file you can reload the configuration with the following command: sudo netplan apply.

Configuration of a failover IP in a virtual machine

When you configure a failover IP inside a virtual machine, you have to specify the route that will be used by the VM. Your configuration file should look like in the following example:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses: [fail.over.ip.address/32]
      gateway4: 62.210.0.1
      nameservers:
        addresses: [62.210.16.6, 62.210.16.7]
      routes:
      - to: 62.210.0.1/32
        via: fail.over.ip.address
        scope: link

Test a configuration

If a user wishes to verify that a configuration file is valid it is recommended to first pass the configuration through a YAML syntax validator. This is done to validate the syntactic validity of the file. Run the following command as sudo to test configurations:

$ sudo netplan try

Next, a user can run sudo netplan generate to validate the configuration is correctly parsed by netplan and written to disk. However, be warned that once generated the new configuration is written to disk the configuration is activated on next boot. Therefore, backing up a current working configuration is highly recommended.

Hierarchy of configuration files

Configuration files can exist in three different locations with the precedence from most important to least as follows:

/run/netplan/*.yaml
/etc/netplan/*.yaml
/lib/netplan/*.yaml

Alphabetically later files, no matter what directory in, will amend keys if the key does not already exist and override previous keys if they do.

Bring interfaces up or down

Previously users were used to using the ifconfig command. Users should now familiarize themselves with the more powerful ip command. Manually modifying network devices is now accomplished via the ip command. As an example to bring up an interface and bring it back down:

ip link set enp3s0 up
ip link set enp3s0 down

See man ip for more information on how to manipulate the state of routing, network devices, interfaces and tunnels.

Advanced Network Configurations

The configuration formats are different, but it's still possible to do more advanced network configurations with Netplan:

Bonding:

network:
  version: 2
  renderer: networkd
  bonds:
    bond0:
      dhcp4: yes
      interfaces:
        - enp2s0
        - enp3s0
      parameters:
        mode: active-backup
        primary: enp2s0

The various bonding modes (balance-rr, active-backup, balance-xor, broadcast, 802.3ad, balance-tlb and balance-alb) are supported.

Bridging:

network:
  version: 2
  renderer: networkd
  bridges:
    br0:
      dhcp4: yes
      interfaces:
        - enp4s0
        - enp3s0

Bridging is even simpler to set up. This configuration creates a bridge device using the two interfaces listed. The device (br0) gets address information via DHCP.

Prevent waiting for interface

Interfaces that are not required for booting or should not be waited on during boot should have the optional: true key added to them. This will prevent long delays in booting for interfaces that may not come up.

Get the current system address

Netplan is only meant to translate configuration into the appropriate files for the backend specified. It does not display IP information by itself, since it does not manage those by itself.

To get the most accurate state of the IP addresses for the system using the ip addr command.

Find the current DNS servers

To determine the current DNS servers used by the system run systemd-resolve --status and look for the DNS Servers: entry to see what DNS server is used.

Ubuntu - What is netplan?

Deconfigure an interface

To deconfigure an interface, remove the configuration for the device from the netplan .yaml file and run sudo netplan apply.

If the interface is not configured in a .yaml file in /etc/netplan, it will not be configured at boot. To remove addresses manually, a user can run ip address del <address> dev <interface>.