Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

WSL2中使用网络代理配置 #47

Closed
citrus327 opened this issue Jan 5, 2023 · 0 comments
Closed

WSL2中使用网络代理配置 #47

citrus327 opened this issue Jan 5, 2023 · 0 comments

Comments

@citrus327
Copy link
Owner

citrus327 commented Jan 5, 2023

WSL2中使用网络代理配置

背景

最近电脑升级了windows11且卸载了ubuntu系统,家里开发环境就顺带移步到WSL2。之前就听说wsl2的整体网络设计不同于wsl1非常麻烦,一直在逃避搞代理这回事儿。现在由于npm publish速度实在堪忧,设置代理这关是逃不掉了,前后折腾了2个小时总算搞明白了。在此记录。

过程

  1. 关闭自动更新dns nameserver
#/etc/wsl.conf
[network]
generateResolvConf = false
  1. 添加以下脚本至.bashrc.zshrc
# add for proxy
export hostip=$(ip route | grep default | awk '{print $3}')
export hostport=1011#根据代理IP自行填入
alias proxy='
    export HTTPS_PROXY="http://${hostip}:${hostport}";
    export HTTP_PROXY="http://${hostip}:${hostport}";
    export ALL_PROXY="http://${hostip}:${hostport}";
    echo -e "Acquire::http::Proxy \"http://${hostip}:${hostport}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf > /dev/null;
    echo -e "Acquire::https::Proxy \"http://${hostip}:${hostport}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf > /dev/null;
'
alias unproxy='
    unset HTTPS_PROXY;
    unset HTTP_PROXY;
    unset ALL_PROXY;
    sudo sed -i -e '/Acquire::http::Proxy/d' /etc/apt/apt.conf.d/proxy.conf;
    sudo sed -i -e '/Acquire::https::Proxy/d' /etc/apt/apt.conf.d/proxy.conf;
  1. 执行proxy设置代理

  2. 每次重启wsl后, /etc/resolv.conf中的内容会被重置,所以需要执行以下内容:

#https://github.com/microsoft/WSL/issues/5420#issuecomment-646479747
sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
sudo bash -c 'echo "[network]" > /etc/wsl.conf'
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'
sudo chattr +i /etc/resolv.conf
  1. 为代理软件设置防火墙放行
    win11下通过防火墙和网络保护 - 允许应用通过防火墙,将v2ray(根据个人情况而定,我用得是v2ray)设置为放行
    image
    image

  2. v2ray设置允许局域网访问
    image

  3. 使用curl测试是否成功

curl -vv google.com

References

Troubleshooting

  1. 占位程序接收到错误数据 - 管理员权限下执行netsh winsock reset 重启电脑解决。
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant