前言本文旨在为需要在 Windows 上使用 WSLWindows 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-Vpushd%~dp0dir/b%SystemRoot%\servicing\Packages\*Hyper-V*.mumhyper-v.txtfor/f%%iin(findstr /i . hyper-v.txt 2^nul)dodism/online/norestart/add-package:%SystemRoot%\servicing\Packages\%%idel hyper-v.txt Dism/online/enable-feature/featurename:Microsoft-Hyper-V-All/LimitAccess/ALL1 查看物理网卡nameGet-NetAdapter#输出Name InterfaceDescription ---- -------------------- 以太网 Realtek PCIe GbE Family Controller WLAN Intel(R)Wi-Fi6AX2012 创建外部虚拟交换机New-VMSwitch-NameWSLBridge-NetAdapterName以太网-AllowManagementOS$trueNew-VMSwitch-NameWSLBridge-NetAdapterNameWLAN-AllowManagementOS$trueGet-VMSwitch#输出Name SwitchType ---- ---------- WSLBridge External3 修改 .wslconfignotepad$env:USERPROFILE\.wslconfig#修改[wsl2]networkingModebridgedvmSwitchWSLBridgedhcpfalse4 重启WSLwsl--shutdown#查看iphostname-Iifconfig5 网络互通问题主机能ping通WSL但WSL内部可能ping不通主机大概率是防火墙问题可添加规则或将虚拟交换机设为内部网络Get-NetConnectionProfile#输出包含该项NetworkCategory:Public#修改为可信任内部网络然后就可以ping通了Set-NetConnectionProfile-InterfaceAliasvEthernet (WSLBridge)-NetworkCategoryPrivate6 主机上不了网#局域网链路检测ping192.168.120.1#外网路由检测ping8.8.8.8# DNS检测nslookupwww.microsoft.com上述测试均正常说明网络正常。但是如果在配置过程中开启了网络代理VPN也可能导致主机上不了网。此时关闭网络代理再查看是否能上网。确认能上网再打开网络代理一切正常。7 删除桥接交换机wsl--shutdownRemove-VMSwitch-NameWSLBridge-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/nullEOF 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]generateResolvConffalse# 重新创建 DNS 文件sudorm-f/etc/resolv.confsudotee/etc/resolv.conf/dev/nullEOF 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.conf9 设置固定IP# 查看地址是否已被分配arp-a|findstr192.168.120.196# 创建添加固定 IP 脚本sudovi/usr/local/bin/fix-wsl-network.sh#写入#!/bin/bashDEVeth0IP192.168.120.196GW192.168.120.1iplinkset$DEVup# 清除所有旧地址ipaddr flush dev$DEV# 添加固定地址ipaddradd$IP/24 dev$DEV# 添加路由iproute replace default via$GWdev$DEV# 给权限sudochmodx /usr/local/bin/fix-wsl-network.sh# 检查 systemd 服务状态systemctl status创建 systemd 服务sudovi/etc/systemd/system/fix-wsl-network.service# 写入[Unit]DescriptionFix WSL Static IPAfternetwork.target[Service]TypeoneshotExecStart/usr/local/bin/fix-wsl-network.shRemainAfterExityes[Install]WantedBymulti-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_UPmtu1500qdisc 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安装OpenSSHsudoaptupdatesudoaptinstallopenssh-server查看版本ssh-V配置SSHsudovim/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按 CtrlO 保存CtrlX 退出编辑器。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 → noble22.04 → jammy20.04 → focal18.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