wsl配置代理

wsl1适用版:

1
2
3
4
5
6
7
8
9
10
11
12
13
hostip="127.0.0.1"
socks_hostport=10808
http_hostport=10809
export all_proxy="socks5://${hostip}:${socks_hostport}"
export http_proxy="http://${hostip}:${http_hostport}"
export https_proxy="http://${hostip}:${http_hostport}"
export ftp_proxy=$http_proxy
export rsync_proxy=$http_proxy
export ALL_PROXY=$all_proxy
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy
export FTP_PROXY=$ftp_proxy
export RSYNC_PROXY=$rsync_proxy

特点:
因为wsl1在逻辑上与宿主机属于相同的主机,ip直接使用127.0.0.1

wsl2适用版:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Set network proxy
hostip=$(ip route | grep default | awk '{print $3}')
socks_hostport=10810
http_hostport=10811
export all_proxy="socks5://${hostip}:${socks_hostport}"
export http_proxy="http://${hostip}:${http_hostport}"
export https_proxy="http://${hostip}:${http_hostport}"
export ftp_proxy=$http_proxy
export rsync_proxy=$http_proxy
export ALL_PROXY=$all_proxy
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy
export FTP_PROXY=$ftp_proxy
export RSYNC_PROXY=$rsync_proxy
export no_proxy="localhost,127.0.0.1"

特点:
因为wsl2在逻辑上与宿主机属于不同的主机,
使用ip route | grep default | awk '{print $3}'获取宿主机ip
另外需要设置no_proxy,否则wsl2运行的程序试图访问wsl2本机时也会被错误转发到宿主机代理然后访问成宿主机,导致一些程序出错。

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

请我喝杯咖啡吧~

支付宝
微信