🛠 Le tour de passe-passe « clé USB Linux » : Ethernet sur USB (g_ether)
Objectives
Turn the Zero 2 W into "Linux on a USB stick": the data port is an OTG port, and the Zero line leaves the OTGID floating — without an SD card, the board actually boots in USB device mode. By loading the g_ether modules, the port presents itself as a USB network card. Result: one laptop, one data cable + one power cable, and direct SSH without a screen, keyboard, or router.
Hardware
- 1 Raspberry Pi Zero 2 W with a Raspberry Pi OS image (SSH pre-seeded)
- 1 micro-USB data cable + 1 USB-A/USB-C adapter for the laptop
- 1 2 A power supply on the POWER port (the data port does not provide power)
- Optional: a Y-cable or OTG hub to use a single cable
Complete code
# 1) /boot/firmware/config.txt — activer le contrôleur USB en mode périphérique :
dtoverlay=dwc2
# 2) /boot/firmware/cmdline.txt — AJOUTER À LA FIN DE L'UNIQUE LIGNE du fichier :
modules-load=dwc2,g_ether
# 3) /etc/network/interfaces — IP statique côté gadget :
auto usb0
allow-hotplug usb0
iface usb0 inet static
address 10.0.0.1
netmask 255.255.255.0
# 4) Activer SSH :
sudo systemctl enable --now ssh
Then reboot, plug the data port into the laptop: ssh pi@10.0.0.1. On Linux/macOS, the laptop detects an "USB Ethernet" interface (you may need to enable connection sharing or set a static IP 10.0.0.2 on the laptop side).
Expected verification
The laptop detects a new USB network interface (on Linux: ip a shows a new usb0/enX interface). ping 10.0.0.1 responds and SSH works — without Wi-Fi or a screen. Bonus: with NO SD card, the Zero 2 W plugged into the PC enumerates a USB device 0a5c: — this is recovery mode (see the usbboot tutorial).
Common errors
- Adding
modules-load=on a new line incmdline.txt: this file must contain ONLY ONE line, everything must be concatenated. - Modifying
/boot/config.txtinstead of/boot/firmware/config.txtdepending on the image version: check wherebootcode.bin/start.elfare located. - Forgetting the power cable on the POWER port: the data port in gadget mode does not provide power.
- On Windows: no immediate RNDIS/ECM driver, prepare the USB Ethernet driver or test from Linux/macOS.
- Waiting for DHCP: without a server, the laptop must be configured with a static IP in the same subnet as 10.0.0.1.
Other tutorials for this board
- Premier démarrage de la Zero 2 W sans écran (headless)
- Se connecter au Wi-Fi (et comprendre les limites 2,4 GHz / WPA2)
- Surveiller la température et la fréquence de la Zero 2 W
- GPIO en Python : LED et bouton avec gpiozero
- Serveur de blocage de pub réseau avec Pi-hole
- Remettre une Zero 2 W en état de boot : usbboot et boot files
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash