Raspberry Pi Zero Ethernet Gadget on Linux


Raspberry pi Ethernet

This guide is meant as an extension to the “Relaying internet access to the Pi Zero 1.3 over USB” document written by the SeedSigner team.


The following steps guide you through the necessary commands and configuration changes needed for the creation of a Rpi Zero v1.3 Ethernet Gadget, specifically on Linux.

Grab a cup of :coffee: and let’s get started!

1. Make sure you have your MicroSD card flashed with the image given at the start of the Manual Installation Instructions. This can be done using the disks tool. Select the drive that corresponds to your memory card, click on the three vertical dots, then Restore Disk Image. Don’t eject the SD card after completion!

2. Follow the steps detailed here. Stop after editing the cmdline.txt file.

3. Set a static IP address for your USB Network device by configuring your address in Raspbian via dhcpcd. To do this, you need to edit the dhcpcd.conf file:

cd rootfs/etc
sudo nano dhcpcd.conf

# Add the following lines at the end of the file.
# For the "routers" and "domain_name_servers" addresses use the internal IP address of your HOST machine, meaning the device you will connect your rpi0 to.
# For the "ip_address" use the next available address on your network.

interface usb0
static ip_address=192.168.7.2/24
static routers=192.168.7.1
static domain_name_servers=8.8.8.8 8.8.4.4

4. Eject the MicroSD card and insert it into you rpi0. The rest of the commands will be executed on our HOST machine.

5. Run ifconfig and take note of the output. We will use this to find our rpi0 Interface name later on.

6. Connect your rpi0 to your machine and wait for ~1 minute.

7. Run lsusb to see if your rpi0 is properly connected. Look for Ethernet/RNDIS Gadget.

8. Run ifconfig again and notice any changes compared to the last time you ran the command. The USB interface should begin with en, looking something like enxaef9489ed5c4. Otherwise, look for usb0 or usb1 and so on.

9. Next we need to configure our local computer’s USB ethernet to use the IP address of our HOST.

Important: Make sure to substitute the IP address and the Interface name with the correct ones.

sudo ip a add 192.168.7.1/24 dev enxaef9489ed5c4
sudo ip link set dev enxaef9489ed5c4 up

10. You should now be able to ssh into your Raspberry Pi using raspberrypi.local like this:

ssh pi@raspberrypi.local

# password = raspberry

Hurray!

We can now talk to our rpi0! The last thing we have to do is give it access to the outside world, not just us.

11. Enable IP forwarding and masquerading on your HOST:

# For this you will need the interface name of your internet connected interface.
# ifconfig will once again show you your interfaces.
# Copy the name of the one through which you are connected to the internet.
# Mine shows up as wlp4s0, which corresponds to my wifi card.

sudo sh -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
sudo iptables -t nat -A POSTROUTING -o wlp4s0 -j MASQUERADE

12. To check if you were successful in your last step, you need to ping a website. SSH into your rpi0, like you did in step 10, and:

ping -c 5 google.com

If the response you get looks something like this:

PING google.com (142.251.36.174) 56(84) bytes of data.
64 bytes from muc12s11-in-f14.1e100.net (142.251.36.174): icmp_seq=1 ttl=117 time=29.2 ms
64 bytes from muc12s11-in-f14.1e100.net (142.251.36.174): icmp_seq=2 ttl=117 time=29.3 ms
64 bytes from muc12s11-in-f14.1e100.net (142.251.36.174): icmp_seq=3 ttl=117 time=27.5 ms
64 bytes from muc12s11-in-f14.1e100.net (142.251.36.174): icmp_seq=4 ttl=117 time=26.0 ms
64 bytes from muc12s11-in-f14.1e100.net (142.251.36.174): icmp_seq=5 ttl=117 time=33.9 ms

--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 10ms
rtt min/avg/max/mdev = 26.031/29.178/33.935/2.662 ms

Congrats! Your Raspberry Pi Zero has now access to the internet. :tada:

Note that these changes are not permanent. You will have to start again from Step 5, every time you reconnect your rpi0.