<img alt="Ubuntu Network configuration with netplan" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/ubuntu-network-netplan-1024×512.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="375" src="data:image/svg xml,” width=”750″>

Ubuntu has switched to Netplan for the configuration of network interfaces. Netplan is based on a YAML-based configuration system that makes the configuration process very simple. Netplan has replaced the old /etc/network/interfaces configuration file that we used to use for configuring network interfaces in Ubuntu.

In this article, you will learn how to configure networks in Ubuntu using Netplan. We will look at both static and dynamic IP configuration. I will be using Ubuntu 22.04 LTS to describe the process in this article.

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

How to configure networking with Netplan on Ubuntu linux shell ubuntu

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/

<img alt="Netplan configuration file" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-262.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="49" loading="lazy" src="data:image/svg xml,” width=”335″>

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

$ cat /etc/netplan/*.yaml

<img alt="Content of the Netplan network config file" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-263.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="113" loading="lazy" src="data:image/svg xml,” width=”598″>

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

<img alt="Edit the network configuration" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-264.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="53" loading="lazy" src="data:image/svg xml,” width=”692″>

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]

Where

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:

<img alt="IP address configuration" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-265.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="431" loading="lazy" src="data:image/svg xml,” width=”754″>

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:

<img alt="Ubuntu DHCP Configuration for Network card" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-266.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="413" loading="lazy" src="data:image/svg xml,” width=”865″>

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

Testing configuration

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

$ sudo netplan try

<img alt="Test config with netplan try command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-267.png626725ff099d9.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="203" loading="lazy" src="data:image/svg xml,” width=”635″>

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.

Apply configuration

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

$ sudo netplan apply

<img alt="Apply network config changes" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-268.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="29" loading="lazy" src="data:image/svg xml,” width=”364″>

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.