Since I’ve been frequently downloading various models with Python recently, I finally decided to solve the long-standing issue of WSL2 not being able to use the Windows host’s proxy. After wasting half an afternoon, I finally got it working. With the help of my capable assistant Gemini, the main steps are as follows:
.wslconfigsettings in Windows- Core settings of v2rayN
- Firewall settings in Windows
- Cleaning up old settings in WSL2
~/.bashrcsettings in WSL2curl -vtesting in WSL2
References:
Advanced settings configuration in WSL | Microsoft Learn
WSL2 使用 V2RayN 局域网 proxychains 代理方案 · Issue #2653 · 2dust/v2rayN
记一次用wsl2中共享宿主机的代理-v2rayN - 沉迷于学习,无法自拔^_^
.wslconfig
Access your personal account folder in Windows. Press
Win + R, enter%UserProfile%, and hit Enter.Check if there is a
.wslconfigfile. If not, create a new text file and name it.wslconfig.Paste the following content into
.wslconfig:1 2 3 4 5 6 7[wsl2] # Enable mirrored networking mode networkingMode=mirrored # Allow WSL2 to access Windows localhost localhostForwarding=true # Automatically synchronize proxy settings (optional: true/false) autoProxy=trueNote that the
autoProxyparameter determines how WSL2 handles the proxy. Setting it totruemeans you don’t need to configure~/.bashrcanymore, but the problems are:- When we use the
env | grep -i proxycommand, we will see many strange network-related variables, even though the proxy does successfully work. - The proxy cannot be toggled on or off within WSL2, causing a lot of traffic to go through the proxy unnecessarily.
Later, we will set it to
falseso that we can easily control the proxy switch inside WSL2, ensuring a clear and transparent WSL2 system.- When we use the
In the Windows terminal, enter
wsl --shutdownto shut down WSL2.
v2rayN
- In the basic settings of v2rayN (version V7.15.7 at the time of writing), enable “Allow connections from the LAN” and “Open a new port for the LAN” (optional).
- At the bottom left of the v2rayN client’s main interface, you can see the port open for the internet, which is
10810in my case. - The system proxy here is “Set system proxy automatically”, and the routing mode is “Bypass (Whitelist)”.
- Then select a node and keep v2rayN running.
FireWall
- Type “firewall” in the Windows search box and select Windows Defender Firewall.
- Click “Allow an app or feature through Windows Defender Firewall”.
- Find or add
v2rayN.exeand its core programs (such asv2ray.exeorxray.exe), ensuring that both Private and Public are checked. The usual paths are underv2rayN\andv2rayN\bin\xray, respectively. - In the advanced settings of the firewall, create a new inbound rule to allow TCP traffic on port
10810(corresponding to v2rayN).
wsl2
Cleaning Up Old Settings
| |
Ensure that you have enabled mirrored networking mode in the first step and successfully restarted WSL2. Of course, this step is not strictly necessary either, because ~/.bashrc will automatically handle these old variables later.
~/.bashrc
| |
After multiple experiments with Gemini, here is the content to append to the bottom of ~/.bashrc:
| |
After saving the above content, enter:
| |
And then:
| |
The following output should appear, indicating a successful connection:
I also tested it with ping, which didn’t work but doesn’t affect its usability.
| |
I tried downloading the model again, and it successfully worked:
| |
Solution If It Still Fails
The most effective method is, after completing the above steps, to enter the following in WSL2:
| |
Then feed the output result to an AI, and it will tell you what to do.