USB Flash Drive in Windows WSL2

Make USB flash drives visible for your WSL in Windows

Spread the love

If you are struggling to see your USB flash drives (and work with) in WSL on Windows 11, this article will help you with this.

If you are using Windows (like me) and WSL installed, the microSD cards or USB flash drives won’t appear automatically in your WSL distro (Ubuntu or Debian, etc), so you need to build custom linux kernel and install additional USB utilities on Windows and on WSL distro, attach the microSD card (or USB flash drive) from Windows to your WSL distro (in Windows command line or PowerShell terminal) and mount it manually (in WSL distro terminal).

Build your custom linux kernel

Start your WSL distro (I’m using Ubuntu):

wsl -d Ubuntu

Following this instructions, configure and build your custom linux kernel for WSL:

sudo apt update && \
sudo apt upgrade && \
cd ~ && \
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git && \
cd WSL2-Linux-Kernel && \
cp /proc/config.gz config.gz && \
gunzip config.gz && \
mv config .config && \
sudo make menuconfig

To get the number of CPU cores available in your WSL, run this command:

getconf _NPROCESSORS_ONLN

Build and install new kernel (I have 16 cores):

sudo make -j 16 && \
sudo make modules_install -j 16 && \
sudo make install -j 16

Copy new kernel to your Windows User’s home folder (replace <user> by your Windows user):

cp arch/x86/boot/bzImage /mnt/c/Users/<user>/usbip-bzImage

Create a .wslconfig file in /mnt/c/Users/<user>/ folder and add a reference to the created image with the following:

[wsl2]
kernel=c:\\Users\\<user>\\usbip-bzImage

and then restart your WSL distro running the following commands in Windows cmd or PowerShell:

wsl --shutdown
wsl -d Ubuntu

Install usbipd-win utility on Windows

Following this instructions, install usbipd-win in your Windows:

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

Install usbip utilities on WSL

Run these commands in your WSL terminal:

sudo apt install linux-tools-generic hwdata && \
sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/*-generic/usbip 20

Attach USB device to WSL

Insert your USB device (microSD card or USB flash drive) into one of the Windows physical port (USB, USB-c, microSD or SD, etc.), once the device is shown in the Device Manager – list all available USB devices in your Windows, run in Windows cmd or PowerShell:

usbipd list

The result should be similar to:

Connected:
BUSID  VID:PID    DEVICE                                    STATE
1-1    14cd:1212  USB Mass Storage Device                   Not shared
1-4    8087:0029  Intel(R) Wireless Bluetooth(R)            Not shared
1-7    27c6:5395  Goodix fingerprint                        Not shared
1-12   0c45:6723  Integrated Webcam                         Not shared
3-4    0bda:8153  Realtek USB GbE Family Controller         Not shared

Persisted:
GUID                                  DEVICE

In my case the microSD card is shown here with BUSID – 1-1, VID:PID – 14cd:1212, Device – USB Mass Storage Device.

Before attaching it to WSL distro, it needs to be binded first, run the following command in cmd or PowerShell, Administrator mode:

usbipd bind -b 1-1

Now usbipd list result should look like this:

Connected:
BUSID  VID:PID    DEVICE                                    STATE
1-1    14cd:1212  USB Mass Storage Device                   Shared
1-4    8087:0029  Intel(R) Wireless Bluetooth(R)            Not shared
1-7    27c6:5395  Goodix fingerprint                        Not shared
1-12   0c45:6723  Integrated Webcam                         Not shared
3-4    0bda:8153  Realtek USB GbE Family Controller         Not shared

Persisted:
GUID                                  DEVICE

After this it can be attached to WSL distro:

usbipd attach --wsl -b 1-1

The result should be like:

usbipd: info: Using WSL distribution 'Ubuntu' to attach; the device will be available in all WSL 2 distributions.
usbipd: info: Using IP address 172.17.32.1 to reach the host.

Now the attached microSD card should be listed in WSL distro terminal (I’m using Ubuntu):

lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 14cd:1212 Super Top microSD card reader (SY-T18)
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Bus 001 Device 004: ID 14cd:1212 Super Top microSD card reader (SY-T18) – is my microSD card. I’m using the SD/microSD to USB adapter, since the exact same SD card (or microSD card) in direct laptop’s SD reader slot did not work with WSL for some reason.

ls -al /dev/sd* should show the new USB attached device as one of the /dev/sd*

ls -al /dev/sd*

Should show something like:

brw-rw---- 1 root disk 8,  0 Jan 15 22:45 /dev/sda
brw-rw---- 1 root disk 8, 16 Jan 15 22:45 /dev/sdb
brw-rw---- 1 root disk 8, 32 Jan 15 22:45 /dev/sdc
brw-rw---- 1 root disk 8, 48 Jan 15 22:46 /dev/sdd
brw-rw---- 1 root disk 8, 49 Jan 15 22:46 /dev/sdd1

/dev/sdd and /dev/sdd1 are new records here, so I suppose /dev/sdd is the one, and /dev/sdd1 is the 1st partition of /dev/sdd, let’s check it with another command:

sudo lsblk -f

You should see something like:

NAME   FSTYPE FSVER LABEL     UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sda    ext4   1.0
sdb    swap   1               8418b373-81b4-4a12-8aa1-9a6e595a7294                [SWAP]
sdc    ext4   1.0             193d5217-6469-4205-8067-2e4fe4f76658  946.6G     1% /snap
                                                                                  /mnt/wslg/distro
                                                                                  /
sdd
└─sdd1 ntfs         RASPBERRY D052EED252EEBC7A

My microSD card is formatted in NTFS filesystem.

So all looks good at this point, we are ready to work with our new USB devices attached to WSL. Now we can mount, unmount, read and write to it as it would be a physical USB device.