
在今天的教程中,我们将教你如何在Windows Subsystem for Linux 2(WSL2)中使用USB设备。我们将首先安装必要的工具,然后配置udev以允许非root用户访问设备,最后使用USB/IP来连接设备。
先决条件
- WSL2内核版本>=5.10.60.1
可以运行
uname -a
命令确认你的WSL版本。
第一步:安装usbipd-win
usbipd-win 是一个在Windows上共享USB设备给其他机器,包括Hyper-V和WSL 2的软件。
安装usbipd-win需要Microsoft Windows 8.1 x64 / Microsoft Windows Server 2012 或更新版本的Windows系统。
你可以从这里下载最新的.msi安装程序,然后在你的USB设备连接的Windows机器上运行它。或者,你也可以使用Windows包管理器安装:
winget install usbipd
这将会安装:
- 一个名为
usbipd
的服务(显示名称:USBIP设备主机)。 你可以使用Windows的服务应用检查这个服务的状态。 - 一个命令行工具
usbipd
。 这个工具的位置将会被添加到PATH
环境变量中。 - 一个名为
usbipd
的防火墙规则,允许所有本地子网连接到这个服务。 你可以修改这个防火墙规则来调整访问控制。
第二步:安装USB/IP客户端工具
我们首先在WSL内安装USB/IP的用户空间工具和USB硬件标识符的数据库。在Ubuntu 20.04 LTS上,运行以下命令:
sudo apt install linux-tools-virtual hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip `ls /usr/lib/linux-tools/*/usbip | tail -n1` 20
⚠️ 这些指令可能随着软件的更新而改变。请确保始终使用最新的指令。
第三步:配置udev
你可能需要配置udev规则以允许非root用户访问设备。请在连接设备之前完成此步骤。
以使用openocd和嵌入式设备为例,你需要将share/openocd/contrib/60-openocd.rules文件复制到/etc/udev/rules.d文件夹。
第四步:使用WSL快捷命令
确保你的WSL命令提示符是开启的。这可以保持WSL 2的轻量级虚拟机活动。然后,你可以使用usbipd的WSL便利命令来连接设备。
首先,运行 usbipd wsl list
列出所有连接到Windows的USB设备。
然后,选择你想要连接的设备,并运行 usbipd wsl attach --busid
命令。(需要管理员权限)
在WSL内部,你可以运行 lsusb
来查看已连接的USB设备。
最后,你可以使用 usbipd wsl detach --busid
命令来停止设备共享。
就是这样!你现在应该能够在WSL2中使用你的USB设备了。
本文链接: https://linuxstory.org/connecting-usb-devices-to-wsl2-a-step-by-step-tutorial
LinuxStory 原创教程,转载请注明出处,否则必究相关责任。
Hi,
I wanted to see if you’d be interested in a link exchange for mutual SEO benefits. I can link to your site (linuxstory.org) from a few of our high-authority websites. In return, you would link back to our clients’ sites, which cover niches like health, business services, real estate, consumer electronics, and more.
If you’re interested, let me know — I’d be happy to share more details!
Thanks for your time,
Karen
SEO Account Manager
Hi there
I am looking to help a couple of local businesses increase their monthly revenue by atleast 20% by optimizing the site for more traffic & enquiries.
The best part? You don’t have to spend anything on ads or even pay me.
I am doing this to provide upfront value to local businesses in hopes they will become paying clients one day.
If you wanna apply for this offer, reply to this email!