How to Setup VLAN on Ubuntu 22.04

Share this article

Introduction

Virtual Local Area Networks (VLANs) offer a powerful way to segment networks and enhance security and performance. In this guide, we’ll explore how to set up VLANs specifically on Ubuntu 22.04, unlocking advanced networking capabilities. The VLAN protocol is based on IEEE 802.1Q. standards.

In our previous article titled How to Configure VLAN on Ubuntu 20.04, we configured the VLAN in Ubuntu 20.04. The methods remain the same here, and we will use the same example.

The image above illustrates an example of how the office network can be divided. There is a distinct separation of the department networks achieved by implementing VLANs.

Preparing for VLAN Configuration on Ubuntu 22.04

Before delving into VLAN setup, it’s essential to ensure that your Ubuntu 22.04 system is up-to-date and that you have administrative privileges to make configuration changes. To utilize VLAN in Ubuntu, begin by installing the VLAN package. Ensure that the Linux kernel driver (module) named 8021q is loaded. Employ the following command to install VLAN:

sudo apt install vlan

If the module isn’t loaded after installation, you can utilize the following command to load the 8021q module.

modprobe --first-time 8021q

Confirm that the module is loaded by using the following command:

modinfo 8021q

The output of the command shown in the testing setup

foofunc@ubuntu-test:~$ sudo modinfo 8021q
filename:       /lib/modules/6.5.0-15-generic/kernel/net/8021q/8021q.ko
version:        1.8
license:        GPL
alias:          rtnl-link-vlan
srcversion:     0E3660BD241BCE5ED70FC2A
depends:        mrp,garp
retpoline:      Y
intree:         Y
name:           8021q
vermagic:       6.5.0-15-generic SMP preempt mod_unload modversions 
sig_id:         PKCS#7
signer:         Build time autogenerated kernel key
sig_key:        4C:28:B2:EE:6F:88:89:25:74:51:DD:47:DB:27:C2:81:96:A3:32:FD
sig_hashalgo:   sha512
signature:      23:6A:33:BD:CC:23:07:6B:AD:29:EE:8C:ED:27:D8:6A:69:13:AF:44:
		D1:AB:A9:31:C1:11:B8:DC:CB:36:8C:9D:CE:45:42:CC:37:3A:B6:F9:
		E8:28:F3:E5:42:B8:F4:C1:49:FE:0C:43:9A:FB:06:65:27:48:2C:58:
		AE:C1:B8:DE:83:64:26:93:71:BE:C8:D3:39:B4:35:5A:C5:75:D9:B2:
		33:D4:3D:67:2A:4C:28:16:27:9E:75:F0:65:1A:E0:2F:C3:4B:B7:35:
		82:A6:B1:13:B6:3D:84:52:E9:D7:AD:97:2C:5C:CE:11:A7:6C:5E:1B:
		9D:7A:27:FD:AB:8A:30:C4:6D:64:9F:B5:85:86:42:9C:7F:0B:59:98:
		07:19:EE:01:C8:4B:C9:95:70:DC:2B:E5:61:EE:9F:5D:D5:1D:BE:03:
		05:BB:70:9D:DF:CA:EE:C2:88:89:37:E4:2C:17:DB:84:9F:43:76:72:
		88:1F:F5:75:8F:9B:4F:71:3B:0C:D1:A5:BF:02:2B:1F:AF:2E:37:E3:
		C0:81:5D:F1:D0:2E:84:B6:EA:D1:4C:CC:C6:74:13:B1:3A:46:D9:1F:
		D7:EB:59:E6:11:44:5B:40:9D:58:71:7B:D3:87:11:D0:01:D3:93:38:
		50:C1:EC:65:2B:86:A2:3E:72:2E:A7:70:15:13:37:4D:C2:02:BF:0A:
		D0:7B:11:B2:A2:9B:70:34:E1:F6:A5:F3:24:BD:05:C0:F1:5F:EC:11:
		A2:C6:0E:AF:0A:F9:BA:3E:18:83:E5:BB:8C:E5:6F:6B:3E:1A:0F:46:
		C5:C0:50:8B:24:52:3B:EC:C5:9A:C1:D6:DA:3E:D8:41:5F:E2:E5:59:
		B5:85:24:AD:B1:F3:69:98:B4:22:5F:0F:50:7C:26:B4:34:57:C6:DD:
		0B:43:14:0F:8F:0A:0E:25:DD:F1:B1:36:62:D2:70:15:24:02:FB:CB:
		AE:57:F5:A9:8C:73:B7:2E:97:6D:90:D0:29:20:C8:CA:8C:46:62:5E:
		4F:93:3E:7E:27:BA:B4:1A:47:13:E9:4B:6E:83:BA:3F:3B:10:E2:CE:
		01:E1:26:02:2B:D8:42:CD:91:DF:83:DF:B4:46:96:F5:AA:0B:AA:BA:
		8D:77:8F:74:74:A0:E1:5C:1A:D3:A0:5D:BA:77:25:27:2C:A4:D7:9E:
		2F:F8:4C:79:16:19:66:63:76:1E:DB:E5:4B:D0:AA:59:FC:1B:6A:E1:
		33:49:90:A5:50:B6:23:40:65:EB:BA:D5:67:A9:FF:75:CA:64:28:70:
		72:CA:41:35:F1:2F:84:34:06:11:32:DC:ED:3A:12:60:53:27:DC:3B:
		F0:9E:DB:27:E2:9B:1F:E8:CE:63:49:04
foofunc@ubuntu-test:~$ 

VLAN configuration

Testing setup

In our testing setup, we aim to assign the following IP address to the VLAN interface.

  • IP address: 192.168.100.2
  • Subnet Mask : 255.255.255.0
  • Default gateway192.168.100.1
  • VLAN ID : 100
  • Device: enp1s0.100

Configure VLAN with IP command (Not Persistent)

First, we will create the VLAN interface enp1s0.100. Then, we will add the IP address to the interface using the following commands.

sudo ip link add link enp1s0 name enp1s0.100 type vlan id 100

Use the “ip a” command to verify whether the above command has added the VLAN interface. The output is provided below:

foofunc@ubuntu-test:~$ 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: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:31:d2:28 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.89/24 metric 100 brd 192.168.122.255 scope global dynamic enp1s0
       valid_lft 3346sec preferred_lft 3346sec
    inet6 fe80::5054:ff:fe31:d228/64 scope link 
       valid_lft forever preferred_lft forever
3: enp1s0.100@enp1s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 52:54:00:31:d2:28 brd ff:ff:ff:ff:ff:ff
foofunc@ubuntu-test:~$

The following “ip ” command will add an IP address to the VLAN interface. It functions similarly to the standard “ip” command used for regular interfaces, except here it pertains to a virtual interface.

sudo ip addr add 192.168.100.2/24 dev enp1s0.100

Verify

List all interfaces on the machine and verify whether the IP address is added to the VLAN interface or not. In the provided command output, you can observe that the IP address 192.168.100.2 is assigned to the interface enp1s0.100.

Note*: This configuration is not permanent. For permanent configuration, follow next section “Set up VLAN using Netplan”.

foofunc@ubuntu-test:~$ sudo ip addr add 192.168.100.2/24 dev enp1s0.100
foofunc@ubuntu-test:~$ 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: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:31:d2:28 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.89/24 metric 100 brd 192.168.122.255 scope global dynamic enp1s0
       valid_lft 3310sec preferred_lft 3310sec
    inet6 fe80::5054:ff:fe31:d228/64 scope link 
       valid_lft forever preferred_lft forever
3: enp1s0.100@enp1s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 52:54:00:31:d2:28 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.2/24 scope global enp1s0.100
       valid_lft forever preferred_lft forever
foofunc@ubuntu-test:~$ 

Set up VLAN using Netplan (Persistent)

Netplan-related configuration files can be found in the /etc/netplan directory. The /etc/netplan directory contains multiple YAML files. In our Ubuntu 22.04 server, the YAML file responsible for network configuration is named 00-installer-config.yaml.

The network configuration file may have different names; in some setups, it is named 01-netcfg.yaml, while in others, it is called 50-cloud-init.yaml.

To assign a static IP address to the network interface, open the 00-installer-config.yaml configuration file. The configuration file appears as shown in the snapshot below.

# This is the network config written by 'subiquity'
network:
  ethernets:
    enp1s0:
      dhcp4: true
  version: 2

  vlans:
    enp1s0.100:
      id: 100
      link: enp1s0
      addresses: [192.168.100.2/24]

After making the changes, save the file and apply them by executing the following command:

sudo netplan apply

Verify

Use the ip a command to list all interfaces in the machine. To see the details of the enp1s0.100 vlan interface.

foofunc@ubuntu-test:~$ sudo netplan apply
foofunc@ubuntu-test:~$ 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: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:31:d2:28 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.89/24 metric 100 brd 192.168.122.255 scope global dynamic enp1s0
       valid_lft 3599sec preferred_lft 3599sec
    inet6 fe80::5054:ff:fe31:d228/64 scope link 
       valid_lft forever preferred_lft forever
3: enp1s0.100@enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 52:54:00:31:d2:28 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.2/24 brd 192.168.100.255 scope global enp1s0.100
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe31:d228/64 scope link 
       valid_lft forever preferred_lft forever

Summary

In this article, we learned about the VLAN and how to configure the VLAN in Ubuntu 22.04. We discussed two different strategies, one non persistent and other persitent to configure the VLAN in Ubuntu. Setting up VLANs on Ubuntu 22.04 can significantly enhance network organization and security. By following this comprehensive guide, you’ll be able to configure VLANs with confidence, optimizing your Ubuntu 22.04 environment for efficient networking.

Leave a Comment

Your email address will not be published. Required fields are marked *