请教我设置 WSL2 网络代理

326 天前
 tuoov

尝试给 WSL 设置代理,试了这两种方法都没有成功,哪位 V 友能帮我指点指点?

一、设置 wslconfig

[wsl2]
networkingMode=mirrored
firewall=true
dnsTunneling=true
autoProxy=true

设置之后启动 WSL 报错: 不支持镜像网络模式,正在回退到 NAT 网络

二、设置 http_proxy

host_ip=$(cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ")

export http_proxy='http://192.168.10.1:18888'
export https_proxy='http://192.168.10.1:18888'

设置之后执行 wget www.google.com, 一直在Connecting to 192.168.10.1:18888。 我代理软件开启了 “Allow LAN” 选项,防火墙设置了专业网络和公共网络都可通信。实在不知道还能怎么办了

4631 次点击
所在节点    宽带症候群
29 条回复
justsomac
326 天前
系统太旧,不支持,升级系统
Goooooos
326 天前
确认下宿主机的 IP ,我的可以正常访问宿主机的 socks5 代理
storyxc
326 天前
可以参考下 /t/975098
tuoov
326 天前
@Goooooos 除了看 `/etc/resolv.conf`还有什么办法可以检查宿主机 IP 吗?
Goooooos
326 天前
@tuoov

ip route show
body007
326 天前
直接 ipconfig 找到 win 系统的 WSL 网卡 ip ,在 wsl 里面用这个 ip ,我这边可以成功。

yushiro
326 天前
wsl 里面装个 v2raya ,默认全局代理,不需要配置任何环境变量
chinaguaiu
326 天前
老实说我就是像你这么干的,就是每次启动 wsl 都要看/etc/resolv.conf 很折磨
chinaguaiu
326 天前
你不妨确认一下代理软件的端口有没有设置好,仔细看一下,可能端口被重置掉了
chensuixiang
326 天前
//注意写本机地址
export ALL_PROXY="http://本机 ip:7890"
//测试
curl https://www:google.com
//取消代理
export ALL_PROXY=

我之前是这样的,本机开的 clash 端口 7890 ,然后到 wsl2 上输入上面 export 命令。缺点是一次性的,每次重启 wsl2 如果要代理都得手动输入一遍。至于 Allow LAN 开不开,忘了,不过之前我确实可行的
uliah
326 天前
可以在 profile 或 bashrc 里面获取一下网关

GATEWAY=`ip route show default|awk '{print $3}'`

tee ~/.ssh/config <<EOF
Host opendev.org
User git
ProxyCommand nc -v -x ${GATEWAY}:1081 %h %p
EOF

tee ~/.gitconfig <<EOF
[credential]
helper = store
[gitreview]
username = hailu
[https]
proxy = http://${GATEWAY}:1081
[http]
proxy = http://${GATEWAY}:1081
EOF

export HTTP_PROXY=http://${GATEWAY}:1081
export HTTPS_PROXY=http://${GATEWAY}:1081
SuperMaskv
326 天前
```bash
#!/bin/sh
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
wslip=$(hostname -I | awk '{print $1}')
port=10800

PROXY_HTTP="http://${hostip}:${port}"

set_proxy(){
export http_proxy="${PROXY_HTTP}"
export HTTP_PROXY="${PROXY_HTTP}"

export https_proxy="${PROXY_HTTP}"
export HTTPS_proxy="${PROXY_HTTP}"

git config --global http.proxy "${PROXY_HTTP}"
git config --global https.proxy "${PROXY_HTTP}"
}

unset_proxy(){
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
git config --global --unset http.proxy
git config --global --unset https.proxy
}

test_setting(){
echo "Host ip:" ${hostip}
echo "WSL ip:" ${wslip}
echo "Current proxy:" $https_proxy
}

if [ "$1" = "set" ]
then
set_proxy

elif [ "$1" = "unset" ]
then
unset_proxy

elif [ "$1" = "test" ]
then
test_setting
else
echo "Unsupported arguments."
fi
```bash
这段脚本是我自己在用的,同时设置了 shell 的代理和 git 的代理。
启用代理就`. ./proxy.sh set` ,禁用就`. ./proxy unset`,查看当前状态就`. ./proxy test`
kaleido
326 天前
没人用 clash 的 tun 吗,啥也不用设置,打开 tun 模式就完了
mclxly
326 天前
我每次按需手动执行:

export http_proxy=socks5://[宿主机 IP]:2080 https_proxy=socks5://[宿主机 IP]:2080
cslive
326 天前
打开 tun 模式完事
alexsz
326 天前
供参考:
我使用 win11 + clash-verge + WSL2 ,系统代理模式
WSL2 的网络代理工作正常
我的.wslconfig 文件内容如下:
[experimental]
autoMemoryReclaim=gradual
networkingMode=mirrored
dnsTunneling=true
firewall=false
autoProxy=true
selca
326 天前
proxychains4
Goooooos
326 天前
@chinaguaiu 不需要,新版 wsl2 宿主机 ip 不会乱变

而且把 OP 的第二部的代码放到.bashrc 也行


export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*')
export https_proxy="http://${hostip}:7890"
export http_proxy="http://${hostip}:7890"
eaststarpen
326 天前
我有一个暴力做法

wsl 的环境变量是包含 windows host 的环境变量的; 就是说可以直接在 wsl bash 里运行 windows 命令

我之前的做法是 ipconfig.exe 获取宿主机 ip, 而且可以直接通过管道同时运行 Windows 和 wsl 指令, 如 "ipconfig.exe | grep -i ipv4"
heiher
326 天前
我之前在 WSL2 上实测过 tun2socks 方式可以: https://github.com/heiher/hev-socks5-tunnel

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1027320

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX