Attaching a Host USB Device to a WSL or Hyper-V VM Ranjan.info

You can use open-source usbipd-win Project for accessing a computer’s physical USB devices from the Windows Subsystem for Linux (WSL2) or Hyper-V virtual machine. This tool allows you to pass through an external USB device connected to a Windows host from any Linux distribution (running as WSL) or virtual machine. It allows to perform any tasks with USB devices (flashing Android devices using ADB and Fastboot, accessing smart cards, working with Arduino hardware, etc.) from a virtual machine or Linux environment.

Usbipd-win uses the TCP/IP protocol to forward USB device traffic over a virtual network interface between the VM/WSL and the host Windows operating system. First, we’ll show how to install the usbipd-win server on a Windows host, then we’ll install the USB/IP client on Linux (WSL) and attach a shared USB device to a Linux VM (WSL).

Usbipd-win supports Windows 8.1 x64 and Windows Server 2012 R2 (and later) and allows sharing of local USB devices on Windows with other virtual machines (including Linux guest operating systems on WSL2 and Hyper-V) Is. Previously on Hyper-V, it was only possible to redirect USB drives to VMs or other USB device types via a limited advanced session mode.

The usbipd-win project is available on GitHub (https://github.com/dorssel/usbipd-win, You can download and install it manually (an MSI installation file is available), but it’s much faster to install using the built-in Wingate package manager.

winget install --interactive --exact dorssel.usbipd-win

Install USBIPD-Win on Windows 10/11

program will make a difference usbipd (USBIP Device Host) Service on Windows: "C:\Program Files\usbipd-win\usbipd.exe" which listens on tcp port 3240

usbipd (USBIP Device Host) service on Windows

An additional rule has been created in Windows Defender Firewall for usbipd.exe to allow access to TCP port 3240 from computers on the local network.

Now let’s configure USBIP support in the Windows Subsystem for Linux environment. Make sure the kernel version in your image is at least 5.10.60.1 (our demo example uses an Ubuntu 22.04 LTS image with WSL 2):

$ uname -a

Now you need to install USB/IP Tools and USB Hardware ID Base.

$ 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

install usbip on linux

Use the following command in the Debian WSL image:

$ sudo apt-get install usbip hwdata usbutils
Install the USB/IP tools in an rpm-based WSL image (CentOS/Oracle Linux):

$ 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
$ sudo yum install kmod-usbip
$ sudo yum install usbip-utils
$ sudo yum install hwdata

Now open an elevated command prompt on the host Windows computer and list the USB devices:

usbipd wsl list

As you can see, none of the USB devices are shared (not shared). You can share a USB device by its BUSID. In my example, I want to passthrough USB mass storage device In the WSL with BUISID 4-2.

usbipd wsl attach --busid 4-2

Attach shared USB device to WSL over IP network

Check that your USB flash drive is connected to WSL:

$ dmesg | tail
$ lsusb

lsusb - check USB device over WSL

If you want to share your USB device with another computer running Linux on the network (this could be a virtual machine with a Linux guest on Hyper-V or another hypervisor), first create an available USB drive on the remote Windows host. List devices:

$ usbip list --remote=192.168.13.21

Now you can mount the required USB device by its ID:

$ sudo usbip attach -remote=192.168.13.21 --busid=4-2

In this example, the IP address of the Windows host on which the usbipd-win server is running is specified.

The shared USB device should now be visible to your Linux tools.

To disable USB device sharing in Windows:

usbipd wsl detach --busid 4-2

Please note that USB drives connected in this way are not recognized as block devices in WSL. test it using lsblk Permission. The fact is that the WSL kernel does not have a driver for USB drives (to connect them, you have to rebuild the kernel).

You will be able to mount the filesystem of a shared USB drive in the standard way on Linux distros.

Therefore, to mount an external USB flash drive, floppy disk, or SD card in WSL, you must use the following command:

$ sudo mkdir /mnt/f
$ sudo mount -t drvfs f: /mnt/f

Mount USB flash drive filesystem with drvfs in WSL

WSL mounts FAT, ExFAT, ReFs, or NTFS drives, as well as VHD images.

Thus, usbipd-win can be used via physical USB devices from physical Windows hosts to WSL, virtual machines, or Linux computers on a network using the USBOverIP protocol.

Leave a Comment