“Everything is created twice, first in the mind and then in reality.”

— Robin Sharma

Prologue

WSL (Windows Subsystem for Linux) introduces Linux features on Windows, which is really awesome. However, sometimes you may run into network connectivity issues, especially when you use a proxy in Windows.

For example, you may not be able to connect to the Internet in WSL, or have to manually set proxy, which you enabled in Windows.

So in this article, I’m going to show you a preferred network configuration for WSL.

Network Configuration

For more detailed information on mirrored mode, go Mirrored mode networking.

To get a better network experience in WSL, I recommend you enable mirrored mode. It will ‘mirror’ the network interfaces that you have on Windows into Linux, so you can connect to the Internet just like you do in Windows.

To enable this, you need to add some entries in the WSL configuration file. It should be C:\Users\{Username}\.wslconfig, or you can access it with environment variable %USERPROFILE%\.wslconfig. Create one if it doesn’t exist.

Now open .wslconfig, It uses the syntax of .ini file. First of all, add a section [wsl2], then all our configurations should be placed under it.

Enable Mirror Mode

To enable mirrored mode, add networkingMode=mirrored in [wsl2]. Then, you should add some firewall rules in Windows.

Open PowerShell with admin privileges to Configure Hyper-V firewall settings to allow inbound connections.

The simplest way is to allow all connections by running the following command.

1
Set-NetFirewallHyperVVMSetting -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -DefaultInboundAction Allow

However, if you want to allow specific port or port range, things will get a little more complicated. For example, if we want to allow TCP on port 80, you should run the following command. Replace {Rule Name} and {Display Name} with your own.

1
New-NetFirewallHyperVRule -Name "{Rule Name}" -DisplayName "{Display Name}" -Direction Inbound -VMCreatorId '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -Protocol TCP -LocalPorts 80

Now, you should have a smoother network connection.

Auto Proxy

If you use proxy on Windows when WSL is running, you may receive notification from WSL reporting HTTP proxy change. WSL can detect Windows’s proxy, but you need to apply them manually. It can be a annoying problem, so you can enable auto proxy for WSL to make the proxy be set automatically in WSL.

To enable this, simply add autoProxy=true under [wsl2].

So eventually, your .wslconfig will look like this.

1
2
3
[wsl2]
networkingMode=mirrored
autoProxy=true

Now, shutdown your WSL (if it is running during configuration) using wsl --shutdown in PowerShell, and restart it. 🎉


Epilogue

If you want to know more about WSL configuration, go Advanced settings configuration in WSL.

Well, with these configurations, you should have a better network experience in WSL. ᓚᘏᗢ