WSL2全局代理配置
本文最后更新于33 天前,其中的信息可能已经过时,如有错误请发送邮件到feilong.shi@163.com

该配置仅供WSL2和宿主机网络环境不同时 使用 ,不提供任何非法的网络工具

适用环境:Windows 11 (23H2+) + WSL2 (Ubuntu/Debian)

核心原理:利用 WSL2 镜像网络模式,通过 Sing-box 的 Tunnel 虚拟设备接管全局流量,并转发给 Windows 宿主机的网络代理软件。

第一部分:Windows 宿主机准备

1. 配置 .wslconfig

此配置开启镜像网络,并关闭 WSL 自动代理和 DNS 隧道,将网络控制权完全交给 Sing-box。

  • 文件路径C:\Users\<你的用户名>\.wslconfig
  • 文件内容
[wsl2]
networkingMode=mirrored
autoProxy=false
dnsTunneling=false
dnsProxy=false
firewall=false
[experimental]

(修改后需在 PowerShell 执行 wsl --shutdown 重启 WSL)
替代方案

搜索WSL Settings,选项如下
![![[Pasted image 20251228020350.png]]](https://i-blog.csdnimg.cn/direct/18731b42e2f84abaadbbaaf2df571ebd.png)

2. Windows 防火墙放行

若不配置此项,WSL 流量会被 Windows 防火墙拦截。

  • 操作:控制面板 -> Windows Defender 防火墙 -> 允许应用通过防火墙 -> 找到你的代理软件 -> 同时勾选“专用”和“公用”

第二部分:WSL2 内部配置

1. 安装 Sing-box

使用官方源安装(会自动配置 systemd 服务,无需手动写 service 文件)。

sudo mkdir -p /etc/apt/keyrings
sudo curl -fsSL https://sing-box.app/gpg.key -o /etc/apt/keyrings/sagernet.asc
sudo chmod a+r /etc/apt/keyrings/sagernet.asc

cat <<'EOF' | sudo tee /etc/apt/sources.list.d/sagernet.sources
Types: deb
URIs: https://deb.sagernet.org/
Suites: *
Components: *
Enabled: yes
Signed-By: /etc/apt/keyrings/sagernet.asc
EOF

sudo apt-get update
sudo apt-get install sing-box

2. 编写配置文件 config.json

这是实现全局透明代理的核心。此配置使用 gvisor 栈提高兼容性,并强制劫持 DNS。

请更改outbounds中的127.0.0.1:7897为你的服务器

  • 文件路径/etc/sing-box/config.json
  • 文件内容
{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "dns": {
    "servers": [
      {
        "tag": "google-doh",
        "type": "tcp",
        "server": "8.8.8.8",
        "detour": "proxy"
      },
      {
        "tag": "local-dns",
        "type": "local",
        "detour": "direct"
      }
    ],
    "final": "google-doh",
    "strategy": "ipv4_only"
  },
  "inbounds": [
    {
      "type": "tun",
      "tag": "tun-in",
      "interface_name": "tun0",
      "address": [
        "172.19.0.1/30"
      ],
      "auto_route": true,
      "strict_route": true,
      "stack": "gvisor",
      "mtu": 9000,
      "sniff": true
    }
  ],
  "outbounds": [
    {
      "type": "http",
      "tag": "proxy",
      "server": "127.0.0.1",
      "server_port": 7897
    },
    {
      "type": "direct",
      "tag": "direct"
    }
  ],
  "route": {
    "rules": [
      {
        "protocol": "dns",
        "action": "hijack-dns"
      },
      {
        "ip_is_private": true,
        "outbound": "direct"
      },
      {
        "protocol": "quic",
        "outbound": "direct"
      }
    ],
    "auto_detect_interface": true,
    "default_domain_resolver": "google-doh",
    "final": "proxy"
  }
}

3. 启动服务

配置完成后,启动并设置开机自启。

sudo systemctl enable --now sing-box
sudo systemctl start sing-box
sudo systemctl status sing-box

第三部分:验证与排错

验证命令

# 1. 检查是否接管流量 (应返回代理 IP)
curl -I https://www.baidu.com

# 2. 检查 DNS 是否正常 (应迅速返回)
nslookup baidu.com

常见问题排查

http协议默认不支持代理UDP,socks5支不支持要看服务端,如果卡住或者 ip 和服务端不同那么就是不支持
可用以下命令排查

turnutils_stunclient -p 3478 stun.chat.bilibili.com

本文转自 https://blog.csdn.net/weixin_42707324/article/details/156344939,如有侵权,请联系删除。

如果觉得本文对您有所帮助,可以支持下博主,一分也是缘哦(*^_^*)
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇