Development

How to Connect USB Device to WSL Instance

Captain Salem 4 min read

How to Connect USB Device to WSL Instance

Windows Subsystem for Linux or WSL for has been a revolutionary feature for us geeks. It allows us to run a Linux instance inside the Windows OS. Yes!! No more dual booting.

Although WSL has been a great feature, it is still far from perfect nor is going to replace a native Linux instance. However, we can appreciate the features and functionality it provides.

In some cases, you may need to connect a physical device to your WSL instance. Whether you are building embedded system or just transfer some files, having the ability to connect USB device to WSL is very helpful.

In this tutorial, you will learn how you can connect your USB devices to a WSL instance.

Requirements

Before enabling the USB connectivity feature on your system, ensure you have the following requirements met.

  1. Windows 11 and above.
  2. WSL Kernel version 5.10.60.1 and later.
  3. A WSL 2 distribution running on your system.
  4. Administrator privileges.

Once you have the above requirements met, we can proceed.

Install the usbipd-win package - setup.

The first step is installing the usbipd-win package. This is a free and open-source package that allows you to share your locally connected USB devices with virtualized machines such as Hyper-V and WSL instances.

Open your browser and navigate to the resource below:

https://github.com/dorssel/usbipd-win/releases

Select the latest release of the package and download.

image-20220714114923819
image-20220714114923819

Once downloaded, launch the installer to begin the installation process.

image-20220714115005582
image-20220714115005582

Follow along with the setup wizard and complete the installation. This may require you to restart your system or WSL session.

Install the usbipd-win package - winget.

If you have the Windows package manager installed, learn more about that in the link below:

https://www.geekbits.io/how-to-install-windows-package-manager/

You can install the usbipd-win package with a simple command. Open your command prompt as admin and run the command:

winget install usbipd

The command above will install

  • A service called usbipd (display name: USBIP Device Host).
  • A command line tool usbipd in your system path
  • A firewall rule called usbipd to allow all local subnets to connect to the service.

You can verify you have the tool installed by running the command:

usbipd --version

The command should return the installed version information as shown:

2.3.0+42.Branch.master.Sha.3d9f5c5acc4e133ab8147684ad1463cbaec43240

Install USP/IP User Space Tools in WSL

The next step is to install the user space tools for USB/IP, and the database for the USB hardware identifiers and the usb utilities.

USB/IP Tools on Ubuntu

Launch your Ubuntu WSL instance and run the command below to install USB/IP tools:

$ sudo apt install linux-tools-5.4.0-77-generic linux-tools-virtual hwdata usbutils
$ sudo update-alternatives --install /usr/local/bin/usbip usbip `ls /usr/lib/linux-tools/*/usbip | tail -n1` 20

USB/IP Tools on Debian

On Debian instances, run the commands below to install usbip and hwdata tools.

sudo apt-get install usbip hwdata usbutils

USB/IP Tools on CentOS/REHL

If you are on CentOS, Fedora on any other REHL family, run the command below to install the usbip client tools.

$ sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
$ sudo rpm -ivh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

Then install the tools as:

$ sudo yum install kmod-usbip
$ sudo yum install usbip-utils
$ sudo yum install hwdata

NOTE: You may need to reboot your WSL instance after installation.

WSL Attach USB Device

Once you have the required tools installed and configured, its time to connect your USB device to a WSL instance.

Launch a command prompt session with administrator privileges and run the command:

usbipd wsl list

The command will list all the installed USB devices on your Windows machine. An example output is as shown:

BUSID  VID:PID    DEVICE                                            STATE
2-2    046d:c52b  Logitech USB Input Device, USB Input Device     Not attached
2-3    046d:c339  USB Input Device                                Not attached
2-5    0408:a061  HD User Facing                                  Not attached
2-14   8087:0026  Intel(R) Wireless Bluetooth(R)                  Not attached

What we need here is the BUSID of the device we wish to connect to the WSL instance. In our example, we wish to attach the device with BUSID of 2-3 to the WSL instance.

To attach the device, use the command syntax:

usbipd wsl attach --busid <busid>

For example:

usbipd wsl attach --busid 2-3

The command above will disconnect the device with the specified busid and attach it to your default WSL instance.

An example output is as shown:

usbipd wsl attach --busid 2-3
usbipd: info: Using default distribution 'Debian'.

WSL View Attached Device

Once the device is connected your WSL instance, you can view it by running the lsusb command.

sudo lsusb

The command will list all the connected devices in your WSL instance as shown:

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 046d:c339 Logitech, Inc. xxx
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

In the example output above, we can see the device BUS 001 as the device we just connected.

WSL Detach Device

Once you are done, you can disconnect your USB device from WSL by running the command (in your windows terminal)

usbipd wsl detach --busid <busid>

For example, to detach the device we just connected, we can run:

 usbipd wsl detach --busid 2-3

And your device is back on your Windows system.

Conclusion

Congratulations, you have successfully connected and disconnect USB devices to and from your WSL instance. If you enjoyed this tutorial or it helped you in any way, feel free to share.

Leave us a comment if you face any issues or contact us for support.

Thanks for reading!!

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.