How to configure network on Ubuntu using Netplan

Netplan is the Ubuntu network configuration tool in all recent Ubuntu versions. Netplan relies on a YAML-based configuration system that simplifies the configuration process. It has replaced the old /etc/network/interfaces configuration file used to configure network interfaces in Ubuntu and other Linux distributions.

In this tutorial, you will learn how to configure networking in Ubuntu using Netplan. We will look at both static and dynamic IP configurations. The article will use Ubuntu 22.04 LTS to describe the process in this article, but the same steps apply to older Ubuntu 20.04.

Configure the network using Netplan

You can find the new configuration files at /etc/netplan/*.yaml. Ubuntu Server creates a Netplan configuration file for the system network named 01-netcfg.yaml, while Ubuntu desktop creates a Netplan configuration file for Network-Manager named 01-network-manager-all.yaml.

While working on the Ubuntu desktop, the author of the article has the file 01-network-manager-all.yaml in the /etc/netplan directory to configure the network. 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 configurations in numerical order. That means file 01 will be applied before file 02.

Now, let's move on to network configuration. Follow the steps below to configure a static or dynamic IP address in Ubuntu:

1. First, find the names of the active network interfaces you want to configure. To do so run the following command:

 

$ip a

How to configure network on Ubuntu using Netplan Picture 1

Note the interface name you want to configure with Netplan.

2. Netplan's default configuration file is located in the /etc/netplan directory. You can find that using the following command:

$ ls /etc/netplan/

How to configure network on Ubuntu using Netplan Picture 2

3. To view the contents of the Netplan network configuration file, run the following command:

$ cat /etc/netplan/*.yaml

How to configure network on Ubuntu using Netplan Picture 3

4. Now, you will need to open the configuration file in any editor: Because the article is using the Nano editor to edit the configuration file, the command that will be run is:

$ sudo nano /etc/netplan/*.yaml

How to configure network on Ubuntu using Netplan Picture 4

5. Update the configuration file according to your network connection needs. For static IP address, add IP address, Gateway, DNS information while for dynamic IP address, 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]

In there

  1. DEVICE_NAME: Name of the interface.
  2. Dhcp4: yes or no depending on dynamic or static IP address
  3. Addresses: The device's IP address in prefix notation. Do not use netmasks.
  4. Gateway: Gateway IP address to connect to the external network
  5. Nameservers: Addresses of DNS name servers

 

Note that YAML files are quite strict in indentation. Use spaces to indent rather than pressing tab. Otherwise, you will get an error.

Configure static IP address in Ubuntu

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

How to configure network on Ubuntu using Netplan Picture 5

Configure dynamic IP address in Ubuntu

To get the IP address from the DHCP server, use the same configuration file syntax above. But do not add IP address, Gateway and DNS server information.

Here, you can see an example configuration file for a dynamic IP address:

How to configure network on Ubuntu using Netplan Picture 6

Once you have completed the static or dynamic IP configuration, save and exit the configuration file.

Test configuration

Before applying any changes, check the configuration file. Run the following command as sudo to test the configuration:

$ sudo netplan try

How to configure network on Ubuntu using Netplan Picture 7

If there are no problems, it will return a message accepting the configuration. If the configuration file does not pass the test, it will be reverted to the previous active configuration.

 

Apply configuration

Now apply the new configurations by running the following command as sudo:

$ sudo netplan apply

How to configure network on Ubuntu using Netplan Picture 8

In case you see any errors, try debugging to investigate the problem. To run debugging, use the following command under sudo:

$ sudo netplan –d apply

Restart the network service

Once all configurations are applied successfully, restart the Network-Manager service by running the following command:

$ sudo systemctl restart network-manager

If you are using Ubuntu Server, use the following command instead:

$sudo systemctl restart system-networkd

Verify IP address

Now to verify if the new configuration was applied successfully, 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 a static or dynamic IP address without any complicated configuration.

5 ★ | 1 Vote

May be interested

  • Install and configure Apache in UbuntuInstall and configure Apache in Ubuntu
    in the following article, we will show you some basic steps to install and set up a web server system using apache on the ubuntu platform. in fact, this step is quite simple and easy, just use synaptic package manager, ubuntu software center to search and install apache2 module package.
  • What is the difference between Ubuntu Desktop and Ubuntu Server?What is the difference between Ubuntu Desktop and Ubuntu Server?
    ubuntu is divided into ubuntu cloud, ubuntu core, ubuntu kylin, ubuntu desktop and ubuntu server. in this article, we will explore all the similarities and differences between ubuntu server and ubuntu desktop.
  • How to share files between Android and Ubuntu onlineHow to share files between Android and Ubuntu online
    when your pc and mobile device are connected to the same wifi network, you can easily make them communicate with each other. in today's article, you will learn how to share files between android and ubuntu on the local network.
  • How to install Nextcloud with OnlyOffice in UbuntuHow to install Nextcloud with OnlyOffice in Ubuntu
    there are many different ways to install and configure nextcloud, including manual installation, through the snap system of ubuntu and docker. here, the article will use the dockoffice configuration provided by onlyoffice.
  • Ubuntu 21.04 users need to update the system ASAPUbuntu 21.04 users need to update the system ASAP
    according to the proposed roadmap, it is only a few days before canonical will officially stop providing updates for ubuntu 21.04 'hirsute hippo', which has been available since april last year.
  • 5 things to do after upgrading to Ubuntu 22.04 LTS5 things to do after upgrading to Ubuntu 22.04 LTS
    ubuntu 22.04 lts jammy jellyfish is the latest version of the popular open source operating system ubuntu. installing a fresh copy on a freshly wiped hard drive will give you a great computing experience.
  • 5 Reasons Ubuntu Cinnamon Is Better Than Other Ubuntu Versions5 Reasons Ubuntu Cinnamon Is Better Than Other Ubuntu Versions
    ubuntu cinnamon became an official ubuntu release in 2023. it gives you ubuntu but with the cinnamon desktop environment instead of gnome.
  • Learn about Ubuntu Remote DesktopLearn about Ubuntu Remote Desktop
    do you need to connect to a remote computer? instead of having to move from room to room, if the devices are on the same network, this will be easy, regardless of the operating system you use.
  • How to Configure Computers on a Local NetworkHow to Configure Computers on a Local Network
    in this article, tipsmake will guide you how to install a lan (local area network) to connect multiple windows computers.
  • How to Configure a RouterHow to Configure a Router
    the router is an important device in the home network. if you configure your router properly, you can keep your information secure from snoopers, securely connect every device in your home to the internet, and even keep your children from seeing things they shouldn't. see. follow the steps in this article to configure your router in just a few minutes.