Skip to content

WSL 相关

https://learn.microsoft.com/zh-cn/windows/wsl/

Terminal window
wsl -l -v
Terminal window
wsl --set-default <DistributionName>
Terminal window
wsl -d <DistributionName>
Terminal window
wsl --update --preview

https://learn.microsoft.com/zh-cn/windows/wsl/connect-usb

Terminal window
# 以管理员运行 powershell
PS C:\Users\Administrator> usbipd.exe list
Connected:
BUSID VID:PID DEVICE STATE
8-4 1a86:7523 USB-SERIAL CH340 (COM27) Not shared
Terminal window
PS C:\Users\Administrator> usbipd.exe bind -b 8-4
usbipd: info: Device with busid '8-4' was shared.
Terminal window
PS C:\Users\Administrator> usbipd.exe attach -w -b 8-4
usbipd: 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.
Terminal window
xxx@xxx:~$ lsusb
Bus 001 Device 005: ID 1a86:7523 QinHeng Electronics CH340 serial converter

此处以 minicom 为例

Terminal window
# 第一个共享的 USB 默认为 ttyUSB0
minicom -D /dev/ttyUSB0 -b 11520

USBIP_Connect

Terminal window
notepad.exe
Terminal window
explorer.exe

Gnome 文本编辑器是 GNOME 桌面环境的默认文本编辑器。

Terminal window
sudo apt install gnome-text-editor -y

若要在编辑器中启动 bashrc 文件,请输入:gnome-text-editor ~/.bashrc

Terminal window
notepad.exe

Nautilus 也称为 GNOME Files,是 GNOME 桌面的文件管理器。 (类似于 Windows 文件资源管理器)。

Terminal window
sudo apt install nautilus -y

若要启动,请输入:nautilus

Terminal window
explorer.exe
  1. 在 wsl 中使用记事本打开 ~/.bashrc 文件
Terminal window
notepad.exe ~/.bashrc
  1. 在文件中添加 code 的环境变量
Terminal window
# 例如 export PATH=$PATH:"/mnt/c/Users/Administrator/AppData/Local/Programs/Microsoft VS Code/bin"
export PATH=$PATH:"/mnt/${windowscode 的路径}"

要在 WSL 中设置默认启动用户为非 root 用户,可以按以下步骤操作:

  1. 打开 WSL 终端。
  2. 输入以下命令以编辑配置文件(假设你使用的是 Ubuntu):
Terminal window
sudo nano /etc/wsl.conf
  1. 添加以下内容:
[user]
default=你的用户名

确保将 “你的用户名” 替换为实际的非 root 用户名。

  1. 保存文件并退出(在 nano 中按 Ctrl + X,然后按 Y 确认保存)。
  2. 关闭 WSL,并在 PowerShell 中运行以下命令以重新加载 WSL:
Terminal window
wsl --shutdown
  1. 再次打开 WSL,应该会默认以指定的用户登录。
Terminal window
用于在适用于 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>
取消注册分发版并删除根文件系统。
Terminal window
# 导出时文件路径必须存在
wsl --export <发行版名称> <导出路径>\backup.tar
Terminal window
wsl --unregister <发行版名称>
Terminal window
# --version 通常使用 2 即 wsl2
wsl --import <新发行版名称> <新路径> <导出路径>\backup.tar --version <版本号>
Terminal window
C:\Users\Administrator>usbipd attach -w -b 8-2
usbipd: 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 connect
usbipd: error: Failed to attach device with busid '8-2'.
Terminal window
usbipd status

如果未安装

Terminal window
sudo apt update
sudo apt install linux-tools-common

要检查防火墙设置,可以按照以下步骤操作:

  1. 打开“控制面板”,然后选择“系统和安全”。
  2. 点击“Windows 防火墙”。
  3. 在左侧菜单中,点击“高级设置”。
  4. 在“入站规则”中查找是否有与 usbipd 或端口 3240 相关的规则。如果没有,你可以手动添加规则。
  1. 在“高级设置”中,点击“入站规则”。
  2. 选择右侧的“新建规则”。
  3. 选择“端口”并点击“下一步”。
  4. 选择“TCP”,然后输入“3240”端口号。
  5. 选择“允许连接”,然后完成向导。
  1. 打开“控制面板”,选择“系统和安全”。
  2. 点击“Windows 防火墙”。
  3. 在左侧菜单中,点击“启用或关闭 Windows 防火墙”。
  4. 选择“关闭 Windows 防火墙(不推荐)”。