WSL2 Networking Mirroring & IPv6: Removing IP Barriers to Turn Your VM into a Real Server

Virtualization tutorial - IT technology blog
Virtualization tutorial - IT technology blog

The Frustration of Default Networking in WSL2

Anyone who has used WSL2 for a long time has likely been driven crazy by the IP address changing every single time the machine restarts. The default NAT (Network Address Translation) mechanism turns WSL2 into an isolated island. Want to point to a hosts file, connect to a database from the host machine, or run services that require a static IP? Get ready to update your configuration every day.

I run a homelab with 12 VMs on Proxmox. When moving to WSL2, I always expected a consistent network experience similar to a real virtual machine. Fortunately, Microsoft released the Networking Mirroring feature. It completely eliminates the annoying NAT layer between Windows and Linux.

Networking Mirroring: When Windows and Linux Become One

Instead of sitting behind a virtual router, Mirroring mode turns the Windows network interface into a direct mirror for WSL2.

  • Shared IP Address: If Windows gets the IP 192.168.1.15, WSL2 gets the exact same one. You no longer have to struggle with manual port forwarding.
  • Full IPv6 Support: You can now test dual-stack applications right on your local machine.
  • Localhost Connectivity: Services running on Windows can be accessed from WSL2 via localhost with near-zero latency.
  • Superior Speed: Significantly reduces overhead since packets don’t need to be encapsulated through a NAT layer.

Prerequisites for the Upgrade

This feature is not available for older Windows versions. You will need:

  • Windows 11 version 22H2 or higher (Build 22621+).
  • WSL version 2.0.0 or newer. Check quickly using the command wsl --version.

Configuration Steps in 2 Minutes

Step 1: Open the .wslconfig file

All WSL2 system settings are located in a single text file in the user directory. Press Win + R, type %USERPROFILE%, and hit Enter. Create a new file named .wslconfig if it doesn’t already exist.

Step 2: Enable Mirroring and IPv6

Paste the following code into the .wslconfig file using Notepad or VS Code:

[wsl2]
# Switch to network interface mirroring mode
networkingMode=mirrored

# Enable IPv6
ipv6=true

# Fix DNS issues when using a VPN (such as Cisco AnyConnect or GlobalProtect)
dnsTunneling=true

# Automatically open Windows Firewall for WSL2
firewall=true

# Synchronize Proxy configuration from Windows to Linux
autoProxy=true

The line networkingMode=mirrored is the game-changer here. Additionally, dnsTunneling is crucial if you frequently work remotely. It prevents WSL2 from losing internet connectivity whenever you toggle a VPN on the host machine.

Step 3: Restart the System

After saving the file, you need to shut down WSL2 completely to apply the new configuration. Open PowerShell and type:

wsl --shutdown

Now, simply reopen Ubuntu and everything is ready.

Verifying the Results

Checking the New IP Address

Run this command in the WSL2 terminal:

ip addr

You will see a list of network interfaces identical to those on Windows. Both Wi-Fi and Ethernet will appear. If your router supports IPv6, WSL2 will also receive a corresponding global address.

Testing IPv6 in Practice

Check your connection to the outside world using the ping command:

ping -6 google.com

If you see responses coming back, it means your development environment now fully supports dual-stack networking.

Real-World Tips and Considerations

After 6 months of using Mirroring mode, here are 3 things to keep in mind:

  1. Avoid Port Conflicts: Since they share the same IP, if you run Nginx on port 80 on Windows, WSL2 cannot use port 80 anymore. Manage your ports as if you were using a single physical server.
  2. Windows Firewall: Sometimes the firewall still blocks inbound connections from other devices on the LAN. If you set up a web server in WSL2 and other devices can’t access it, check your Inbound Rules.
  3. Docker Desktop: Update Docker to the latest version. Older versions may encounter network errors when switching to Mirroring mode.

Using standard network emulation helps me detect connection errors right on my local machine. It saves hours of debugging when pushing code to Production environments on the Cloud or Proxmox.

Conclusion

Configuring Networking Mirroring is something you should do immediately if you want to turn WSL2 into a professional workstation. This setup not only fixes VPN-related connectivity issues but also provides a much smoother networking experience. Try configuring it today, and you’ll find that managing IPs on WSL2 is no longer a nightmare.

Share: