🛠 Gadget USB : Ethernet sur le port OTG + relais Wi-Fi

Raspberry Pi Zero W (2017) — Board Sheet · intermediaire · Schéma : IMG-zero-w-intermediaire-2-gadget-usb-ethernet-sur-le-port-otg.svg — Zero W branchée au port USB d'un PC via le port données micro-USB ; elle apparaît comme interface usb0 et relaie Internet par Wi-Fi.

Objectives

Use the micro-USB data port (OTG, BCM2835) in gadget mode: the Zero W enumerates as an Ethernet card on any PC while relaying Internet through its Wi-Fi — the "Linux USB stick", with no configuration required on the PC side.

Hardware

Steps

  1. Enable the controller: in /boot/config.txt add dtoverlay=dwc2, and modules=dwc2 on the dwc_otg.lpm_enable=0 console=... line in /boot/cmdline.txt.
  2. Create the Ethernet gadget via configfs/libcomposite (modprobe libcomposite, usb0 in g_ether RNDIS/CDC).
  3. Link the two network interfaces: enable routing sysctl -w net.ipv4.ip_forward=1 and masquerade behind wlan0: iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE.
  4. On the PC, the interface appears (usb0 / "Remote NDIS") and obtains an address via DHCP through dnsmasq on the board.

Verification

ip a shows usb0 UP; from the PC, ping to a public IP goes out through the Zero W's Wi-Fi.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials