WSL: WSL2 system_proxy-to-env_var translation fails to translate 127.0.0.1, making local proxy unreachable
Windows Version
Microsoft Windows [Version 10.0.22631.2129]
WSL Version
1.3.15.0
Are you using WSL 1 or WSL 2?
- WSL 2
- WSL 1
Kernel Version
5.15.90.4-microsoft-standard-WSL2 (root@4b885f886b45)
Distro Version
Debian GNU/Linux trixie/sid
Other Software
Clash For Windows, v0.20.12
Repro Steps
- Use CFW to set up a local socks + http proxy.
- Tick the “system proxy” box in CFW to make it change Windows proxy settings.
- Launch WSL2.
- Run
apt update.
Expected Behavior
apt update should go through my proxy.
Actual Behavior
Output similar to
E: Failed to fetch http://mirrors.ustc.edu.cn/debian/pool/main/a/aalib/libaa1_1.4p5-50_amd64.deb Unable to connect to 127.0.0.1:7890:
Diagnostic Logs
This part is not useful. What is useful is:
$ env | grep 7890
https_proxy=http://127.0.0.1:7890
HTTPS_PROXY=http://127.0.0.1:7890
HTTP_PROXY=http://127.0.0.1:7890
http_proxy=http://127.0.0.1:7890
See? WSL took the Windows proxy settings into Debian, but forgot that 127.0.0.1 means something else here. The snippet in /etc/profile.d/proxy.sh fixes the problem in shell, but not in WSL VSCode:
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY
export all_proxy="socks5://127.27.16.1:7890"
Recommended fix
All forms of localhost, [::1], 127.0.0.1 should be replaced with a reference to the parent machine, preferably using a hostname, and not this ad-hoc nameserver trick currently documented.
$HOSTNAME.local is almost sufficient: my Windows hostname being “Arisu”, running nslookup arisu.local and nslookup arisu does correctly return the needed name 127.27.16.1, except also returns a lot of useless addresses for other Windows network adapters like “192.168.56.1”, “192.168.53.2”, “172.19.64.1”, and finally my LAN “10.0.1.212”.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 32 (2 by maintainers)
Why do you just have to write some incoherent machine-translated comment?
OF COURSE I KNOW WSL2 makes a local network. That’s the whole reason why I am requesting a translation, because an address other than 127.0.0.1 is needed. I have done manual translations that work, but it should be automated, because the feature is otherwise broken.
First of all, I’ve managed to make it work, so I’m suffering from none of these user issues. Secondly, my WSL opens straight to root. What sudo?
Yes that’s the problematic translation mechanism.
Your condescending “advice” only shows that you don’t understand the situation. Take this stuff somewhere else.
本处的回答文不对题,造成了困扰,表示歉意。即将移除该回答。感谢题主指正。
------------------------------Ready for removal ↓-----------------------------
@Artoria2e5 你这边其实主要是两个问题。第一个问题是wsl的流量给到windows代理的问题,你可以参考[#4402] 。总的来说你要把访问宿主机当作访问局域网中的电脑来看待。寻找正确的IP、允许防火墙通过、代理允许LAN访问。
@Artoria2e5 You make two troubles. First of all, the traffic from WSL shall be handle on the proxy software which host in windows exactly, across the LAN(local area network). Check for the correct IP address, being traffic allowed by firewall, and also LAN access allowed by proxy software. See [#4402].
第二个问题是你对环境变量的概念有些模糊,所以虽然添加了环境变量
HTTP_PROXY等等,但是apt update仍然无法生效。分析如下。Secondly, there is some confusion over the environment variables. Though the env-values such as
HTTP_PROXYis exported to the shell environment space,apt updatedoes not work. Follow me.问题的关键 The key to the question
我知道你这是只是省略的写法,但是你实际运行的命令准确的是
sudo apt update。请注意,当你使用sudo指令的时候,你使用的不仅仅是更高的用户权限,还包括root用户的用户空间数据,其中就包含了环境变量数据。也即是,你在普通用户环境中运行的命令:This is omitted. The full command is
sudo apt update. Look out that you are higher user access allowed at the meanwhile the shell switch to root user space when you runsudoprefix command. In short, commands run in normal user space:这几个环境变量的添加是有效的,但是只是在普通用户的用户空间里。当你使用
sudo指令的时候已经切换到root用户空间了,读取的是root用户空间的环境变量,当然看不到你在普通用户空间中设置的环境变量。It works only in normal user space. Because of switching from normal user space to root user spece, which happened by
sudocommand, you are not allowed access the env-values set in normal user space.验证 Verifying
你看一下我用bash shell进行的验证:
verifying by bash shell:
或者
or
同理,你在root用户空间设置的环境变量在普通用户空间也看不到,这里就不做验证了。
When you try to access the root user space env-values from normal user user space, the result is similar. Skip.
解决 solution
先切换到root用户空间再设置代理环境变量,然后执行
apt update:Switch to root user space before exporting the proxy env-values. Then run
apt update.实测有效。
In my case, it works well.
嫌麻烦的话建议用一下[proxychains]工具,可以这样:
[proxychains] is recommended for convenience. Like:
免去了切换用户空间的麻烦。
Less switching over user space.
没有,我目前也是用了一种不太优雅的方法解决的。
我在上层路由固定了本机的IP地址,然后在vscode中Http: Proxy设置为固定的ip + 7890端口。
通过这种方式,vscode插件确实可以联网了,并且可以使用本机代理,非常的不优雅,但是有用,😂
Mirrored 的结果是为了保持ip和主机一致啊,如果没生效后面的也没必要查了,至少我这mirrored加上hostAddressLoopback=true后,vscode server不会有问题😂
tohsaka888 is obviously in WSL2 because he described being unable to reach 127.0.0.1:7890. He confirmed that Rollinon’s solution, which is WSL2-only, works, for Fred’s sake.
In other news, I’ve found a way to clear that env var in vscode:
That changes the
code-servershell script to unset the variables explicitly. Theall_proxyexport should also fix copilot (& chat), because vscode, like chromium, takes proxies from env var. You will need to re-run it for each upgrade.Do make sure your VPN exit node is not blocked by copilot. I tried it on copilot chat and it took it quite a while.
Oooohh thumbs down! very scary!