Manage Networking with Netplan on Debian / Ubuntu

In this guide, we want to teach you How To Manage Networking with Netplan on Debian / Ubuntu. You will learn Netplan configuration on Debian / Ubuntu.

Netplan is a utility developed by Canonical, the company behind Ubuntu. It provides a network configuration abstraction over the currently supported two “backend” systems, (or “renderer” in Netplan terminology): networkd and NetworkManager.

Using Netplan, both physical and virtual network interfaces are configured via YAML files which are translated to configurations compatible with the selected backend.

How To Manage Networking with Netplan on Debian / Ubuntu

To complete this guide, you must log in to your Ubuntu or Debian system as a non-root user with sudo privileges. Then, follow the steps below.

Install Netplan on Debian / Ubuntu

First, update your local package index with the command below:

sudo apt update

Netplan is installed by default in the latest Ubuntu 22.04. To install it on your Debian system or older release of Ubuntu, run the command below:

sudo apt install netplan.io

As we said before, Netplan is configured via YAML files. So you need to create a simple YAML file.

Create a Netplan YAML File on Debian / Ubuntu

At this point, you can use your favorite text editor to create a simple YAML file in the Netplan directory, here we use the vi editor.

You can create a new file or edit the default. If you opt to edit the default, I suggest making a copy with the command:

sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak

With your backup in place, you’re ready to configure.

sudo vi /etc/netplan/01-netcfg.yaml

Add the following content to the file:

network:
  version: 2
  renderer: NetworkManager

The configuration is meant to set all the network interfaces on the system to be managed by the NetworkManager renderer

When you are done, save and close the file.

How To Use Netplan on Debian / Ubuntu

Now you can use the command below to generate the required configuration for the renderers:

sudo netplan generate

To apply all configurations for the renderers, restart them as necessary by using the command below:

sudo netplan apply

To apply configuration and wait for user confirmation, use the following command:

sudo netplan try
Output
Do you want to keep these settings?


Press ENTER before the timeout to accept the new configuration


Changes will revert in 116 seconds
Configuration accepted.

In the next step, we want to show you how to configure a static IP address with Netplan. to do this, you need to know the name of the device to be configured. 

You can find your IP address by using the command below:

ip a

To view your default gateway, use the following command:

ip r

Configure Static IP Address with Netplan

To complete this step, you need to open your Netplan YAML file again:

sudo vi /etc/netplan/01-netcfg.yaml

Your file should be like this layout:

network:

    Version: 2

    Renderer: networkd

    ethernets:

       DEVICE_NAME:

          Dhcp4: yes/no

          Addresses: [IP/NETMASK]

          Gateway: GATEWAY

          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 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.

Here is an example YAML file for configuring static IP address with Netplan on Debian / Ubuntu:

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 file with your Networking needs, and save and close the file.

Note: The netmask is no longer configured in form 255.255.255.0. Instead, the netmask is added to the IP address.

Before applying the changes, let’s test the configuration. To do this, run the command below:

sudo netplan try

The above command will validate the configuration before applying it. If it succeeds, you will see Configuration accepted.

At this point, you can apply the new changes by using the Netplan apply command on Debian / Ubuntu:

sudo netplan apply

At this point, you can issue the command ip a to see that your new address configurations are in place.

How To Use DHCP with Netplan

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: true

       optional: true

Save and close that file. Test the file with the following command:

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. Or, you could leave it set to use DHCP (but seeing as how this is a server, you probably won’t want to do that).

Note: If you have more than one interface, you could name the second .yaml configuration file 02-netcfg.yaml. Netplan will apply the configuration files in numerical order, so 01 will be applied before 02. Create as many configuration files as needed for your server.

For more Netplan configuration, you can visit Netplan configuration examples.

Conclusion

At this point, you have learned to Manage Networking with Netplan on Debian / Ubuntu and Netplan Configuration.

Hope you enjoy it.

You may be like these articles:

How To Install and Use Linux Screen Command

Disable NetworkManager on AlmaLinux 8

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!