How to configure static IP using Netplan on Ubuntu

Estimated reading: 5 minutes 362 views

From Ubuntu 17.10 onwards, the way a network is configured is completely changed. In earlier versions of Ubuntu, a static file (/etc/network/interfaces) was used for configuring network interfaces. Canonical has launched a new tool called Netplan to manage network settings. Netplan is a utility that uses YAML to configure networking on a Linux system.

Prerequisites

Ubuntu 22.04 system or any other Ubuntu version from 17.10 or higher

How does Netplan work?

Netplan fetches the network configuration information from a .yaml file (YAML format). This file can reside inside the following netplan directories:

1. /etc/netplan/
2. /lib/netplan/
3. /run/netplan/

At the initial boot phase, Netplan creates backend config files inside the ‘/run’ directory and transfers control of devices to one of the supported network services/daemon: NetworkManger or Systemd-networkd.

Netplan Commands

Three commands are used in conjunction with Netplan:

netplan generate: This will generate a configuration for renderers or backends using the /etc/netplan.
netplan apply: It is used to apply all the configurations for the renderers.
netplan try: Apply a configuration, then wait for the user to confirm.

Getting started with Netplan

A basic Netplan configuration can be written as::

Netplan reads the above configuration when the system boots and generates a file as ‘/run/NetworkManager/conf.d/10-globally-managed-devices.conf’. The system will be informed that all the network configuration and device management tasks will be handled by NetworkManager. There are currently two backends: NetworkManager and systemd-networkd. Only one can be supported at a time. The default renderer is ‘systemd-networkd’.

Configuring a Static IP address using Netplan

The configuration file for Netplan(.yaml) is stored in the directory ‘/etc/netplan’. In our case, there is no config file in this directory, and no IP is assigned to the interface ‘ens3’:

Let’s get started now to assign a static IP on this interface. Before we dive into this tutorial, you must read the below important note:

IMPORTANT NOTE: You should first check if the below configurations work by running the command:

In this way, we can roll back our changes in the config file after a specific timeout. The following screen will appear to confirm if you want to keep changes or revert back to the configuration.

Setting a Single Static IP

In cases where your VPS has a network error or you install the VPS from the ISO file. You can access via VNC console to configure your VPS network.

If you do not know how to use VNC, you can refer to the following instructions:

https://green.cloud/docs/how-to-manage-your-kvm-vps/

To configure, you need to determine the name of your ethernet network card. With Ubuntu, enter the ip a command to check.

ip a 

At this point, the system will print out the network card information. As shown below, I have a Lookpack card and the main card is ens3.

Now open the configuration file to start configuring the IP. The netplan configuration files will be in the /etc/netplan path and the files with the extension will be .yaml.

configure Static IP on Ubuntu

My configuration file is 00-installer-config.yaml, use nano editor to edit the vi 00-installer-config.yaml file.

What you need to know in this configuration file is that each Netplan file will start with the keyword network and have at least 2 required elements that are.

  • version: Version parameter of the network
  • device type: Can be ethernets, bonds, bridges, or VLANs

Below is my entire configuration in the file with comments on each line following the # symbol.

# This is the network config written by 'subiquity'
network:
  ethernets: {}
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      addresses:
        - Your_IP/Subnet_Mask
      routes:
          - to: default
            via: Gateway_address
      nameservers:
            search: [google.com]
            addresses: [8.8.8.8, 8.8.4.4]

configure Static IP on Ubuntu
After the configuration file adjustment is complete, enter the following command to apply the change and verify the change.:

netplan apply
ip a
configure Static IP on Ubuntu

To check if there is a network, I do a Ping to the internet. Here I PING to google.com and the result was successful.

Screenshot 2020 12 23 at 20.57.34@2x

Setting Multiple Static IP addresses

If you want to assign different IPs to different interfaces in the same configuration file, you can do so as follows.

nano /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
  ethernets: {}
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      addresses:
        - Your_IP1/Subnet_Mask
        - Your_IP2/Subnet_Mask
      routes:
          - to: default
            via: Gateway_address
      nameservers:
            search: [google.com]
            addresses: [8.8.8.8, 8.8.4.4]

Once done with the configuration, remember to run the netplan apply command to apply the changes and bring the interfaces up.

That is all about how to configure Static IP Addresses using Netplan on Ubuntu.

Summary

In this guide, we have learned to set static IP using Netplan. Netplan config file is very space-sensitive as it uses the YAML format. Managing networking with Netplan is quite straightforward. You will not find it tough to master once you get used to it.