前言
本文旨在为需要在 Windows 上使用 WSL(Windows Subsystem for Linux)进行开发的用户提供一份从安装到网络配置、再到开发环境搭建的完整指南。无论你是刚开始接触 WSL,还是已经使用过但遇到了网络连接、远程开发或软件源配置等问题,本文都将提供清晰的步骤和解决方案。
目标读者:
- 需要在 Windows 上搭建 Linux 开发环境的开发者。
- 希望 WSL 能够与主机处于同一局域网,方便进行网络调试、远程连接的用户。
- 遇到 WSL 网络桥接、DNS 解析、git clone 慢等常见问题的用户。
主要内容概览:
- WSL 安装与基础配置:包括 Windows 终端安装、WSL 初始化、root 用户设置等。
- 网络桥接模式设置:详细讲解如何将 WSL 网络从默认的 NAT 模式改为桥接模式,使其获得与主机同网段的独立 IP,并解决桥接后可能遇到的网络互通、主机断网、DNS 解析等问题。
- 开发环境配置:
- SFTP 连接:在 WSL 中安装并配置 OpenSSH 服务,实现通过 SFTP 进行文件传输。
- VS2022 远程调试:配置 WSL 作为 VS2022 的远程开发环境,支持 CMake 项目的编译与调试。
- 更新软件源:将 Ubuntu 的软件源更换为国内镜像(如阿里云),加速软件包下载。
- 解决 git clone 网络问题:通过修改 DNS 或使用镜像代理,解决 GitHub 克隆缓慢或失败的问题。
- 高级网络配置:包括为 WSL 设置固定 IP,确保其 IP 地址在重启后保持不变,方便服务部署与访问。
通过本文,你将能够搭建一个稳定、高效且与本地网络无缝集成的 WSL 开发环境。
Windows终端安装
wsl--installwsl.exe--list--onlinewsl.exe--installUbuntu-22.04 wsl--unregisterUbuntu设置root用户密码
sudopasswdroot查看wsl版本
wsl-l-v或者wsl--list--verbose网络设置桥接模式
需要启用Hyper-V
控制面板 - 程序 - 启用或关闭Windows功能 - 启用Hyper-V
重启电脑
Get-Module-ListAvailableHyper-V#输出Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules ModuleType Version PreRelease Name PSEdition ExportedCommands ---------- ------- ---------- ---- --------- ---------------- Manifest2.0.0.0 Hyper-V Core,Desk{Add-VMAssignableDevice, Add-VMDvdDrive…Windows家庭版没有Hyper-V功能,需要单独下载安装
将下面的代码复制保存为 .cmd 文件,使用管理员身份运行。
脚本执行完成后,重启电脑,然后再在控制面板里启用Hyper-V
pushd"%~dp0"dir/b%SystemRoot%\servicing\Packages\*Hyper-V*.mum>hyper-v.txtfor/f%%iin('findstr /i . hyper-v.txt 2^>nul')dodism/online/norestart/add-package:"%SystemRoot%\servicing\Packages\%%i"del hyper-v.txt Dism/online/enable-feature/featurename:Microsoft-Hyper-V-All/LimitAccess/ALL1 查看物理网卡name
Get-NetAdapter#输出Name InterfaceDescription ---- -------------------- 以太网 Realtek PCIe GbE Family Controller WLAN Intel(R)Wi-Fi6AX2012 创建外部虚拟交换机
New-VMSwitch`-Name"WSLBridge"`-NetAdapterName"以太网"`-AllowManagementOS$trueNew-VMSwitch`-Name"WSLBridge"`-NetAdapterName"WLAN"`-AllowManagementOS$trueGet-VMSwitch#输出Name SwitchType ---- ---------- WSLBridge External3 修改 .wslconfig
notepad$env:USERPROFILE\.wslconfig#修改[wsl2]networkingMode=bridgedvmSwitch=WSLBridgedhcp=false4 重启WSL
wsl--shutdown#查看iphostname-Iifconfig5 网络互通问题
主机能ping通WSL,但WSL内部可能ping不通主机
大概率是防火墙问题,可添加规则或将虚拟交换机设为内部网络
Get-NetConnectionProfile#输出包含该项NetworkCategory:Public#修改为可信任内部网络,然后就可以ping通了Set-NetConnectionProfile`-InterfaceAlias"vEthernet (WSLBridge)"`-NetworkCategoryPrivate6 主机上不了网
#局域网链路检测ping192.168.120.1#外网路由检测ping8.8.8.8# DNS检测nslookupwww.microsoft.com上述测试均正常,说明网络正常。
但是如果在配置过程中开启了网络代理(VPN),也可能导致主机上不了网。
此时关闭网络代理,再查看是否能上网。
确认能上网,再打开网络代理,一切正常。
7 删除桥接交换机
wsl--shutdownRemove-VMSwitch-Name"WSLBridge"-Force8 WSL DNS解析问题
#局域网ping-c3192.168.120.1#WSL 外网路由ping-c3223.5.5.5# DNSping-c3openapi.alipay.comopenapi.alipay.com 失败:确认只有 DNS 故障。
#查看当前 DNScat/etc/resolv.conf# 临时修复sudotee/etc/resolv.conf>/dev/null<<'EOF' nameserver 223.5.5.5 nameserver 119.29.29.29 nameserver 1.1.1.1 EOF# 测试cat/etc/resolv.confnslookupopenapi.alipay.com getent hosts openapi.alipay.comping-c3openapi.alipay.com# 永久有效sudovi/etc/wsl.conf# 写入[network]generateResolvConf=false# 重新创建 DNS 文件sudorm-f/etc/resolv.confsudotee/etc/resolv.conf>/dev/null<<'EOF' nameserver 192.168.120.1 nameserver 223.5.5.5 nameserver 119.29.29.29 EOF# 防止 dhcpcd 再次覆盖sudochattr +i /etc/resolv.conf# 重启WSLwsl--shutdown设置不可变后,将来需要修改 DNS 时先执行:
sudo chattr -i /etc/resolv.conf
9 设置固定IP
# 查看地址是否已被分配arp-a|findstr192.168.120.196# 创建添加固定 IP 脚本sudovi/usr/local/bin/fix-wsl-network.sh#写入#!/bin/bashDEV="eth0"IP="192.168.120.196"GW="192.168.120.1"iplinkset$DEVup# 清除所有旧地址ipaddr flush dev$DEV# 添加固定地址ipaddradd$IP/24 dev$DEV# 添加路由iproute replace default via$GWdev$DEV# 给权限sudochmod+x /usr/local/bin/fix-wsl-network.sh# 检查 systemd 服务状态systemctl status创建 systemd 服务sudovi/etc/systemd/system/fix-wsl-network.service# 写入[Unit]Description=Fix WSL Static IPAfter=network.target[Service]Type=oneshotExecStart=/usr/local/bin/fix-wsl-network.shRemainAfterExit=yes[Install]WantedBy=multi-user.target# 重新加载服务sudosystemctl daemon-reload# 设置开机启动sudosystemctlenablefix-wsl-network.service# 立即启动sudosystemctl start fix-wsl-network.service# 查看服务状态systemctl status fix-wsl-network.service# 检查检查ip地址ipaddr show eth0# 显示无期限 固定ip2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdisc mq state UP group default qlen1000link/ether 5e:bb:f6:9e:ee:fa brd ff:ff:ff:ff:ff:ff inet192.168.120.196/24 scope global eth0 valid_lft forever preferred_lft foreverSFTP连接wsl
安装OpenSSH
sudoaptupdatesudoaptinstallopenssh-server查看版本
ssh-V配置SSH
sudovim/etc/ssh/ssh_config配置文件打开端口
Port22启动ssh服务
sudoservicesshstartVS2022远程调试
安装构建工具
sudoapt-getinstallg++ gdb cmakegitopenssl libssl-dev ninja-build设置VS2022——CMake——永不自动运行配置步骤
CMake设置——添加新的配置——选择WSL-GCC-Debug
更新源
Ubuntu 系统中关于软件源配置方式变化:
从传统的 sources.list 格式迁移到 deb822 格式
/etc/apt/sources.list —> /etc/apt/sources.list.d/ubuntu.sources
# 备份 sources.list.d 目录下的所有源文件sudocp-r/etc/apt/sources.list.d/ /etc/apt/sources.list.d.bak/# 若存在传统的 sources.list 文件,也一并备份sudocp/etc/apt/sources.list /etc/apt/sources.list.baksudorm-f/etc/apt/sources.list.d/ubuntu.sources# 删除默认 Ubuntu 源sudorm-f/etc/apt/sources.list# 若存在传统文件,也删除sudonano/etc/apt/sources.list.d/aliyun.sources按 Ctrl+O 保存,Ctrl+X 退出编辑器。
Types:debURIs:http://mirrors.aliyun.com/ubuntu/Suites:noble noble-updates noble-backports noble-securityComponents:main restricted universe multiverseArchitectures:amd64Signed-By:/usr/share/keyrings/ubuntu-archive-keyring.gpgTypes:deb-srcURIs:http://mirrors.aliyun.com/ubuntu/Suites:noble noble-updates noble-backports noble-securityComponents:main restricted universe multiverseArchitectures:amd64Signed-By:/usr/share/keyrings/ubuntu-archive-keyring.gpgUbuntu 24.04 → noble,22.04 → jammy,20.04 → focal,18.04 → bionic。
可通过 lsb_release -c 命令查看当前系统的代号。
sudoaptupdategit clone网络问题
sudonano/etc/resolv.confnameserver8.8.8.8# Google DNSnameserver1.1.1.1# Cloudflare DNSnameserver8.8.4.4# 备用 Google DNS如果主机设置了DNS,需要与主机设置的DNS地址一致
# 镜像源下载gitclone https://gh.llkk.cc/https://github.com/unitreerobotics/unitree_sdk2.gitgitclone https://gh-proxy.com/https://github.com/unitreerobotics/unitree_sdk2.git