WSL2网络代理配置

yuanheci 2023年11月29日 255次浏览

关闭wsl:
wsl --shutdown
查看wsl中的系统:
wsl -l -v

配置WSL2
  WSL2的网络相当于windows10主机下的一个子网,WSL2 linux被分配了1个子网中的IP,win10主机会增加1个虚拟接口,这个接口配置的IP是该子网的路由的IP,win10主机成了子网的路由。当子网需要访问外界网络的时候,默认会通过win10主机作为路由来上网,因此linux中的机器如果需要配置代理,使用到windows本地代理软件,代理IP均把localhost改成子网路由地址。

  终端配置代理不是127.0.0.1!而是WSL2与宿主机通信的172的那个网段(可以在宿主机中用ipconfig查询),端口号就是CFW的端口7890。

  注意要关闭windows对WSL的防火墙。

Clash for windows终端代理配置:

配置 ~/.bash_profile
//注意git部分代理要特别加上,否则代理git时会报错
source ~/.bashrc

function proxy_on() {
    export http_proxy="http://172.19.80.1:7890"
    export https_proxy="https://172.19.80.1:7890"
    git config --global http.proxy http://172.19.80.1:7890
    git config --global https.proxy http://172.18.80.1:7890
    echo -e "终端代理已开启。"
}

function proxy_off(){
    unset http_proxy https_proxy
    git config --global --unset http.proxy
    git config --global --unset https.proxy
    echo -e "终端代理已关闭。"
}

打开终端代理: proxy_on
关闭终端代理: proxy_off

如果这样打开代理后git还报错,那就针对git在终端中再运行一遍命令。
git config --global http.proxy http://172.19.80.1:7890
git config --global https.proxy http://172.18.80.1:7890