Netplan is the Ubuntu network configuration tool in all recent Ubuntu versions. Netplan is based on a YAML-based configuration system that makes the configuration process very simple. It has replaced the old /etc/network/interfaces configuration file that we used to use for configuring network interfaces in Ubuntu and other Linux distributions.
In this tutorial, you will learn how to configure networks in Ubuntu using Netplan. We will look at both static and dynamic IP configurations. I will be using Ubuntu 22.04 LTS to describe the process in this article, but the same steps apply to the older Ubuntu 20.04 as well.
Network configuration using Netplan
You can find the new configuration files at /etc/netplan/*.yaml. Ubuntu server generates Netplan configuration file for system-networkd named 01-netcfg.yaml, while Ubuntu desktop generates a Netplan configuration file for Network-Manager named 01-network-manager-all.yaml.
As I am working on Ubuntu desktop, I have 01-network-manager-all.yaml file in my /etc/netplan directory for network configuration. 01-network-manager-all.yaml is used to configure the first interface. If you have multiple interfaces, use 02-network-manager-all.yaml for the second interface. Netplan applies the configuration in the numerical order. That means 01 file will be applied before the 02 file.
Now let’s move towards network configuration. Follow the steps below to configure static or dynamic IP addressing in Ubuntu:
1. First, find the name of the active network interfaces that you want to configure. To do so run the following command:
$ ip a
Note the interface name that you want to configure using Netplan.
2. The Netplan default configuration file is under the directory /etc/netplan. You can find that using the following command:
$ ls /etc/netplan/
3. To view the content of Netplan network configuration file, run the following command:
$ cat /etc/netplan/*.yaml
4. Now you will need to open the configuration file in any editor: As I am using Nano editor to edit the configuration file, so I will run:
$ sudo nano /etc/netplan/*.yaml
5. Update the configuration file as per your networking needs. For static IP addressing, add the IP address, Gateway, DNS information while for dynamic IP addressing, there is no need to add this information as it will get this information from DHCP server. Use the following syntax to edit the configuration file.
network: Version: 2 Renderer: NetworkManager/ networkd ethernets: DEVICE_NAME: Dhcp4: yes/no Addresses: [IP_ADDRESS/NETMASK] Gateway: GATEWAY Nameservers: Addresses: [NAMESERVER_1, NAMESERVER_2]
- DEVICE_NAME: Name of the interface.
- Dhcp4: yes or no depending upon dynamic or static IP addressing
- Addresses: IP address of the device in prefix notation. Do not use netmask.
- Gateway: Gateway IP address to connect to an outside network
- Nameservers: Address of DNS name servers
Note that YAML files are rather strict in the indentation. Make use of spaces for indentation, not tabs. Otherwise, you will encounter an error.
Configure static IP address in Ubuntu
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:
Configure Dynamic IP address in Ubuntu
To obtain IP addressing from the DHCP server, use the same above configuration file syntax. But do not add the IP address, Gateway, and DNS server information.
Here you can see my configuration file for dynamic IP addressing:
Once you have done with the static or dynamic IP configuration, save, and exit the configuration file.
Before applying any changes, we will test the configuration file. Run the following command as sudo to test configurations:
$ sudo netplan try
If there is no issue, it will return the configuration accepted message. If the configuration file fails the test, it will be reverted to a previous working configuration.
Now apply the new configurations by running the following command as sudo:
$ sudo netplan apply
In case you see any error, try debugging to investigate the problem. To run debug, use the following command as sudo:
$ sudo netplan –d apply
Restart the network service
Once all the configurations are successfully applied, restart the Network-Manager service by running the following command:
$ sudo systemctl restart network-manager
If you are using a Ubuntu Server, instead use the following command:
$sudo systemctl restart system-networkd
Verify IP address
Now to verify if the new configurations are successfully applied, run the following command to verify the IP address:
$ ip a
Whether you have an Ubuntu server or desktop, you can simply use Netplan to configure static or dynamic IP addressing without needing any complex configuration.