Last modified: 2021-12-18 23:03

Quick Introduction to Ethernet OTG

Just in case you really didn't hear about OTG and/or gadget mode, here's a short explanation.

In gadget mode, a Linux computer (this is the gadget and doesn't need to be a Raspberry) connects to another computer (the host) as a USB disk, a keyboard or a network interface.

Although that's not Raspberry specific its interesting with them taking their size and mobility into account. Here's an example from the MagPi Magazine: Make a Pi Zero W Smart USB flash drive. I didn't try that but it sound really cool.

If you own a Raspberry Zero without wireless then OTG ethernet is the only way to connect the device to the Internet to install new software or updates.


Here's a thread from the Raspberry forum that explains how to configure OTG ethernet gadget mode on a Raspi 4. It's the same for Raspberry Zeros.

It comes down to adding


to the Raspi's /boot/config.txt and


to /boot/cmdline.txt. In case you didn't already enable ssh on the gadget, create the file /boot/ssh to start the sshd daemon when it boots.

That's all you need to do. However, with this setup the Raspi chooses a different MAC address on each boot and gets a different IP-number every time. That's not an issue but when you use ssh to login to the gadget ssh adds a new entry to your ~/.ssh/known_hosts and you might want to clean it from time to time. Alternatively, the forum thread shows how to fix the MAC and this issue.

Connecting to the gadget

To put a Zero into gadget mode you connect the Pi's USB port (the USB port next to the HDMI interface) to any USB port of your host. There are two thing to know:

  1. Not all USB cables work. Some cables seem to provide only power and/or perhaps copying data too but they are not good enough for OTG application. For instance, the official Raspberry USB cable works and most of the cables that come with mobile phones work. However, I have a bunch of cables that don't.

  2. With the right cable on the right port you can connect to a Linux computer like another Raspberry. (Note: sometimes / seldom the Raspi host fails to identify the gadgets and returns a kernel error in then system logs.) Windows computers must install an NDIS driver to do networking on the USB port.

On your Linux host you can run dmesg to check if everything went well:

$ dmesg | grep CDC
[16740.998800] cdc_ether 1-1.3:1.0 usb0: register 'cdc_ether' at
usb-0000:01:00.0-1.3, CDC Ethernet Device, xx:xx:xx:xx:xx:xx

where xx:xx:xx:xx:xx:xx is the MAC address of the host's usb interface, see ifconfig usb0. (Usually the device is usb0.)

OK, you have the network connection but what is the gadget's IP number? This is dynamically chosen each time it boots and connects to a host. The thing is that you don't need to know that (when working with a Linux host). What you really need is the gadget's hostname. If that's e.g. zero-01 then you can connect to it with

$ ssh zero-01.local

The "domain" .local is important. Without you may not be able to connect because the host will then use the normal way to resolve the name zero-01. With .local it will resolve that with MDNS on its directly connected interfaces.

For Windows hosts I have read (but didn't check) that they don't implement MDNS properly. The best way to work around is to fix the client's IP-number.

Interacting with the gadget

So far the gadget can talk to the host. For Internet access the host must forward all requests from the gadget. This needs to be configured in the host's firewall rules and the script masq-routing does this. You run this from the host with

$ sudo masq-routing zero-01.local

When you add the gadget's hostname on the command line the script tries to ssh to it and sets its default route to the USB host.

otg-disk is another script I have prepared for OTG ethernet. It is installed on the gadget and starts a samba file server - if USB ethernet is found - to allow picking files from the Raspi. See OTG Disk for more information.

Things that are not working

Mobile phones run a Linux kernel and I would expect them to be OTG hosts as well. However, connecting a Raspi to it (using a compatible cable, which is more difficult to find) brings the Raspi into gadget mode with usb0 configured but the mobile doesn't recognize it. So, USB tethering is not working and accessing the Raspi from a mobile HTTP browser fails too. But it could be that there are more thing to do and I don't know.