How to Install and Configure Netplan for Network Configuration in Ubuntu 20.04

How to Install and Configure Netplan for Network Configuration in Ubuntu 20.04 | DigitalOcean Community

Today we’re going to talk about how to install and configure Netplan for network configuration on Ubuntu 20.04. If you’ve been using Ubuntu for a while now, you might be familiar with the good old ifconfig command that was used in previous versions of Ubuntu to manage your network interfaces. However, starting from version 17.10, Ubuntu switched to Netplan as its default tool for managing network configuration.

So, let’s dive right into it! To begin with, make sure you have an up-to-date system by running the following command:


# Update and upgrade the system using apt command
# The -y flag automatically confirms any prompts during the upgrade process
sudo apt update && sudo apt upgrade -y

Once that’s done, we can proceed with installing Netplan. If you haven’t already installed it, run this command to get it on your machine:


# Install Netplan tools using apt package manager
sudo apt install netplan-tools

The above script uses the `apt` package manager to install Netplan tools. `sudo` is used to run the command with root privileges. 


# Check if Netplan is already installed
if [ -f /etc/netplan/config.yaml ]; then
    echo "Netplan is already installed."
else
    echo "Netplan is not installed."
fi

This code segment checks if Netplan is already installed by checking for the existence of the `config.yaml` file in the `/etc/netplan` directory. If the file exists, the script will print "Netplan is already installed." If the file does not exist, the script will print "Netplan is not installed." 


# Create a new Netplan configuration file
sudo touch /etc/netplan/config.yaml

This code segment creates a new Netplan configuration file named `config.yaml` in the `/etc/netplan` directory. `sudo` is used to run the command with root privileges. 


# Edit the Netplan configuration file
sudo nano /etc/netplan/config.yaml

This code segment opens the `config.yaml` file in the nano text editor for editing. `sudo` is used to run the command with root privileges. 


# Apply the changes made to the Netplan configuration file
sudo netplan apply

This code segment applies the changes made to the Netplan configuration file. `sudo` is used to run the command with root privileges.

Now that we have Netplan installed, let’s create a new configuration file for our network interface. By default, Ubuntu creates a sample configuration file at /etc/netplan/00-installer-config.yaml. However, since this is just an example and might not fit your needs, it’s best to start from scratch by creating a new YAML file in the same directory:

# Create a new YAML file for network interface configuration
$ sudo nano /etc/netplan/my_network_interface.yaml

# The following code sets the network interface to use DHCP for IP configuration
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: true # Enable DHCP for IPv4 configuration
      dhcp6: false # Disable DHCP for IPv6 configuration

# The following code sets the network interface to use a static IP address
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      addresses: [192.168.1.10/24] # Set static IP address and subnet mask
      gateway4: 192.168.1.1 # Set default gateway
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4] # Set DNS servers

# The following code sets the network interface to use a static IP address with a custom DNS server
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      addresses: [192.168.1.10/24] # Set static IP address and subnet mask
      gateway4: 192.168.1.1 # Set default gateway
      nameservers:
        addresses: [192.168.1.2] # Set custom DNS server

# The following code sets the network interface to use a static IP address with a custom DNS server and a secondary IP address
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      addresses: [192.168.1.10/24, 192.168.1.11/24] # Set primary and secondary IP addresses with subnet masks
      gateway4: 192.168.1.1 # Set default gateway
      nameservers:
        addresses: [192.168.1.2] # Set custom DNS server

Inside that file, add the following configuration for our network interface (let’s call it eth0):

# This is an example YAML configuration file for Netplan on Ubuntu 20.04
# For more information about Netplan and its syntax, check out: https://netplan.io/examples

# Set the network version to 2
network:
  version: 2
  # Use networkd as the renderer
  renderer: networkd
  # Configure the network interface named eth0
  ethernets:
    eth0:
      # Enable DHCP for automatic IP address assignment
      dhcp4: true # set to false if you want to use a static IP address instead of DHCP

In this example, we’re using the NetworkManager (renderer) and setting our interface to obtain an IP address via DHCP. If you prefer to use a static IP address, simply change `dhcp4: true` to `dhcp4: false`, then add your desired configuration for that network interface under the `addresses:` key.
Once you’re done editing the file, save and close it (Ctrl + X, Y, Enter). Now let’s apply our new Netplan configuration by running this command:


# This script is used to apply a new Netplan configuration
# by running the command "sudo netplan apply"

# First, we need to open the Netplan configuration file
# using the command "sudo nano /etc/netplan/01-netcfg.yaml"
# and make any necessary changes to the network interface settings

# To set a static IP address, we need to change "dhcp4: true"
# to "dhcp4: false" and add our desired configuration
# under the "addresses:" key

# Once we're done editing the file, we can save and close it
# by pressing Ctrl + X, Y, Enter

# Now, we can apply our new Netplan configuration
# by running the command "sudo netplan apply"
# This will update the network interface settings
# according to our changes in the Netplan configuration file

That’s it! Your network interface should now be configured using Netplan. If you want to check the current status of your interfaces, run `ip addr show`. You can also revert back to the default Ubuntu configuration by running:

# Reset Netplan configuration
sudo netplan reset # Use sudo to run the command as root user

# Check current interface status
ip addr show # Use ip command to show current interface status

# Revert back to default Ubuntu configuration
sudo netplan reset # Use sudo to run the command as root user

Now how to configure a static IP address on Ubuntu 20.04 using Netplan. First, open your terminal either by using the Ctrl+Alt+T keyboard shortcut or by clicking on the terminal icon and run:

# Use sudo to run the nano command with root privileges
sudo nano /etc/netplan/my_static_ip.yaml

# The following code creates a new file named "my_static_ip.yaml" in the specified directory
# and opens it in the nano text editor for editing.

# The code below is used to configure a static IP address on Ubuntu 20.04 using Netplan.
# It is important to specify the correct IP address, gateway, and DNS server for the network
# in order for the connection to function properly.

network:
  version: 2
  renderer: networkd
  ethernets:
    # Specify the network interface to be configured
    enp0s3:
      dhcp4: no # Disable DHCP and use a static IP address
      addresses: [192.168.1.100/24] # Set the desired IP address and subnet mask
      gateway4: 192.168.1.1 # Set the gateway IP address
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4] # Set the DNS server addresses

# Save the changes made to the file and exit the nano text editor.
# The changes will be applied when the netplan configuration is applied.
# This can be done by running the following command in the terminal:
# sudo netplan apply

Inside that file, add the following configuration for our network interface (let’s call it eth0):

# This is an example YAML configuration file for Netplan on Ubuntu 20.04
# For more information about Netplan and its syntax, check out: https://netplan.io/examples

# Set the network version to 2 and use the networkd renderer
network:
  version: 2
  renderer: networkd

  # Configure the eth0 interface
  ethernets:
    eth0:
      # Set DHCP to false to use a static IP address
      dhcp4: false
      # Assign the IP address and subnet mask for the interface
      addresses: [192.168.121.221/24]
      # Set the gateway for the interface
      gateway4: 192.168.121.1

In this example, we’re using the NetworkManager (renderer) and setting our interface to use a static IP address of `192.168.121.221/24`. We also set the gateway to be `192.168.121.1`. Once you’re done editing the file, save and close it (Ctrl + X, Y, Enter). Now let’s apply our new Netplan configuration by running this command:


# Set the NetworkManager renderer to use a static IP address of 192.168.121.221/24
# and set the gateway to be 192.168.121.1
# This will ensure a stable and consistent network connection
$ sudo netplan apply

# Apply the new Netplan configuration
# This command will activate the changes made in the previous step
# and update the network settings accordingly
# It is important to run this command after making any changes to the network configuration
$ sudo netplan apply

That’s it! Your network interface should now be configured using a static IP address. If you want to check the current status of your interfaces, run `ip addr show`. You can also revert back to the default Ubuntu configuration by running:

# Reset netplan configuration
sudo netplan reset

# Check current interface status
ip addr show

# Revert back to default Ubuntu configuration
sudo netplan reset

We hope this tutorial helped you understand how to install and configure Netplan for network configuration on Ubuntu 20.04, as well as how to set up a static IP address using Netplan. If you have any questions or comments, feel free to leave them below!

SICORPS