Setting up VLANs (Virtual Local Area Networks) on Linux can streamline your network management. Here’s a compact guide to configuring VLANs in trunk mode.
Step 1: Load the 8021q Kernel Module
sudo modprobe 8021q
Step 2: Create VLAN Interfaces
VLAN 12:
sudo ip link add link eno2 name vlan12 type vlan id 12
sudo ip link set dev vlan12 up
sudo ip addr add 10.2.2.46/24 dev vlan12
sudo ip route add default via 10.2.2.1 dev vlan12
VLAN 11:
sudo ip link add link eno2 name vlan11 type vlan id 11
sudo ip link set dev vlan11 up
sudo ip addr add 192.168.59.46/24 dev vlan11
sudo ip route del default via 10.2.2.1 dev vlan12
sudo ip route add default via 192.168.59.1 dev vlan11
Step 3: Verify Interfaces
ip addr show vlan12
ip link show eno2
ip link show vlan11
Step 4: Make Configurations Persistent
VLAN 11:
sudo vim /etc/sysconfig/network-scripts/ifcfg-vlan11
Add the following content:
DEVICE=vlan11
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.59.46
PREFIX=24
VLAN=yes
PHYSDEV=eno2
VLAN 12:
sudo vim /etc/sysconfig/network-scripts/ifcfg-vlan12
Add the following content:
DEVICE=vlan12
BOOTPROTO=none
ONBOOT=yes
IPADDR=10.2.2.46
PREFIX=24
VLAN=yes
PHYSDEV=eno2
Step 5: Restart Network Services
sudo systemctl restart NetworkManager
Step 6: Verify VLAN Interfaces
ip addr show vlan11
ip addr show vlan12
Step 7: Configure Static Routes
Route for VLAN 11:
sudo vim /etc/sysconfig/network-scripts/route-vlan11
Add the default route:
default via 192.168.59.1 dev vlan11
(Optional) Route for VLAN 12:
sudo vim /etc/sysconfig/network-scripts/route-vlan12
Add the default route:
default via 10.2.2.1 dev vlan12