How To Share USB Devices Over IP Using A Raspberry Pi

Sep 16, 2022 · 3 mins read
How To Share USB Devices Over IP Using A Raspberry Pi

In the video below, we show you how to configure a Raspberry Pi for USB over IP and how to configure a Linux computer to access its USB devices over the network


If you want to share a USB device over IP, then you can do this for free using a Raspberry Pi

Although, only one Linux computer can use a USB device at a time

This could be used to allow you to configure a VM for instance to access a USB device over the network without tying it to a specific hypervisor so that you can migrate the VM more easily

It allows you to share USB devices from a central location so that you don’t have to be physically present to transfer them between computers

It also provides an option to place USB devices such as radio controllers in areas where they can get better wireless coverage, but access them from a server in a rack for instance

Useful links:
https://derushadigital.com/other%20projects/2019/02/19/RPi-USBIP-ZWave.html
https://wiki.archlinux.org/title/USB/IP

Hardware Suggestions:
GeekPi Raspberry Pi 4 4GB

  1. Raspberry Pi:
    Switch to root account

    su -
    Install software
    apt install usbip
    modprobe usbip_host
    echo 'usbip_host' >> /etc/modules
    List USB devices and note down the product/vendor
    usbip list -l  
    Create start script
    nano /usr/sbin/usbip_start.sh
    #!/bin/bash
    
    usb1='0781:5567'
    usb2='0658:0200'
    
    /usr/sbin/usbip bind --$(/usr/sbin/usbip list -p -l | grep '#usbid='$usb1'#' | cut '-d#' -f1)
    /usr/sbin/usbip bind --$(/usr/sbin/usbip list -p -l | grep '#usbid='$usb2'#' | cut '-d#' -f1)
    Make this executable
    chmod +x /usr/sbin/usbip_start.sh
    Create stop script
    nano /usr/sbin/usbip_stop.sh
    #!/bin/bash
    
    usb1='0781:5567'
    usb2='0658:0200'
    
    /usr/sbin/usbip unbind --$(/usr/sbin/usbip list -p -l | grep '#usbid='$usb1'#' | cut '-d#' -f1)
    /usr/sbin/usbip unbind --$(/usr/sbin/usbip list -p -l | grep '#usbid='$usb2'#' | cut '-d#' -f1)
    killall usbipd
    Make this executable
    chmod +x /usr/sbin/usbip_stop.sh  
    Create service
    nano /lib/systemd/system/usbipd.service
    [Unit]
    Description=usbip host daemon
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/usr/sbin/usbipd -D
    ExecStartPost=/bin/bash -c '/usr/sbin/usbip_start.sh'
    ExecStop=/bin/bash -c '/usr/sbin/usbip_stop.sh'
    
    [Install]
    WantedBy=multi-user.target
    Start the service
    systemctl --system daemon-reload
    systemctl enable usbipd.service
    systemctl start usbipd.service
    Check what USB devices are found
    usbip list -p -l

  2. Linux Client
    Switch to root account

    su -
    Check USB devices attached
    lsusb  
    Install software and create a service
    apt install usbip hwdata usbutils -y
    modprobe vhci-hcd
    echo 'vhci-hcd' >> /etc/modules
    Create start script
    nano /usr/sbin/usbip_start.sh
    #!/bin/bash
    
    server1='192.168.200.7'
    usb1='0781:5567'
    usb2='0658:0200'
    
    /usr/sbin/usbip attach -r $server1 -b $(/usr/sbin/usbip list -r $server1 | grep $usb1 | cut -d: -f1)
    /usr/sbin/usbip attach -r $server1 -b $(/usr/sbin/usbip list -r $server1 | grep $usb2 | cut -d: -f1)
    Make this executable
    chmod +x /usr/sbin/usbip_start.sh
    Create stop script
    nano /usr/sbin/usbip_stop.sh
    #!/bin/bash
    
    /usr/sbin/usbip detach --port=$(/usr/sbin/usbip port | grep '<Port in Use>' | sed -E 's/^Port ([0-9][0-9]).*/\1/')
    /usr/sbin/usbip detach --port=$(/usr/sbin/usbip port | grep '<Port in Use>' | sed -E 's/^Port ([0-9][0-9]).*/\1/')
    Make this executable
    chmod +x /usr/sbin/usbip_stop.sh
    Create service
    nano /lib/systemd/system/usbip.service
    [Unit]
    Description=usbip client
    After=network.target
    
    [Service]
    Type=oneshot
    RemainAfterExit=yes
    ExecStartPost=/bin/bash -c '/usr/sbin/usbip_start.sh'
    ExecStop=/bin/bash -c '/usr/sbin/usbip_stop.sh'
    
    [Install]
    WantedBy=multi-user.target
    Start the service
    systemctl --system daemon-reload
    systemctl enable usbip.service
    systemctl start usbip.service
    Check USB device is attached
    usbip port
    lsusb

Sharing is caring!