Ubuntu Netplan 101

In many of my previous presentations we have configured networking for Docker and LXD using built-in commands for those containers. The CLI for both Docker and LXD allow you to configure your network interfaces.

It’s important to know how to configure Linux networking in a Linux server or desktop instance. These instances can be bare metal machines, virtual machines, LXD containers or LXD Virtual machines. I have covered all of these on the channel.

We learned in this video how to use Netplan in an Ubuntu 22.04 server instance to change the network configuration. We used network-manager as our network renderer as opposed to networkd and we also configured bridged adapters and vlan adapters. To support all these network types, install the following dependencies:

sudo apt install bridge-utils
sudo apt install network-manager
sudo apt install vlan
sudo apt install net-tools

The Netplan configuration files are stored in:

/etc/netplan

To begin with, I deleted all of the existing files. If there is more than one YAML file located here, it will be used in numbered order as a part of your network configuration. To simplify understanding netplan, I use one single file named 01-netcfg.yaml. If you are scared to delete your YAML files, make a backup elsewhere.

cd /etc/netplan
rm *.yaml

The first network configuration is for a bridge. We named the bridge “bridge0”. Bridges are used to connect one network to another and are used often to allow docker or LXD to present instances with LAN addresses as opposed to internal NAT addresses which they use by default. This is covered on the channel in many other videos.

To create a bridge configuration:

sudo nano /etc/netplan/01-netcfg.yaml

Put the following text in the file and save it:

network:
  version: 2
  renderer: NetworkManager

  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false

  bridges:
    bridge0:
      interfaces: [eth0]
      addresses: [172.16.25.1/16]
      routes:
         - to: default
           via: 172.16.0.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 1.0.0.1
      parameters:
        stp: true
        forward-delay: 4
      dhcp4: no

To apply the configuration:

sudo netplan apply

You can see the result with the “ifconfig” command.

To change the network configuration back to DHCP:

sudo nano 01-netcfg.yaml

Clear out the file and replace it with the following:

network:
  version: 2
  renderer: NetworkManager

  ethernets:
    eth0:
      dhcp4: true
      dhcp6: false
      routes:
         - to: default
           via: 172.16.0.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 1.0.0.1

Apply the configuration:

sudo netplan apply

Again, an “ifconfig” will show the results and the bridge0 device will be gone.

If you have a second Ethernet adapter installed, you can edit the file again:

sudo nano 01-netcfg.yaml

Replace the contents with the following:

network:
  version: 2
  renderer: NetworkManager

  ethernets:
    eth0:
      dhcp4: true
      dhcp6: false
      routes:
         - to: default
           via: 172.16.0.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 1.0.0.1
    eth1:
      dhcp4: true
      dhcp6: false
      nameservers:
        addresses:
          - 1.1.1.1
          - 1.0.0.1

Apply the changes:

sudo netplan apply

“ifconfig” to see the results.

Finally, l created a configuration to include two of my VLANs. You must have a managed router and a managed switch to support VLANs and you must have previously created the VLANs. Edit the file again:

sudo nano 01-netcfg.yaml

Replace the contents with the following:

network:
  version: 2
  renderer: NetworkManager

  ethernets:
    eth0:
      dhcp4: true
      routes:
         - to: default
           via: 172.16.0.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 1.0.0.1

  vlans:
    vlan100:
      id: 100
      link: eth0
      addresses: [192.168.100.77/24]
    vlan200:
      id: 200
      link: eth0
      addresses: [192.168.200.88/24]

To apply the changes:

sudo netplan apply

Do an ifconfig to see the new VLAN adapters.

I cannot emphasize enough how important it is to get YAML file spacing correct and to not have any embedded tabs. The YAML language parser is brutal and small spacing errors can be a huge time sink. I have made every effort in the examples to preserve the formatting in the code blocks.