From WSL2 to VirtualBox VM
Right now (April 2025) Microsoft permits to configure WSL2 in mirrored networking (and DNS Tunnelling) mode:
[wsl2]
networkingMode=mirrored
dnsTunneling=true
That's useful for access a webserver you run within your WSL2 instance in localhost from the host machine. E.g. a simple python webserver executed like this from the command line:
python -m http.webserver
it's accesible from a browser on the host machine at the address http://localhost:8000/
.
But... What if you you are developing a webserver within your WSL2 instance and you needs to test this webserver within a VirtualBox VM?
A simple solution is:
check for the existance of a dedicated Host-Only Ethernet Adapter created by VirtualBox; its DHCP Server will use a IP/netmask and DNS configuration like this:
IPv4 Address 192.168.99.1 IPv4 Network Mask 255.255.255.0 Server Address 192.168.99.50 Server Mask 255.255.255.0 Lower Address Bound 192.168.99.99 Upper Address Bound 192.168.99.254 Install the Hyper-V Service, command line tool and GUI tools. Then following this guide we can create an external switch that use our existing VirtualBox Host-Only Ethernet Adapter.
that's the result with ipconfig from a powershell session:
cmdEthernet adapter vEthernet (hyperv-to-virtualkbox): Connection-specific DNS Suffix . : Link-local IPv6 Address . . . . . : fe80::aa:bb:cc:aaf%28 IPv4 Address. . . . . . . . . . . : 192.168.99.1 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . :
In the end it's possible the connect from the WSL2 instance to the VirtualBox VM like this:
bashssh [email protected]
Where 192.168.99.51
is the VM ip. See also this article about ssh forwarding.