WSL 网络配置

WSL 网络模式、端口转发、代理配置和网络故障排查


📋 目录


网络架构

WSL 1 网络

  • 网络模式:与 Windows 共享网络堆栈
  • IP 地址:与 Windows 主机使用相同的 IP 地址
  • 端口访问:直接访问,无需端口转发
  • 性能:网络性能较好

WSL 2 网络

  • 网络模式:虚拟网络,使用 NAT(网络地址转换)
  • IP 地址:独立的 IP 地址(通常为 172.x.x.x)
  • 端口访问:需要配置端口转发才能从 Windows 访问
  • 性能:网络性能优秀,但需要额外配置

网络模式对比

特性WSL 1WSL 2
IP 地址与 Windows 相同独立 IP(172.x.x.x)
端口转发不需要需要
网络性能良好优秀
防火墙共享 Windows 防火墙独立防火墙规则

查看网络信息

查看 IP 地址

# 查看所有网络接口
ip addr show
# 或
ifconfig
 
# 查看主 IP 地址
hostname -I
 
# 查看 IPv4 地址
ip -4 addr show
 
# 查看特定接口
ip addr show eth0

查看网络接口

# 列出所有网络接口
ip link show
 
# 查看接口状态
ip link show eth0
 
# 启用/禁用接口
sudo ip link set eth0 up
sudo ip link set eth0 down

查看路由表

# 查看路由表
ip route show
# 或
route -n
 
# 查看默认网关
ip route | grep default

查看 DNS 配置

# 查看 DNS 服务器
cat /etc/resolv.conf
 
# 测试 DNS 解析
nslookup google.com
dig google.com

查看网络连接

# 查看监听端口
netstat -tuln
# 或
ss -tuln
 
# 查看活动连接
netstat -tun
ss -tun
 
# 查看特定端口
netstat -tuln | grep 3000
ss -tuln | grep 3000

查看 Windows 主机 IP

# 查看 Windows 主机 IP(从 WSL 2)
cat /etc/resolv.conf | grep nameserver | awk '{print $2}'
 
# 或使用脚本
ip route show | grep -i default | awk '{ print $3}'

端口转发

自动端口转发

WSL 2 支持自动端口转发(需要 Windows 11 或 Windows 10 版本 2004+)。

启用自动端口转发

.wslconfig 中配置:

[wsl2]
localhostForwarding=true

工作原理

  • WSL 2 自动检测监听端口
  • 自动在 Windows 主机上创建端口转发规则
  • 从 Windows 访问 localhost:port 会自动转发到 WSL 2

手动端口转发

方法一:使用 netsh(PowerShell)

# 获取 WSL 2 IP 地址
$wslIp = (wsl hostname -I).Trim()
 
# 转发端口 3000
netsh interface portproxy add v4tov4 `
    listenport=3000 `
    listenaddress=0.0.0.0 `
    connectport=3000 `
    connectaddress=$wslIp
 
# 查看所有端口转发规则
netsh interface portproxy show all
 
# 删除端口转发
netsh interface portproxy delete v4tov4 listenport=3000 listenaddress=0.0.0.0

方法二:使用脚本自动配置

创建 portproxy.ps1

# 获取 WSL 2 IP 地址
$wslIp = (wsl hostname -I).Trim()
Write-Host "WSL IP: $wslIp"
 
# 定义要转发的端口
$ports = @(3000, 8080, 5000, 3306, 5432)
 
foreach ($port in $ports) {
    # 删除现有规则(如果存在)
    netsh interface portproxy delete v4tov4 listenport=$port listenaddress=0.0.0.0 2>$null
    
    # 添加新规则
    netsh interface portproxy add v4tov4 `
        listenport=$port `
        listenaddress=0.0.0.0 `
        connectport=$port `
        connectaddress=$wslIp
    
    Write-Host "Forwarded port $port to $wslIp"
}
 
Write-Host "Port forwarding configured successfully"

方法三:使用 SSH 隧道

# 在 WSL 2 中设置 SSH 服务器
sudo apt install openssh-server
sudo service ssh start
 
# 在 Windows 中使用 SSH 端口转发
ssh -L 3000:localhost:3000 username@wsl_ip

动态端口转发脚本

创建 update-portproxy.ps1(在 Windows 启动时运行):

# 检查 WSL 是否运行
$wslStatus = wsl --list --verbose | Select-String "Running"
 
if ($wslStatus) {
    $wslIp = (wsl hostname -I).Trim()
    
    # 更新端口转发规则
    $ports = @(3000, 8080, 5000)
    
    foreach ($port in $ports) {
        netsh interface portproxy delete v4tov4 listenport=$port listenaddress=0.0.0.0 2>$null
        netsh interface portproxy add v4tov4 `
            listenport=$port `
            listenaddress=0.0.0.0 `
            connectport=$port `
            connectaddress=$wslIp
    }
}

从外部访问 WSL 服务

# 1. 配置端口转发(如上)
# 2. 配置 Windows 防火墙规则
New-NetFirewallRule -DisplayName "WSL Port 3000" `
    -Direction Inbound `
    -LocalPort 3000 `
    -Protocol TCP `
    -Action Allow

代理配置

在 WSL 中配置代理

方法一:环境变量

# 临时设置(当前会话)
export http_proxy="http://proxy.example.com:8080"
export https_proxy="http://proxy.example.com:8080"
export no_proxy="localhost,127.0.0.1"
 
# 永久设置(添加到 ~/.bashrc)
echo 'export http_proxy="http://proxy.example.com:8080"' >> ~/.bashrc
echo 'export https_proxy="http://proxy.example.com:8080"' >> ~/.bashrc
echo 'export no_proxy="localhost,127.0.0.1"' >> ~/.bashrc
source ~/.bashrc

方法二:apt 代理配置

# 编辑 apt 配置文件
sudo nano /etc/apt/apt.conf.d/95proxies
 
# 添加代理配置
Acquire::http::Proxy "http://proxy.example.com:8080";
Acquire::https::Proxy "http://proxy.example.com:8080";

方法三:使用 Windows 代理

# 获取 Windows 主机 IP
WIN_IP=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
 
# 设置代理为 Windows 主机(如果 Windows 上有代理)
export http_proxy="http://$WIN_IP:8080"
export https_proxy="http://$WIN_IP:8080"

Git 代理配置

# 全局配置
git config --global http.proxy http://proxy.example.com:8080
git config --global https.proxy http://proxy.example.com:8080
 
# 仅对特定域名配置
git config --global http.https://github.com.proxy http://proxy.example.com:8080
 
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

npm 代理配置

# 设置代理
npm config set proxy http://proxy.example.com:8080
npm config set https-proxy http://proxy.example.com:8080
 
# 取消代理
npm config delete proxy
npm config delete https-proxy

防火墙配置

Windows 防火墙

允许 WSL 访问

# 允许 WSL 网络访问
New-NetFirewallRule -DisplayName "WSL" `
    -Direction Inbound `
    -InterfaceAlias "vEthernet (WSL)" `
    -Action Allow

允许特定端口

# 允许端口 3000
New-NetFirewallRule -DisplayName "WSL Port 3000" `
    -Direction Inbound `
    -LocalPort 3000 `
    -Protocol TCP `
    -Action Allow

Linux 防火墙(iptables)

# 查看防火墙规则
sudo iptables -L
 
# 允许端口 3000
sudo iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
 
# 保存规则(Ubuntu/Debian)
sudo iptables-save | sudo tee /etc/iptables/rules.v4

网络故障排查

无法访问互联网

# 1. 检查网络连接
ping 8.8.8.8
 
# 2. 检查 DNS 解析
nslookup google.com
dig google.com
 
# 3. 检查 DNS 配置
cat /etc/resolv.conf
 
# 4. 检查路由
ip route show
 
# 5. 重启网络(如果可能)
sudo service networking restart

无法访问 Windows 主机

# 1. 获取 Windows 主机 IP
WIN_IP=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
 
# 2. 测试连接
ping $WIN_IP
 
# 3. 测试端口
telnet $WIN_IP 3389  # RDP

端口转发不工作

# 1. 检查端口转发规则
netsh interface portproxy show all
 
# 2. 检查 WSL IP 地址
wsl hostname -I
 
# 3. 检查防火墙规则
Get-NetFirewallRule | Where-Object DisplayName -like "*WSL*"
 
# 4. 测试端口
Test-NetConnection -ComputerName localhost -Port 3000

DNS 解析问题

# 1. 检查 /etc/resolv.conf
cat /etc/resolv.conf
 
# 2. 如果被自动覆盖,禁用自动生成
sudo nano /etc/wsl.conf
# 添加:
[network]
generateResolvConf = false
 
# 3. 手动配置 DNS
sudo nano /etc/resolv.conf
# 添加:
nameserver 8.8.8.8
nameserver 8.8.4.4

网络性能问题

# 1. 检查网络接口速度
ethtool eth0
 
# 2. 测试网络速度
# 安装 iperf3
sudo apt install iperf3
 
# 在服务器端
iperf3 -s
 
# 在客户端
iperf3 -c server_ip

高级网络配置

静态 IP 配置

WSL 2 默认使用 DHCP,但可以通过脚本设置静态 IP:

# 创建网络配置脚本
sudo nano /usr/local/bin/wsl-static-ip.sh
 
#!/bin/bash
# 获取 Windows 主机 IP
WIN_IP=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
# 设置静态 IP(需要根据实际情况调整)
sudo ip addr add 172.20.0.2/20 dev eth0
sudo ip route del default
sudo ip route add default via $WIN_IP

网络桥接

WSL 2 支持镜像网络模式(Windows 11):

# 在 .wslconfig 中
[wsl2]
networkingMode=mirrored

VPN 兼容性

某些 VPN 可能与 WSL 2 网络冲突:

# 如果 VPN 导致网络问题,尝试:
# 1. 重启 WSL
wsl --shutdown
wsl
 
# 2. 检查路由表
ip route show
 
# 3. 手动添加路由(如果需要)
sudo ip route add <network> via <gateway>

📚 参考资源


返回 WSL 知识体系

WSL 网络配置 端口转发 代理