The Raspberry Pi is a versatile and popular single-board computer, ideal for a variety of projects ranging from home automation to media centers. While many users rely on Wi-Fi for internet connectivity, there are times when a wired connection can offer more stability and speed. Enter Ethernet over USB—a method that can significantly enhance network performance for Raspberry Pi users.
What is Ethernet Over USB?
Ethernet over USB is a technology that allows you to connect your Raspberry Pi to a network using a USB port instead of the traditional Ethernet jack. This approach is especially useful for models like the Raspberry Pi Zero, which lacks an onboard Ethernet port. With the right adapters, you can easily establish a fast, reliable connection to the internet or local network.
For this we will use the USB Gadget.
What is a USB Gadget?
A USB gadget refers to a software-defined device that allows a Linux-based system (like a Raspberry Pi) to emulate a USB device. This emulation enables the system to interact with a host computer as if it were a traditional USB peripheral (e.g., a keyboard, mouse, network interface, or mass storage device).
Key Features of USB Gadgets
- Device Emulation: USB gadgets can mimic a wide variety of USB devices. This allows the Linux system to act as:
- Ethernet (ECM, RNDIS): Allows the device to connect to a network over USB.
- Mass Storage: Enables the device to present itself as a USB flash drive.
- Serial Devices: Allows serial communication over USB.
- MIDI Devices: For music-related applications.
- On-The-Go (OTG) Support: USB gadgets leverage USB OTG functionality, which allows devices to switch between host and device roles, enabling greater versatility in connectivity.
- Kernel Configuration : USB gadgets are configured through the Linux kernel’s configuration filesystem (usually found at /sys/kernel/config/usb_gadget/). This allows users to create, configure, and remove gadgets dynamically.
- Flexibility for Projects : Using USB gadgets, developers can create custom interfaces for various applications, making them ideal for IoT projects, embedded systems, and more.
Use Cases:
- Headless Systems: Connecting to a Raspberry Pi without needing a monitor or keyboard, using a USB Ethernet gadget for networking.
- Development Boards: Creating custom peripherals for projects, such as a USB serial interface for debugging.
- File Transfer: Setting up a mass storage gadget to transfer files easily between devices.
Setting Up an Ethernet USB Gadget on Raspberry Pi
Connect the Usb Type-C port Raspberry pi 4 to pc.
Next , we will need to enable the USB gadget mode by modifying the boot configuration. Open the config.txt file by :
sudo nano /boot/config.txt
# Add the following line at the end:
dtoverlay=dwc2
# Save and exit (CTRL+X, then Y).
Purpose of dtoverlay=dwc2
- USB OTG Support: The DWC2 overlay enables USB On-The-Go (OTG) functionality on the Raspberry Pi. This allows the device to act as both a host and a device, depending on how it is connected.
- Gadget Mode: By enabling DWC2, you can configure the Raspberry Pi to function as a USB device (e.g., Ethernet gadget, mass storage, etc.), allowing it to communicate with a host computer via USB. This is especially useful for models like the Raspberry Pi Zero, which do not have an Ethernet port.
- Enhanced USB Features: The overlay allows the use of advanced USB features, making it possible to set up different USB functions and configurations, such as serial communication or network interfaces, through the kernel’s configuration filesystem.
Next , we will need to create usb ethernet gadget by executing following script :
#!/bin/bash
GADGETDIR='mygadget'
SERIAL=`cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2`
HOSTPREFIX="02"
DEVICEPREFIX="06"
MANUFACTURER="nobody"
PRODUCT='nothing'
# Calculate MAC addresses based on unique serial number of raspberry pi
padded='00000000000000'$SERIAL
basemac=${padded: -12} # last 12 characters
hostmac=$HOSTPREFIX:${basemac: -10:2}:${basemac: -8:2}:${basemac: -6:2}:${basemac: -4:2}:${basemac: -2:2}
devmac=$DEVICEPREFIX:${basemac: -10:2}:${basemac: -8:2}:${basemac: -6:2}:${basemac: -4:2}:${basemac: -2:2}
# Check if running as root
if [ "$(id -u)" -ne 0 ]; then
echo "Must be root" >&2
exit 1
fi
# load the libcomposite module
modprobe libcomposite
mkdir -p /sys/kernel/config/usb_gadget/$GADGETDIR
cd /sys/kernel/config/usb_gadget/$GADGETDIR || exit
echo 0x1d6b > idVendor
echo 0x0104 > idProduct
echo 0x0100 > bcdDevice
echo 0x0200 > bcdUSB
mkdir -p strings/0x409
echo $SERIAL > strings/0x409/serialnumber
echo $MANUFACTURER > strings/0x409/manufacturer
echo $PRODUCT > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower
mkdir -p functions/ecm.usb0
# Assign static mac address
echo $hostmac > functions/ecm.usb0/host_addr
echo $devmac > functions/ecm.usb0/dev_addr
ln -s functions/ecm.usb0 configs/c.1/
ls /sys/class/udc > UDC
Assigning the ip static address to Usb ethernet gadget
on raspberry pi side
nmcli con add type ethernet con-name usb0 ifname usb0 ip4 10.42.0.2/24 gw4 10.42.0.1
nmcli con mod usb0 ipv4.dns "8.8.8.8,8.8.4.4"
nmcli con up usb0
on pc/laptop side
- if you are using linux on your pc/laptop then it is very easy to setup static ip and share the internet .
- open the network manager gui and go to IPv4 setting and select shared to other computers and press ok.
- Now disconnect and reconnect the new interface . it should assign the ip to usb0 interface on raspberry pi .

Conclusion
Ethernet over USB is a powerful tool for Raspberry Pi users looking to enhance their connectivity options. Whether for stability, speed, or flexibility, this method opens up new possibilities for projects and applications. With simple setup steps and minimal cost, integrating Ethernet over USB into your Raspberry Pi projects can help you achieve better performance and reliability in your tech endeavors. So why not give it a try? Your next project could greatly benefit from a solid, wired connection!