Firstly, this only works on the USB C port not on the USB 2 or USB 3 ports. Because of this you may have to find another way to get power into the 4B.
- Requirements:
- Raspberry Pi 4B (any RAM size) and asociated cables, SD card etc.
- USB A male to USB C male cable.
- Another computer to act as the USB host. I've not tried it but I guess you could loop back to the USB 2/3 ports on the 4B
- Procedure:
- On the 4B, edit /boot/config.txt and add
Code: Select all
dtoverlay=dwc2
- On the 4B, edit /boot/cmdline.txt adding
Code: Select all
modules-load=dwc2,g_ether
- Reboot
- Log in to the 4B and run You should see a new usb0 network interface.
Code: Select all
ifconfig -a
- If you haven't already, connect to your USB host with the USB A to USB C cable. The USB host will show a new USB network adapter.
- Configure both enternet interfaces as required. DHCP, static IP addresses, bridging, routing, etc
- On the 4B, edit /boot/config.txt and add
- Gotchas:
- MAC addresses used for each end of the USB link are randomly generated each time the g_ether module is started. This will not cause problems if using static IP addresses but will when using DCHP assigned one (especially if your DHCP server sets address based on the MAC address).
Fixed MAC addresses can be set by appending this to /boot/cmdline.txt:MAC addresses are expected to be unique to an interface.Code: Select all
g_ether.host_addr=<MAC addres> g_ether.dev_addr=<MAC addres>
I have a script here: https://github.com/thagrol/usb-gadget that can generate suitable MAC addresses based on the serial number of the Pi it's running on. RUn asand copy the generated MAC addresses into /boot/cmdline.txtCode: Select all
sudo ./set_id.py --test
- The content of /boot/cmdline.txt must be on a single line with a linux line ending. It's best to edit it on a Pi with nano/vi/leafpad/etc rather than on windows.
- From a network/ethernet point of view it doesn't matter which end of the link is running as a USB master or slave. You can have DHCP servers etc running on the 4B and have them available to the USB host.
- The normal (USB A) ports on the 4B are unaffected by this.
- If the 4B does not switch to device mode, this can be forced by changing your config.txt entry to
and rebooting.
Code: Select all
dtoverlay=dwc2,dr_mode=peripheral
- MAC addresses used for each end of the USB link are randomly generated each time the g_ether module is started. This will not cause problems if using static IP addresses but will when using DCHP assigned one (especially if your DHCP server sets address based on the MAC address).