Set up a router VM in hyper-v

I have a test VM I want it to go through a routing VM. And it is possible to WSL2 to through the router because WSL2 uses Hyper-V architecture to enable its virtualization.

Build the router Virtual Machine

I install Minimal Debian; it takes 3gb of hard disk space, at least 512MB of memory to run. follow this Debian Installation Guide

Set up virtual switches

after installing Debian, I set up a custom virtual switch that allows us to manage IP addresses ourselves.

hyper01

Click Virtual Switch Manager on the right side plane

hyper02 choose internal, then create Virtual Switch Manager

hyper03 set a name, then click OK

hyper04 change Debian adapter to you just created one. chose Debian and click setting

hyper05 now, choose network adapter. then on the right side, choose your created adapter

Set up a NAT network

Now open window PowerShell(win key + a), type those commands.

  1. Get-NetAdapter show all adapter. ifIndex is the interface index of the virtual switch; this value will use in the next step.
  2. New-NetIPAddress set IP to an adapter, change InterfaceIndex to your adapter index
  3. New-NetNAT create NAT network
Get-NetAdapter
New-NetIPAddress -IPAddress 10.0.1.1 -PrefixLength 24  -InterfaceIndex 61
New-NetNAT -Name "NATNetwork" -InternalIPInterfaceAddressPrefix 10.0.1.0/24

hyper06 Get-NetAdapter and find ifindex

now, I have a gateway IP 10.0.1.1, but there is not DHCP server router VM doesn't have an IP address, to add static IP open /etc/network/interfaces. copy and paste those line, then reboot, the network should work now.

allow-hotplug eth0
iface eth0 inet static
      address 10.0.1.2
      netmask 255.255.255.0
      gateway 10.0.1.1

Connect WSL2 to router VM

after WSL2 starts, it creates an adapter vEthernet (WSL) that has its own IP address different than I made network ID, so WSL2 can't connect to router VM directly. What If adding a WSL adapter to router VM? they have the same network ID and connect directly. The problem becomes how to set static IP to WSL2. one solution is adding a second IP address for wsl adapter, which is the same network ID in router VM. so what I want is that: hyper07

First add WSL adapter to router VM as before, then add second static IP to /etc/network/interfaces.

allow-hotplug eth1
iface eth1 inet static
      address 172.30.48.2
      netmask 255.255.240.0

Next, adding static to WSL adapter, open PowerShell

netsh interface ip add address "vEthernet (WSL)" 172.30.48.3 255.255.240.0;

open WSL terminal run 172.30.48.2, it should work. then setting WSL route by those commands

sudo ip addr add 172.30.48.3/20 broadcast 172.30.63.255 dev eth0 label eth0:1 ;
sudo ip route del default
sudo ip route add default via 172.30.48.2 dev eth0

finally, Enable IP forwarding in router VM

echo 0 > /proc/sys/net/ipv4/ip_forward
sysctl -p /etc/sysctl.conf

Run a startup

the second IP of the WSL adapter and IP route in wsl is not persistent; it is lost after restarting system. This can be done by Task Scheduler.

Wrapping up

if I have several virtual Machines within Hyper-V, it just natural I want to access those machines using WSL2. connecting those machine to the router, running some services on router like pihole

All articles written with ❤ by apSomething using Gatsby, Emotion, and hosted with cloudflare pages.