Wsl设置代理

WSL2不能共享Windows的代理策略,需要WSL2配置代理

获取宿主机IP

1
2
3
## 获取主机 IP
## 主机 IP 保存在 /etc/resolv.conf 中
export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver ).*')

终端设置代理

1
2
3
4
5
6
7
export https_proxy="http://${hostip}:7890";
export http_proxy="http://${hostip}:7890";

export http_proxy="socks5://${hostip}:7890"
export https_proxy="socks5://${hostip}:7890"

export all_proxy="socks5://${hostip}:7890"

设置别名使用

1
2
3
4
5
6
7
8
9
10
11
12
13
# proxy
export hostip=$(ip route | grep default | awk '{print $3}')
export hostport=7890
alias proxy='
export HTTPS_PROXY="http://${hostip}:${hostport}";
export HTTP_PROXY="http://${hostip}:${hostport}";
export ALL_PROXY="http://${hostip}:${hostport}";
'
alias unproxy='
unset HTTPS_PROXY;
unset HTTP_PROXY;
unset ALL_PROXY;
'