Install Netplan on Ubuntu

29/12/2020
Netplan is a utility for configuring network interfaces on Linux. Netplan uses YAML files for configuring network interfaces. YAML configuration file format is really simple. It has clear and easy to understand syntax. Netplan works with traditional Linux networking system Systemd-networkd and Network Manager. With Netplan you can configure the network of your Ubuntu machines easier than ever before. Starting from Ubuntu 18.04 LTS, Ubuntu uses Netplan to configure network interfaces by default.

In this article, I will show you how to install Netplan on Ubuntu 16.04 LTS and later, and how to use Netplan on Ubuntu. I will be using Ubuntu 18.04 LTS for the demonstration. But it should work the same way on every Ubuntu version where Netplan is installed. Let’s get started.

Installing Netplan on Ubuntu 16.04 LTS:

Netplan is available in the official package repository of Ubuntu. So, it is really easy to install. Netplan is not installed by default on Ubuntu 16.04 LTS. So, I am focusing on the installation method of Netplan on Ubuntu 16.04 LTS in this section. First, update the APT package repository cache with the following command:

$ sudo apt update

The APT package repository cache should be updated.

Now, install Netplan with the following command:

$ sudo apt install netplan

Netplan should be installed.

By default, Netplan is disabled on Ubuntu 16.04 LTS. You have to enable it manually. To enable Netplan, you have to create a file netplan in the /etc/default/ directory and add ENABLED=1 to it.

To do that, run the following command:

$ echo "ENABLED=1" | sudo tee /etc/default/netplan

/etc/default/netplan file should be created.

Now, reboot your computer with the following command:

$ sudo reboot

Netplan should be enabled.

Netplan Configuration Files:

On Ubuntu 18.04 LTS, the Netplan YAML configuration files are placed in the /etc/netplan/ directory. To configure a network interface, you have to create or modify required YAML files in this directory. YAML configuration files has the .yaml extension. The default Netplan YAML configuration file /etc/netplan/50-cloud-init.yaml is used to configure network interfaces using Netplan.

On Ubuntu 16.04 LTS, the configuration files are placed in the /etc/plan directory and the default configuration file is /etc/plan/netplan-acl.

Make sure you modify the correct configuration file depending on the version of Ubuntu you’re using.

Configuring Network Interface via DHCP with Netplan:

In this section, I will show you how to configure a network interface via DHCP on Ubuntu with Netplan.  First, find the network interface name that you want to configure with the following command:

$ ip a

As you can see, I have one network interface card (NIC) installed on my Ubuntu 18.04 LTS machine and the network interface name is ens33. It does not have any IP address configured right now. Let’s use Netplan to configure it via DHCP.

To configure the network interface ens33 via DHCP using Netplan, open the default Netplan configuration file on Ubuntu 18.04 LTS /etc/netplan/50-cloud-init.yaml with the following command:

$ sudo nano /etc/netplan/50-cloud-init.yaml

You should see the following window.

Now add the following lines in the network section.

ethernets:
ens33:
dhcp4: yes

Here, dhcp4: yes means, use DHCP for IPv4 protocol to configure the network interface ens33.

NOTE: The indentations are really useful. Make sure you indent each line correctly. It will make the syntax clearer and comfortable to the eye.

Finally, the configuration file should look something like this.

Now, press <Ctrl> + x and then press y followed by <Enter> to save the file.

The good thing about Netplan is that before you apply the changes, you can make sure the configuration file has no typos or any other mistakes with the following command:

$ sudo netplan try

Now press <Enter>.

If everything is alright, you should see Configuration accepted message as marked in the screenshot below.

If there’s any problem with the configuration file, you will see appropriate error messages here.

This feature will surely help you to avoid complex hard to track future problems with Netplan configuration files.  Finally, apply the changes permanently using Netplan with the following command:

$ sudo netplan apply

As you can see, the network interface ens33 is configured via DHCP.

Setting Up Static IP Address with Netplan:

If you want to set up a static IP on your network interface using Netplan, then this section is for you. You can manually set the IP address, name server, gateway etc. of your network interface using Netplan. Let’s say, you want to configure your network interface ens33 as follows:

Static IP address: 192.168.10.33
Subnet mask: 255.255.255.0
Gateway: 192.168.10.1
DNS server: 192.168.10.1

First, check the network configuration of ens33 network interface with the following command:

$ ip a

This is to help you verify that the network interface settings really changed.

Now, edit the Netplan YAML configuration file /etc/netplan/50-cloud-init.yaml with the following command:

$ sudo nano /etc/netplan/50-cloud-init.yaml

If you’ve followed me throughout the article, then the configuration file should be like this. Now, remove the line as marked in the screenshot below.

And type in the lines as marked in the screenshot below.

NOTE: Remember, indentation is essential for YAML files. If you forget to indent correctly, Netplan will not let you apply the configuration file. So, you must indent every step of the YAML configuration file as shown in the screenshot below.

Now, press <Ctrl> + x and then press y followed by <Enter> to save the file.

Now, check whether there’s any error in the configuration file with the following command:

$ sudo netplan try

press <Enter>.

As you can see, the configuration file is accepted.

Finally, apply the configuration file with the following command:

$ sudo netplan apply

As you can see, the IP address is changed as expected.

The gateway is also set correctly.

The DNS server is set correctly as well.

So, that’s how you install and use Netplan on Ubuntu to configure network interfaces using YAML files. If you want to learn more about Netplan, please visit the official website of Netplan at https://netplan.io.  Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Install GoLang on Ubuntu and Write Your First Program

Install GoLang on Ubuntu and Write Your First Program in Go GoLang is a very powerful programming language developed by...
12/02/2020

How to install LibreOffice 5.4.0 Released on Ubuntu

LibreOffice 5.4.0 had just been released as the last major release of the LibreOffice 5.x family, and its available for install...
12/02/2020

Install Oracle JDK 9 on Ubuntu 17.10

Install Oracle JDK 9 on Ubuntu 17.10 JDK or Java Development Kit is used to develop Java applications. It is used by Java...
12/02/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024