How to Configure Interface as Vlan Trunk on Ubuntu

Written by: Bagus Facsi Aginsa
Published at: 10 Feb 2021


In ubuntu 18.04 or later, we have an application called netplan to manage our network. The configuration file is located in /etc/netplan directory. In this tutorial, We will using netplan to configure trunk vlan interface.

Sudo Privileges

Before start we make sure that we will have no permission issue on the configuration process.

sudo su

Use Case

To demonstrate how we can make our interface a trunk vlan, we will use this use case:

                                  __________________     
                                 |______            |
    vlan 110 -----------\        |      |           |
                         |-------|eth0  |  ubuntu   |
    vlan 120 -----------/        |______|  server   |
                                 |__________________|

Our server will have 1 interface named eth0 that connected to 2 vlans.

  1. On vlan 110 we will assign 192.168.6.60/24 ip address with gateway to 192.168.6.1.
  2. On vlan 120 we will assign 172.16.100.99/24 ip address.

Netplan Configuration

To configure the network in our server, first open the configuration file:

nano /etc/netplan/50-cloud-init.yaml

In my server the file name is 50-cloud-init.yaml, maybe it is different in your case. Just open the yaml file located on /etc/netplan directory.

This is how the configuration is implemented to suits the use case we mention above:

network:
    ethernets:
        eth0:
            dhcp4: false
    vlans:
        vlan.110:
            id: 110
            link: eth0
            addresses: [192.168.6.60/24]
            gateway4: 192.168.6.1
        vlan.120:
            id: 120
            link: eth0
            addresses: [172.16.100.99/24]
    version: 2

As we can see here, to define vlan interface we need vlans: directive. If you need more vlan interface, just add another vlan configuration under vlans: directive. The key to binding the vlan to the interface is link: eth0 on each vlan configuration, make sure the interface name is right. When we define the link, it will treat the link as trunk vlan.

You can check your interface name by running this command:

ip addr

After editing the file, use this command to activate the new configuration:

netplan apply

Thats it! Congratulation you just made a vlan interface on your Ubuntu 18.04!