rss logo

Configure a VLAN Trunk on a Hyper-V VM

Hyper-V VLAN trunk configuration illustration

On a Microsoft Hyper-V hypervisor, I needed to configure a VLAN trunk on a virtual machine used as a Linux router. The goal was to allow this VM to route traffic between other virtual machines and external workstations located on different subnets.

This configuration is not exposed directly in the standard Hyper-V graphical interface. To enable VLAN trunking on a VM network adapter, we need to use PowerShell (Don't panic, it's only two lines!).

In this example, my Hyper-V host is running Windows Server 2019 (yes, I live in the past…) but the same approach should also apply to more recent versions of Windows Server. Although the VM is used here as a Linux router, this configuration can be useful in any situation where a Hyper-V virtual machine needs access to multiple VLANs.

Network Architecture Diagram

In this example, a Linux router VM is connected to the Hyper-V virtual switch through a VLAN trunk. A second virtual machine is placed on VLAN 10 and uses the Linux router VM to communicate with other networks.

💡 Note: If the VLANs must reach the physical network, the physical switch port connected to the Hyper-V host must also be configured as a trunk port and must allow the same VLANs.

Hyper-V VLAN trunk network architecture with a Linux router VM and a VLAN 10 virtual machine
Hyper-V network architecture using a VLAN trunk connected to a Linux router VM and a VLAN 10 virtual machine.

Linux Router Configuration

💡 Note: The following commands are temporary and are mainly intended for testing. For a production setup, you should make the configuration persistent using your Linux distribution's network configuration files, such as /etc/network/interfaces on Debian-based systems, or an equivalent network configuration method.

On the Linux router VM, we first need to enable VLAN support and IPv4 forwarding. In this example, the trunk interface is eth0, and the VLAN interface will be created for VLAN 10.

  • Load the 802.1Q kernel module and enable IPv4 forwarding:
root@host:~# modprobe 8021q
root@host:~# echo 1 > /proc/sys/net/ipv4/ip_forward

💡 Note: The interface name eth0.vlan10 is arbitrary. A common alternative is eth0.10.

  • Create a VLAN subinterface on eth0 for VLAN 10:
root@host:~# ip link add link eth0 name eth0.vlan10 type vlan id 10
  • Bring up the new eth0.vlan10 interface:
root@host:~# ip link set eth0.vlan10 up
  • Assign an IP address to the eth0.vlan10 interface:
root@host:~# ip addr add 192.168.10.254/24 dev eth0.vlan10

Enable VLAN Trunking on Hyper-V

Now that the Linux router VM is configured, we need to enable VLAN trunking on its Hyper-V network adapter. This setting is not available in the standard Hyper-V graphical interface, so it must be configured from an elevated PowerShell console.

  • List the network adapters attached to the virtual machine named router and retrieve the MAC address of the adapter on which you want to enable VLAN trunking:
PS C:\ > Get-VMNetworkAdapter -VMName router

Name            IsManagementOs VMName  SwitchName MacAddress   Status IPAddresses
----            -------------- ------  ---------- ----------   ------ -----------
Network Adapter False          ROUTER  vSwitch1   0015DEADBEE5 {Ok}   {}
  • Using the MAC address retrieved above, enable VLAN trunking on the VM network adapter:
PS C:\ > Get-VMNetworkAdapter -VMName router | Where-Object { $_.MacAddress -eq '0015DEADBEE5' } | Set-VMNetworkAdapterVlan -Trunk -AllowedVlanIdList 1-100 -NativeVlanId 1

💡 Note: In this example, -AllowedVlanIdList 1-100 allows VLAN IDs from 1 to 100 on the trunk. The -NativeVlanId 1 value means that untagged traffic received by the VM network adapter is associated with VLAN 1. Tagged traffic for the VLANs listed in -AllowedVlanIdList is passed to the VM with its VLAN tags. Adjust these values according to your own network design.

Configure VLAN Access on the Hyper-V VM

Now that VLAN trunking is enabled on the Linux router VM, we can configure another virtual machine to communicate with the router through VLAN 10.

  • Open the settings of the virtual machine, go to the Network Adapter section, connect it to vSwitch1, enable Virtual LAN identification, and set the VLAN ID to 10:
Hyper-V VM network adapter settings showing vSwitch1 selected and VLAN ID 10 enabled
Hyper-V VM network adapter connected to vSwitch1 with VLAN ID 10 enabled.

The virtual machine should now be able to communicate with the Linux router through VLAN 10.