We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – Configuring Linux Bridge / VLAN interface using Netplan on Ubuntu

Learn how to – Configuring Linux Bridge / VLAN interface using Netplan on Ubuntu.

For communication between two systems to occur, they need to be connected either internally or externally. This network can be minor such as in a home or even large such as in universities or data centers. Maintaining the system’s network, therefore, becomes a vital task for any system or network administrator. Their main task revolves around network configuration, monitoring, securing, and troubleshooting.

There are quite a number of tools one can use when managing networks on Ubuntu. These include:

  • ifconfig: Display and manipulate route and network interfaces.
  • mtr: Combines ping and tracepath into a single command.
  • IP: It is a replacement of ifconfig command.
  • traceroute: Network troubleshooting utility.
  • tracepath: Similar to traceroute but doesn’t require root privileges.
  • whois: Will tell you about the website’s whois.
  • nslookup: Find DNS-related query.
  • dig: Query DNS-related information.
  • netstat: Display connection information.
  • ping: To check connectivity between two nodes.

In this guide, we will walk through how to configure Linux Bridge / VLAN interface using Netplan on Ubuntu.

Netplan is a tool developed by Canonical to provide an easy way to configure networks on a Linux system. Creating a network is easily done using a YAML file that contains all the descriptions of the required network interfaces. Normally, it reads the network configurations from /etc/netplan/*.yaml. During boot, Netplan generates backend-specific configuration files in /run that hand off the devices to a specific networking daemon. Currently, it supports Systemd-networkd and NetworkManager as the backend/renderer systems.

Below is a design overview of Netplan.

In Ubuntu 20.04 and later, Netplan has replaced the traditional method of configuring networks using the /etc/network/interfaces. The main reason for this is to make things easier and more centralized.

Netplan uses a set of commands in its operation:

  • netplan generate: It generates the required config for renderers
  • netplan apply: Apply all configurations for the renderers, restarting them as necessary.
  • netplan try: Apply configuration and wait for user confirmation; will roll back if the network is broken or no confirmation is given.

By the end of this guide, you should be able to understand:

  • The basic structure of YAML configuration files used by Netplan
  • How to create a simple rule to assign a static IP to a network interface
  • How to apply configurations using generate, try and apply subcommand

Getting Started

For this guide, you need:

  • Ubuntu system
  • Netplan which is installed by default on Ubuntu 20.04 and later
  • sudo privileges

Also, install the required package on your system.

sudo apt update -y
sudo apt install bridge-utils vim -y

1. Creating a Network Bridge using Netplan on Ubuntu

There are two ways how to create a network bridge using Netplan. These are:

  • bridge using DHCP addressing
  • bridge using static IPv4 addressing

A. Network bridge using DHCP addressing

This is the easiest of the two since it does not require IP inclusions. Begin by editing the default YAML at /etc/netplan/01-network-manager-all.yaml

sudo vim /etc/netplan/01-network-manager-all.yaml

On a fresh installation, the YAML will appear as shown:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager

You can choose to edit the renderer to a preferred one say networkd:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: networkd

In this guide, we will proceed with the default renderer(NetworkManager).

Obtain the interface name using the command:

$ 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 42:3a:ef:01:30:2d brd ff:ff:ff:ff:ff:ff
    altname enp0s18
    inet 192.168.205.22/24 brd 192.168.205.255 scope global noprefixroute ens18
       valid_lft forever preferred_lft forever
    inet6 fe80::2c01:ccda:29ac:b41/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens19: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether fe:68:0c:29:b7:85 brd ff:ff:ff:ff:ff:ff
    altname enp0s19
    inet 192.168.205.30/24 brd 192.168.205.255 scope global dynamic noprefixroute ens19
       valid_lft 43105sec preferred_lft 43105sec
 .....

Now add the below lines to create a network bridge using DHCP addressing:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens19:
      dhcp4: no

  bridges:
    br0:
      dhcp4: yes
      interfaces:
        - ens19

Once the interface name has been provided as required, save and apply the configurations.

sudo netplan apply

After some time, the bridge interface should appear with DHCP as shown;

B. Network bridge using static IPv4 addressing

It is also possible to provide static IPv4 addressing(netmask, gateway, DNS, search domains) to your bridge. Open the file for editing;

sudo vim /etc/netplan/01-network-manager-all.yaml

Make the below adjustments to your YAML:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens19:
      dhcp4: true

  bridges:
    br0:
      addresses: [ 192.168.205.26/24 ]
      routes:
        - to: default
          via: 192.168.205.1
      mtu: 1500
      nameservers:
          addresses: [8.8.8.8]
      interfaces:
        - ens19

The lower part of the YAML defines the various static IPv4 addresses for the bridge(br0). These are:

  • 192.168.205.26/24: the static IP address of the bond and subnet
  • 192.168.205.1: the gateway
  • 8.8.8.8: nameserver

Once the adjustments have been made, save the and apply the config changes:

sudo netplan apply

After a few minutes, the bridge should appear as shown:

Using the Network bridge

You can also test if the bridge is working using the ping command below:

ping -c 3 192.168.205.26

Sample Output:

Install net-tools:

sudo apt install net-tools -y

Check the routing configuration;

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.205.1   0.0.0.0         UG    100    0        0 ens18
0.0.0.0         192.168.205.1   0.0.0.0         UG    425    0        0 br0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens18
192.168.205.0   0.0.0.0         255.255.255.0   U     100    0        0 ens18
192.168.205.0   0.0.0.0         255.255.255.0   U     425    0        0 br0

You can now use the created bridge as desired. The best example is using it on KVM virtual machines as shown in the guide below:

2. Creating a VLAN interface using Netplan on Ubuntu

A VLAN interface can be defined as a virtual interface attached to the physical network port or bond that your VLAN is configured. It is used to tag the traffic route through it with the appropriate VLAN ID

Similar to bridges, you can easily create VLAN interfaces on Ubuntu using Netplan. Open the YAML file:

sudo vim /etc/netplan/01-network-manager-all.yaml

Now modify this file as preferred. For this case, we will create a VLAN interface with static IP information:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens19:
      dhcp4: true

  vlans:
     vlan.45:
     id: 45
     link: ens19
     addresses: [ 192.168.205.28/24 ]
     routes:
       - to: default
         via: 192.168.205.1
     nameservers:
         search: [ domain.local ]
         addresses: [8.8.8.8]

Save the file and apply the changes:

sudo netplan apply

Verify the changes:

Verify if the VLAN is working:

ping -c 3 192.168.205.28

Sample output:

3. Configure a bridge using a tagged VLAN

With the above VLAN, we can create a network bridge from it using both DHCP or static IP. All you need to do is provide the VLAN interface as the interface for the bridge network.

Open the file for editing:

sudo vim /etc/netplan/01-network-manager-all.yaml

Now under the created VLAN interface, add a network bridge:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens19:
      dhcp4: true

  vlans:
     vlan.45:
     id: 45
     link: ens19
     addresses: [ 192.168.205.28/24 ]
     routes:
       - to: default
         via: 192.168.205.1
     nameservers:
         search: [ domain.local ]
         addresses: [8.8.8.8]

  bridges:
    br0:
      addresses: [ 192.168.205.29/24 ]
      mtu: 1500
      nameservers:
          addresses: [8.8.8.8]
      interfaces:
        - vlan.45

Save the file and apply changes:

sudo netplan apply

Once applied, verify the changes:

Verify if the created bridge is working:

ping -c 3 192.168.205.29

Sample output:

Verdict

In this guide, we have systematically walked through how to configure Linux Bridge / VLAN interface using Netplan on Ubuntu. With this knowledge, you are in a better position to create a simple rule to assign a static IP to a network interface as well as apply the Netplan configurations.

Interested in more?

Cart

Your Cart is Empty

Back To Shop