Raspberry Pi Zero’s come equipped with a USB OTG port which means they can act as a USB client, such as in this case becoming a network adapter. There are quite a few articles on configuring the Pi to do this but most of them are based on Raspbian Jessie rather than Raspbian Stretch. There’s only a small difference but it’s not well documented really.
This guide will cover:
- Flashing an SD card with Raspbian Stretch.
- Configuring the SD card from an Ubuntu host machine to act as a network client with USB OTG.
- Configuring the host machine to relay internet access to the Pi.
This guide also assumes some familiarity with the Linux console and Ubuntu.
Step 1 – Flashing the Pi
This is a simple enough step but somewhat depends on the host machine and your preferences. I used the minimal Raspbian Stretch image from here. You might choose to use the full version but keep in mind this guide will only work with a version of Raspbian Stretch.
Unzip the downloaded image with your archiver GUI or…
(cd ~/Downloads && unzip *-raspbian*.zip)
Flash the image to your SD card (beware of DD the data destroyer when using this):
sudo dd if=$(ls ~/Downloads/*raspbian-stretch-*.img) of=/dev/mmcblk0
All done, now make sure to type sync
in your terminal then remove the SD card and put it back in again.
Step 2 – Configuring the Pi
With your GUI file manager click on boot and rootfs to mount them if they haven’t been already. Then open up cmdline.txt in the boot partition and add modules-load=dwc2,g_ether
at the end of the line after rootwait
.
Now open up config.txt in the boot partition and add dtoverlay=dwc2
to the end of the file.
Make sure those are saved and then we’ll do the complicated part.
This next part is where it differs between Jessie and Stretch in newer releases you need to modify /etc/dhcpcd.conf
and in older releases /etc/network/interfaces
, this will only cover the former.
So next we need to append the following network configuration to /etc/dhcpcd.conf
:
interface usb0 static ip_address=192.168.137.2 static routers=192.168.137.1
The reason the above chooses 192.168.137.0 is because that’s the default IP range used by Windows Internet Connection Sharing, making this also compatible with Windows.
This is a bit tricky because only root can write the filesystem, so to get round this, run the following:
export INTERFACES=/media/${USER}/rootfs/etc/dhcpcd.conf cat << EOF | sudo tee --append ${INTERFACES} interface usb0 static ip_address=192.168.137.2 static routers=192.168.137.1 EOF
Lastly eject the rootfs and boot partitions using your file manager, and then the SD card is all setup.
Step 3 – Host Machine Setup
First we need to configure the network with a static IP.
- Click the networking icon in the top right and then Edit Connections.
- In the panel that pops up click the Plus/Add and create an Ethernet type.
- Name it PiStatic and then click
IPv4 settings
. - Change
Automatic (DHCP)
toManual
. - Add an IP with the address
192.168.137.1
and the subnet255.255.255.0
and leave the gateway blank. - Click Save and then that parts all done.
At this point connect the Pi to the computer via the USB OTG port and wait patiently for it to boot up, when it has it should automatically connect to the PiStatic network. At this point you should be able to ping/ssh into the Pi using:
ssh pi@192.168.137.2
(the default password is raspberry)
Now the last thing to do is enable forwarding your computers internet connection. In the console run ifconfig
to find out the name of your internet connected interface it may be eth0
or something more obscure like enp0s31f6
.
Then run the following commands (replace eth0
with your network interface):
sudo sysctl net.ipv4.ip_forward=1 sudo iptables -A FORWARD -o eth0 -j ACCEPT sudo iptables -A FORWARD -m state --state ESTABLISHED,RELATED -i eth0 -j ACCEPT sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
The forwarding will not persist across reboots so keep a note of those commands, it’s possible to make it permanent but it is a security vulnerability.
You should now be able to SSH into the Pi and ping external sites such as google.co.uk.