作者: DWDROME
维护时间: 2025-03-22
参考文章:Windows子系统(WSL)通过桥接网络实现被外部局域网主机直接访问
WSL 环境桥接与雷达通信配置笔记
环境说明
- Windows 11 专业版(启用 Hyper-V)
- WSL2 Ubuntu 20.04
- 物理网线(连接 Livox 雷达)
- WSL 使用桥接方式访问物理网段,设置静态 IP 与雷达同网段
实现方法
思路就是将 WSL2 自建的虚拟 NAT 网络桥接到 Windows 主机网卡(以太网 2)上,再通过脚本为 WSL 配置静态 IP,从而实现与雷达设备通信。
1、开启 Hyper-V
桥接功能依赖 Hyper-V 组件,仅在 Windows 10/11 专业版中提供。开启方法如下:
- 打开控制面板 → 程序 → 启用或关闭 Windows 功能;
- 勾选 Hyper-V 相关项;
- 重启电脑生效。
2、桥接网络
WSL2 默认采用 NAT 网络,不利于局域网通信,需将其桥接到物理网卡(如以太网 2)。
操作步骤:
- 启动一次
wsl
,确保 Hyper-V 为其创建虚拟交换机; - 管理员身份打开 PowerShell,执行
Get-NetAdapter
查出网卡名称; - 使用以下命令桥接网卡:
Set-VMSwitch "WSL (Hyper-V firewall)" -NetAdapterName "以太网 2"
3、手动修改 WSL 网络
进入 WSL,配置 IP 与路由,保持与雷达在同一网段:
ip addr del ...
ip addr add ...
ip route add ...
nano /etc/resolv.conf
若步骤繁琐,可参考下方自动脚本配置。
4、一键桥接脚本说明
为了简化配置,建议编脚本:
(1)WSL 网络配置脚本 set_eth0.sh
作用:在 WSL 内配置 eth0 的 IP、路由和 DNS。
内容如下(请自行填写):
#!/bin/bash
# 设置 eth0 的静态 IP 以连接 Livox 雷达new_ip=192.168.1.50
brd=192.168.1.255
gateway=192.168.1.1
nameserver=192.168.1.1
net_dev=eth0# 获取当前 IP
current_ip=$(ip addr show $net_dev | grep 'inet\b' | awk '{print $2}' | head -n 1)# 删除旧 IP(如果存在)
if [ -n "$current_ip" ]; thenecho "检测到当前 IP 为 $current_ip,正在删除..."echo "YourPassword" | sudo -S ip addr del "$current_ip" dev $net_dev
elseecho "未检测到已有 IP,跳过删除。"
fi# 添加新 IP 和网关
sudo ip addr add $new_ip/24 broadcast $brd dev $net_dev
sudo ip route add 0.0.0.0/0 via $gateway dev $net_dev# 设置 DNS
sudo sed -i "\$c nameserver $nameserver" /etc/resolv.conf
(2)PowerShell 桥接脚本 wsl-bridge-on.ps1
作用:以管理员权限运行,桥接虚拟交换机,并调用 WSL 中的配置脚本。
内容如下(请自行填写):
# wsl-bridge-on.ps1
# 自动桥接 WSL 到物理网卡(以太网 2)并配置 WSL 静态 IP
$currentWi = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentWp = [Security.Principal.WindowsPrincipal]$currentWi
if (-not $currentWp.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {$boundPara = ($MyInvocation.BoundParameters.Keys | foreach { '-{0} {1}' -f $_, $MyInvocation.BoundParameters[$_] }) -join ' '$currentFile = $MyInvocation.MyCommand.Definition$fullPara = $boundPara + ' ' + $args -join ' 'Start-Process "$psHome\pwsh.exe" -ArgumentList "$currentFile $fullPara" -verb runasreturn
}Write-Host "`n正在检测并准备 WSL 网络..."
wsl --cd ~ -e ls$switchName = "WSL (Hyper-V firewall)"
$netAdapter = "以太网 2"if ((Get-VMSwitch).Name -contains $switchName) {Write-Host "`n检测到交换机已存在,跳过创建。"
} else {Write-Host "`n未检测到交换机,正在创建..."New-VMSwitch -Name $switchName -NetAdapterName $netAdapter -AllowManagementOS $true
}Write-Host "`n执行桥接配置..."
Set-VMSwitch $switchName -NetAdapterName $netAdapterWrite-Host "`n正在配置 WSL 静态 IP..."
wsl bash -c "/home/dw/set_eth0.sh"Write-Host "`ndone"
pause
(3)PowerShell 桥接脚本 wsl-bridge-off.ps1
取消桥接脚本,同时重启wsl
# 检查并以管理员身份运行 PS 并带上参数
$currentWi = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentWp = [Security.Principal.WindowsPrincipal]$currentWi
if (-not $currentWp.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {$boundPara = ($MyInvocation.BoundParameters.Keys | foreach { '-{0} {1}' -f $_, $MyInvocation.BoundParameters[$_] }) -join ' '$currentFile = $MyInvocation.MyCommand.Definition$fullPara = $boundPara + ' ' + $args -join ' 'Start-Process "$psHome\pwsh.exe" -ArgumentList "$currentFile $fullPara" -verb runasreturn
}echo "正在解除 WSL 桥接..."
Set-VMSwitch "WSL (Hyper-V firewall)" -SwitchType Internalecho "正在重启 WSL..."
wsl --shutdown
wsl --cd ~ -e lsecho "`ndone"
pause
6、启用 PowerShell 脚本执行权限
默认 Windows 禁止执行 .ps1
脚本,需手动启用:
管理员 PowerShell 中执行:
Set-ExecutionPolicy RemoteSigned
输入 Y
确认。
7、使用方法
- 执行桥接:双击
wsl-bridge-on.ps1
,等待执行完成; - 执行取消桥接:双击
wsl-bridge-off.ps1
。
如需修改打开方式:右键
.ps1
→ 选择pwsh.exe
打开,并设为默认。
8、补充说明
- 使用过程中若出现多个
vEthernet
接口,建议删除无效网卡并重建交换机;