WSL 相关
https://learn.microsoft.com/zh-cn/windows/wsl/
多子系统管理
Section titled “多子系统管理”查看已安装的 WSL 分发版
Section titled “查看已安装的 WSL 分发版”wsl -l -v设置默认子系统
Section titled “设置默认子系统”wsl --set-default <DistributionName>启动指定的 wsl 子系统发行版
Section titled “启动指定的 wsl 子系统发行版”wsl -d <DistributionName>更新到预览版
Section titled “更新到预览版”wsl --update --preview共享 USB 设备
Section titled “共享 USB 设备”安装 usbipd
Section titled “安装 usbipd”https://learn.microsoft.com/zh-cn/windows/wsl/connect-usb
Windows 端 usbipd 的使用
Section titled “Windows 端 usbipd 的使用”查看可共享的 USB 设备
Section titled “查看可共享的 USB 设备”# 以管理员运行 powershellPS C:\Users\Administrator> usbipd.exe listConnected:BUSID VID:PID DEVICE STATE8-4 1a86:7523 USB-SERIAL CH340 (COM27) Not shared绑定 USB 设备
Section titled “绑定 USB 设备”PS C:\Users\Administrator> usbipd.exe bind -b 8-4usbipd: info: Device with busid '8-4' was shared.激活 USB 设备
Section titled “激活 USB 设备”PS C:\Users\Administrator> usbipd.exe attach -w -b 8-4usbipd: info: Using WSL distribution 'Ubuntu-24.04' to attach; the device will be available in all WSL 2 distributions.usbipd: info: Using IP address xxx.xx.xx.x to reach the host.查看 USB 设备
Section titled “查看 USB 设备”xxx@xxx:~$ lsusbBus 001 Device 005: ID 1a86:7523 QinHeng Electronics CH340 serial converter使用 USB 设备
Section titled “使用 USB 设备”此处以 minicom 为例
# 第一个共享的 USB 默认为 ttyUSB0minicom -D /dev/ttyUSB0 -b 11520USBIP Connect 插件
Section titled “USBIP Connect 插件”
高级文件管理
Section titled “高级文件管理”记事本打开文件
Section titled “记事本打开文件”notepad.exe资源管理器打开文件夹
Section titled “资源管理器打开文件夹”explorer.exeLinux GUI 应用
Section titled “Linux GUI 应用”Gnome 文本编辑器
Section titled “Gnome 文本编辑器”Gnome 文本编辑器是 GNOME 桌面环境的默认文本编辑器。
sudo apt install gnome-text-editor -y若要在编辑器中启动 bashrc 文件,请输入:gnome-text-editor ~/.bashrc
跨文件系统命令替代
Section titled “跨文件系统命令替代”notepad.exeNautilus 文件管理器
Section titled “Nautilus 文件管理器”Nautilus 也称为 GNOME Files,是 GNOME 桌面的文件管理器。 (类似于 Windows 文件资源管理器)。
sudo apt install nautilus -y若要启动,请输入:nautilus
跨文件系统命令替代
Section titled “跨文件系统命令替代”explorer.execode 命令失效
Section titled “code 命令失效”- 在 wsl 中使用记事本打开
~/.bashrc文件
notepad.exe ~/.bashrc- 在文件中添加 code 的环境变量
# 例如 export PATH=$PATH:"/mnt/c/Users/Administrator/AppData/Local/Programs/Microsoft VS Code/bin"export PATH=$PATH:"/mnt/${windows 中 code 的路径}"设置默认启动用户
Section titled “设置默认启动用户”要在 WSL 中设置默认启动用户为非 root 用户,可以按以下步骤操作:
- 打开 WSL 终端。
- 输入以下命令以编辑配置文件(假设你使用的是 Ubuntu):
sudo nano /etc/wsl.conf- 添加以下内容:
[user]default=你的用户名确保将 “你的用户名” 替换为实际的非 root 用户名。
- 保存文件并退出(在 nano 中按
Ctrl + X,然后按Y确认保存)。 - 关闭 WSL,并在 PowerShell 中运行以下命令以重新加载 WSL:
wsl --shutdown- 再次打开 WSL,应该会默认以指定的用户登录。
WSL 指定盘使用
Section titled “WSL 指定盘使用”关键命令帮助
Section titled “关键命令帮助”用于在适用于 Linux 的 Windows 子系统中管理分发版的参数:
--export <Distro> <FileName> [选项] 将分发版导出到 tar 文件。 文件名可以是 - for stdout。
选项: --vhd 指定应将分发版导出为 .vhdx 文件。
--import <Distro> <InstallLocation> <FileName> [选项] 将指定的 tar 文件作为新分发版导入。 文件名可以是 - for stdin。
选项: --version <Version> 指定要用于新分发的版本。
--vhd 指定所提供的文件是 .vhdx 文件,而不是 tar 文件。 此操作在指定的安装位置创建 .vhdx 文件的副本。
--import-in-place <Distro> <FileName> 将指定的 .vhdx 文件作为新分发版导入。 必须使用 ext4 文件系统类型设置此虚拟硬盘的格式。
--list, -l [选项] 列出分发版。
选项: --all 列出所有分发版,包括当前 正在安装或卸载的分发版。
--running 仅列出当前正在运行的分发版。
--quiet, -q 仅显示分发版名称。
--verbose, -v 显示有关所有分发版的详细信息。
--online, -o 显示适合通过 'wsl --install' 安装的可用分发版列表。
--set-default, -s <Distro> 将分布版设置为默认值。
--set-version <Distro> <Version> 更改指定分发版的版本。
--terminate, -t <Distro> 终止指定的分发版。
--unregister <Distro> 取消注册分发版并删除根文件系统。WSL 盘符迁移
Section titled “WSL 盘符迁移”导出现有的 WSL 发行版
Section titled “导出现有的 WSL 发行版”# 导出时文件路径必须存在wsl --export <发行版名称> <导出路径>\backup.tar卸载现有的 WSL 发行版
Section titled “卸载现有的 WSL 发行版”wsl --unregister <发行版名称>导入到新的位置
Section titled “导入到新的位置”# --version 通常使用 2 即 wsl2wsl --import <新发行版名称> <新路径> <导出路径>\backup.tar --version <版本号>Failed to attach
Section titled “Failed to attach”C:\Users\Administrator>usbipd attach -w -b 8-2usbipd: info: Using WSL distribution 'Ubuntu' to attach; the device will be available in all WSL 2 distributions.usbipd: info: Using IP address 192.168.1.4 to reach the host.usbipd: warning: A firewall appears to be blocking the connection; ensure TCP port 3240 is allowed.WSL usbip: error: tcp connectusbipd: error: Failed to attach device with busid '8-2'.检查 usbipd 是否被安装
Section titled “检查 usbipd 是否被安装”usbipd status如果未安装
sudo apt updatesudo apt install linux-tools-common要检查防火墙设置,可以按照以下步骤操作:
在 Windows 上检查防火墙设置:
Section titled “在 Windows 上检查防火墙设置:”- 打开“控制面板”,然后选择“系统和安全”。
- 点击“Windows 防火墙”。
- 在左侧菜单中,点击“高级设置”。
- 在“入站规则”中查找是否有与
usbipd或端口 3240 相关的规则。如果没有,你可以手动添加规则。
添加防火墙规则允许端口 3240:
Section titled “添加防火墙规则允许端口 3240:”- 在“高级设置”中,点击“入站规则”。
- 选择右侧的“新建规则”。
- 选择“端口”并点击“下一步”。
- 选择“TCP”,然后输入“3240”端口号。
- 选择“允许连接”,然后完成向导。
临时禁用防火墙:
Section titled “临时禁用防火墙:”- 打开“控制面板”,选择“系统和安全”。
- 点击“Windows 防火墙”。
- 在左侧菜单中,点击“启用或关闭 Windows 防火墙”。
- 选择“关闭 Windows 防火墙(不推荐)”。