« Go Back

Using Netplan for Networking Configurations Print

  • IPv4, IPv6, Netplan, Network Configuration, Ubuntu
  • 25

Using Netplan for Networking Configurations

Christopher Smith, MBA - July 26th, 2022

     

What is Netplan?

     Netplan is the network configuration tool used in all recent releases of Ubuntu starting with release 18.04, Bionic Beaver. Netplan utilizes a YAML-based configuration system that provides for a simple network configuration process. In previous versions of Ubuntu, and all other linux distributions, the /etc/network/interfaces was used to configure networking settings. This article will assist with both static and dynamic IP configurations using Ubuntu 20.04. 

 

Network Configuration using Netplan

 

     Network configuration files can now be found under /etc/netplan/$DISTRO.yaml. As always, $DISTRO should be replaced with the option for the distribution you are working with. These options can be found below. Since I am working with the desktop distribution in this case, the configuration can be found at /etc/netplan/01-network-manager-all.yaml. If your server has more than one network interface, multiple configuration files will be available. In this instance interface 2 would use 02-network-manager-all.yaml. Netplan will apply each configuration file in numerical sequence.

 

Replace $DISTRO with your Ubuntu Distribution

Ubuntu Server - [01-netcfg.yaml]

Ubuntu Desktop - [01-network-manager-all.yaml]

 

     Now that we know what we have our configuration file we can work toward configuring each network interface. The first thing you will want to do is find the name of the active interface that you want to configure. To do this, run the following command. Please ensure to note which interface you want to configure.

$ ip a

 

     We know that Netplan can be found in the directory /etc/netplan. This can be found using the following command:

$ ls /etc/netplan/

 

     To view the content of the configuration file you can use the following command:

$ cat /etc/netplan/01-network-manager-all.yaml

 

     Now, using an editor, you can open the configuration file. Since I use nano I will run the following command and enter my sudo password:

$ sudo nano /etc/netplan/01-network-manager-all.yaml

 

     Update the configuration file to suit your networking preferences. When using static IP addressing, add the IP addresses, gateway address, DNS information. If you are using dynamic IP addressing there is no need to add this information as it will pull the information from the DHCP server.  Use the syntax below when editing the configuration file. It is important to note that YAML is very strict when making indentations. Use space instead of tab otherwise the configuration may create an error.

network: 

        Version: 2

        Renderer: NetworkManager/ networkd

        ethernets:

             DEVICE_NAME:

                   Dhcp4: yes/no

                   Addresses: [IP ADDRESS/NETMASK]

                   Gateway: [GATEWAY]

                   Nameservers:

                          Addresses: [NAMESERVER_1,NAMESERVER_2]

 

Examples

Configuring Static IP addresses in Ubuntu

     To manually configure an IP address use the syntax above to add the IP addresses, gateway, and DNS Server information. You can see my configuration for a static IP below.

 

Configuring Dynamic IP Addresses in Ubuntu

     To use a dynamic IP address, Ubuntu can be configured to retrieve this information from the DHCP server. Simple change dhcp4 to yes and do not enter any IP addresses, gateways, or DNS information. You can see my configuration below using the required syntax.

 

Testing Configurations

     Before applying any changes, you will want to test your configuration for any errors. If the configuration returns that there are no issues, an acceptance message will be returned. If errors are found, the configuration will be returned to the previous working configuration. Run the following command under sudo to test configurations:

$ sudo netplan try

 

Apply the New Network Settings and Restart the Network Service

     Once you have tested your new configuration, you need to apply the configuration. The following command will accomplish this goal:

$ sudo netplan apply

 

     Now that we have applied the new configuration, we need to restart the network service. Depending on your distribution of Ubuntu (Server or Desktop) you will need to use one of the following commands:

Desktop - $ sudo systemctl restart network-manager

Server - $ sudo systemctl restart system-networkd

 

Verify Your IP Address

     Finally, we can verify that the new configurations were successfully applied. Run the following command and verify your IP address:

 

Additional Resources

    While this is a simple IPv4 configuration model, there are much more complex ways to configure networking on you Ubuntu server. For more advanced configuration examples, please visit the link below.

https://netplan.io/examples - Advanced IPv4 Examples

https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-configure-networking-in-ubuntu-20-04-with-netplan/ - IPv6 Examples

 


Was this answer helpful?