This tutorial shows how to share your USB device, e.g., USB mouse/keyboard, USB drive, webcam, and speaker, in Ubuntu so you can access remotely from other computers/laptops.
Linux Kernel includes a command line tool called USB/IP, allowing to share USB devices between computers with their full functionality. So, a computer can use remote USB devices as if they were directly attached.
If you just want to share USB mouse & keyboard between computers, then Barrier is a good choice. But for webcam, usb drive, printer (through it usually has option to do the job), and other general USB devices, this tutorial will show you how.
Step 1: Install the USB/IP kernel tool
First of all, press Ctrl+Alt+T
on keyboard to open up a terminal window.
When terminal opens, run command to install the Linux kernel tools:
sudo apt install linux-tools-$(uname -r)
This command installs the kernel tools for current Kernel version. As Ubuntu updates its kernel regularly, you may need to re-run this command again for newer kernels.
Step 2: Share USB devices at run-time
If you just want to try out this method, that works until reboot or you manually disable it, then run the commands below one by one.
1. First, load the USB/IP specific kernel modules. On the host computer with the USB devices connected, you need to first load the usbip_core
and usbip_host
kernel modules.
To do so, open terminal (Ctrl+Alt+T) and run command:
sudo modprobe usbip_core
sudo modprobe usbip_host
2. Then, run the daemon. There’s a usbipd
command for running the server daemon, which by default listen on TCP port 3240.
To start the daemon, simply open terminal and run command:
usbipd &
If port 3240 is in use, or you want to listen on another port, then use usbipd --tcp-port 1234 &
(replace number 1234) instead.
3. Next, print all USB devices on computer, for the information, such as bus-id, vendor ID and product ID.
usbip list -l
In the output, you need to remember the bus-id (usually 1-1, 1-2, …) for the USB device you want to share.
Finally, bind the USB device so it can’t be access remotely (replace bus-id 1-1
with yours).
sudo usbip bind -b 1-1
It should outputs something looks like “bind device on ***: complete”. However, for USB keyboard/mouse, if you move or use the devices on host before connecting from remote, the bind may fail.
And, to stop sharing the USB device, use command:
sudo usbip unbind -b 1-1
Step 3: Access the USB Device from Remote Machine
For the client machines, you also have to install the USB/IP tool. See step 1 for Linux, and go to this page for Windows (not tested).
Then, you have to load the vhci_hcd
kernel module by running command:
sudo modprobe vhci_hcd
Finally, list accessible remote USB devices:
sudo usbip list -r 192.168.0.104
And, attach the remote USB via command:
sudo usbip attach -r 192.168.0.104 -b 1-1
Here replace 192.168.0.104
with yours host IP, and replace 1-1
according which bus-id you shared. If everything goes well, you should now be able to use the remote USB device just like directly attached.
To disconnect remote USB, use command to first list all connected:
sudo usbip port
Then, run command to detach (replace 00
according to last command output).
sudo usbip detach -p 00
Enable USB Device Sharing on Startup
If the previous steps work good, and you want to make it permanent, then you can write rules to auto-load the kernel modules, auto-start the daemon, and auto-bind the USB device.
NOTE: In the steps below, I will use gnome-text-editor
(Gnome Default) to create & edit conf file. You may replace it with gedit
for 22.04 and earlier, mousepad
for XFCE desktop, or nano
text editor (Ctrl+S to save, Ctrl+X to exit) that works in most desktop environments.
1. First, auto-load the kernel modules on system boot by creating a custom conf file via command (Ctrl+Alt+T):
sudo gnome-text-editor /etc/modules-load.d/usbip.conf
When file opens, just add the 2 lines below and save:
usbip_core usbip_host
2. Then, auto-run the usbipd
daemon by creating a systemd service via command:
sudo gnome-text-editor /etc/systemd/system/usbipd.service
When file opens, add the lines below and save:
[Unit] Description=USB/IP server After=network.target [Service] ExecStart=/usr/bin/usbipd [Install] WantedBy=multi-user.target
Finally, run command to enable & start the service:
sudo systemctl enable --now usbipd.service
NOTE: The service may fail to start until system reboot, as you already run the daemon in step 2-2.
3. Finally, create a service to auto-bind the USB device your want to share. To do so, run command:
sudo gnome-text-editor /etc/systemd/system/[email protected]
When file opens, add the lines below and save.
[Unit] Description=USB-IP Binding on bus id %I After=network-online.target usbipd.service Wants=network-online.target Requires=usbipd.service #DefaultInstance=1-1 [Service] Type=simple ExecStart=/usr/bin/usbip bind -b %i RemainAfterExit=yes ExecStop=/usr/bin/usbip unbind -b %i Restart=on-failure [Install] WantedBy=multi-user.target
Next, you need to run command to enable & start the service:
sudo systemctl enable --now [email protected]
In this command, replace 1-1 according to which bus-id of the USB device you want to share.
When done, restart your computer and try to attach from remote computer.
Undo:
To undo the last step, first open terminal (Ctrl+Alt+T) and run command to disable the services (also replace 1-1 accordingly):
sudo systemctl disable --now [email protected]
sudo systemctl disable --now usbipd.service
Then, remove the 3 config files, though the service files do not function until you re-enable them:
sudo rm /etc/systemd/system/[email protected]
sudo rm /etc/systemd/system/usbipd.service
sudo rm /etc/modules-load.d/usbip.conf