How To Manage Ubuntu / Debian Networking using Netplan

An IP address is a unique address that is assigned to any device once connected to the internet to help identify it. There are two types of Ip addresses, Public and Private addresses. Public Addresses are routable on the world wide web and will grant you connectivity to other servers and routers around the world. Private addresses allow you to connect to an internal network example that of an organization, company, department, or Home network.

With a Linux distribution package like Ubuntu or Debian, you can configure network settings and configure interfaces to make them work according to your needs. The Linux kernel supports a variety of Network interfaces including lo for the local loopback interface, eth for ethernet card interface (eth0, eth1), tr for token ring card interface (tr0, tr1), and sl for SLIP interfaces (sl0, sl1).

You can assign a device a static IP address or use a Dynamic Host Control Protocol (DHCP). A server using the DHCP network protocol automatically assigns an IP address to a computer from a defined range of numbers configured for a given network. There are a set of graphical and command line utilities shipped with Ubuntu and Debian to configure Network settings. One of the is Netplan which we are going to look into in this guide.

What is Netplan?

Netplan is a network configuration abstraction renderer that is used to easily configure networking in a Linux system. It works by a system administrator creating a YAML file that constitutes the required network interfaces and what each should be configured to do. Then Netplan will generate all the necessary configurations for your chosen renderer tool. Netplan currently supports NetworkManager which would mean all interface configurations are handled by the GUI and Systemd-networkd renderer which allows the systems to handle all the network configurations.

You can create more than one configuration file with different configuration details. It is advisable to name them a numerical such as 01-config, 02-config, and so on. Netplan would parse the configurations from the files in numerical order where 01-config would be applied before 02-config

Install Netplan on Debian / Ubuntu

Before we can begin the installation, first update your system with the latest packages.

sudo apt update && sudo apt upgrade 

On the latest Ubuntu system (24.04), the Netplan utility is installed by default. For Debian System, install the Netplan utility with the following command

sudo apt install netplan.io

Manage Ubuntu / Debian Networking using Netplan

Without a configuration file, Netplan would not do anything. We can create a simple file in the Netplan directory to bring things up via DHCP.

sudo nano /etc/netplan/config.yaml

Then append the following lines noting the spaces as it is a Yaml file.

network:
  version: 2
  renderer: NetworkManager

Save and exit the file. This file makes the NetworkManager manage all devices.

To generate the required configuration for the renderers.

sudo netplan generate

To apply all configurations for the renderers, restart them as necessary.

sudo netplan apply

To apply configuration and wait for user confirmation;

$ sudo netplan try
Do you want to keep these settings?

Press ENTER before the timeout to accept the new configuration

Changes will revert in 112 seconds
Configuration accepted.

To see your IP address

$ ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 6a:a8:04:c0:eb:0e brd ff:ff:ff:ff:ff:ff
    altname enp0s18
    inet 192.168.200.43/24 brd 192.168.200.255 scope global noprefixroute ens18
       valid_lft forever preferred_lft forever
    inet6 fe80::68a8:4ff:fec0:eb0e/64 scope link 
       valid_lft forever preferred_lft forever

To view your default gateway, use the following command

$ ip r
default via 192.168.200.1 dev ens18 proto static metric 100 
169.254.0.0/16 dev ens18 scope link metric 1000 
192.168.200.0/24 dev ens18 proto kernel scope link src 192.168.200.43 metric 100 

Dynamic IP Address Assignment using Netplan

To configure an interface to use DHCP, edit the configuration file and add the following lines by setting DHCP to true.

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens18:
      dhcp4: true

Apply the configuration with the following command.

sudo netplan apply

Check your IP address to see the changes:

: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 6a:a8:04:c0:eb:0e brd ff:ff:ff:ff:ff:ff
    altname enp0s18
    inet 192.168.200.161/24 brd 192.168.200.255 scope global dynamic noprefixroute ens18
       valid_lft 43197sec preferred_lft 43197sec
    inet6 fe80::68a8:4ff:fec0:eb0e/64 scope link 
       valid_lft forever preferred_lft forever

Static IP Address Assignment using Netplan

To set a static IP address with Netplan, edit the configuration file.

sudo nano /etc/netplan/config.yaml

Add the following lines and update the details with your preferred IP address, default gateway, and DNS server.

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens18:
      addresses:
        - 192.168.200.49/24
      nameservers:
        search: [example.techview.com, sales.techview.com]
        addresses: [8.8.8.8, 8.8.4.4, 192.168.200.1]
      routes:
        - to: default
          via: 192.168.200.1

Save and exit the file, then apply the changes by running the following command

sudo netplan apply

Confirm your new static IP address.

$ ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 6a:a8:04:c0:eb:0e brd ff:ff:ff:ff:ff:ff
    altname enp0s18
    inet 192.168.200.49/24 brd 192.168.200.255 scope global noprefixroute ens18
       valid_lft forever preferred_lft forever
    inet6 fe80::68a8:4ff:fec0:eb0e/64 scope link 
       valid_lft forever preferred_lft forever

Connecting Multiple interfaces with DHCP

Connecting to multiple networks through a specific interface with DHCP can be done as shown below.

network:
  version: 2
  ethernets:
    enred:
      dhcp4: yes
      dhcp4-overrides:
        route-metric: 100
    engreen:
      dhcp4: yes
      dhcp4-overrides:
        route-metric: 200

Save and exit the file then apply the changes with the following command

sudo netplan apply

Connect to an open Wireless network

To connect to a wireless network:

network:
  version: 2
  wifis:
    wl0:
      access-points:
        opennetwork: {}
      dhcp4: yes

Apply the changes:

sudo netplan apply

Connect to a personal Network

This will involve providing the network name and password.

network:
  version: 2
  renderer: NetworkManager
  wifis:
    wlp2s0b1:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.0.21/24]
      nameservers:
        addresses: [192.168.0.1, 8.8.8.8]
      access-points:
        "network_ssid_name":
            password: "**********"
      routes:
        - to: default
          via: 192.168.0.1

Apply the changes:

sudo netplan apply

Multiple Addresses on a Single Interface

To connect to multiple interfaces on a single interface, assign a list of addresses to that interface as shown below.

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens18:
      addresses:
          - 10.100.1.37/24
          - 10.100.1.38/24:
              label: enp3s0:0
          - 10.100.1.39/24:
              label: enp3s0:some-label
      routes:
          - to: default
            via: 10.100.1.1

Apply the changes

sudo netplan apply

Multiple addresses with multiple gateways

To use multiple addresses with multiple gateways, the syntax is almost similar to the one above

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens18:
      addresses:
        - 10.0.0.10/24
        - 11.0.0.11/24
        routes:
        - to: default
          via: 10.0.0.1
          metric: 200
        - to: default
          via: 11.0.0.1
          metric: 300

Apply the changes:

sudo netplan apply

Conclusion

Netplan may be new and an alternate way to which most users were accustomed to configuring Linux network settings, but it is simple and easy to get used to. With just a simple configuration file written in YAML, Netplan parses the written configuration and applies it to the system. Should you have more than one interface and probably use two or more configuration files, Netplan applies the files in numerical order.

More articles available on this site:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

Nextcloud is a free and open-source syncing and file sharing server. It is self-hosted and allows companies to have a […]

What is Tokei and how is it useful to developers? Tokei is a very powerful program that is used to […]

The aaPanel is a free and open-source control panel used to manage hosting services such as websites, databases, Docker containers, […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.