Raspberry Pi Zero is a neat little machine. Weighting just a few grams, half the size of a credit card, and with a thickness of just a couple centimeters with a case. You can put a terabyte of storage if you want, especially with microSD cards that are getting more and more affordable at higher capacity and performance.
While most people would think that it's best for IoT related project, in this article I'm going to do something different. I am going to run a Linux server on it. One neat trick that I'm going to use is hooking it up as a CDC ethernet device to a TP-Link WDR4300 router running OpenWRT.
What am I going to do with it? I was planning to have a Nextcloud instance on it, but have decided it would be too much of hassle. Now I'm not exactly sure, and am simply keeping my options open. I'm thinking to run a Redis and MySQL servers for a recent project of mine.
If you're to follow my footsteps, you'll need a Raspberry Pi Zero, an OpenWRT router with at least one USB port, and a proper USB cable to connect them. Shouldn't this be obvious? Oh, and you'll need a computer to set up both the Pi and the router, which for me is a Linux box.
microSD & Pi
So first of all, set up the Pi. After flashing the latest Raspberry Pi OS on a microSD with your computer, go to the boot
partition.
- On
config.txt
, go to the last line and adddtoverlay=dwc2
. - On
cmdline.txt
, go to the first line and find the wordrootwait
. Addmodules-load=dwc2,g_ether
with a space between them, and when applies, another space between it and the words after. - Last but not least, create an empty file called
ssh
to enable ssh. Now, (safely!) remove the microSD and put it in your Pi.
Time to grab that USB cable. Plug it into your Pi. There are two micro USB ports, make sure to put it on the port labeled USB
and not PWR
.
Testing
The next thing to do is test that you can actually connect to your Pi. You may skip this step if you don't feel like it, but if anything goes wrong, it's easier to spot it here than in later steps. You may go back to this step for troubleshooting.
Plug the other end of the cable to your computer. You'll see a network device popping up. If not, your system might need a driver (search: CDC Ethernet device).
You may need to wait for it to connect. When it's done, see if you can find its gateway IP address (don't make me teach you how). SSH as user pi
into that address (port 22 as usual).
When you're in, you'll know it.
Static IP
Now, to set up static IP network...
Find the IP address of your router. Find an unassigned address for the Pi to use on the subnet.
This section assumes that you're in the SSH. Otherwise, you'd have to adapt. For this you can simply access the microSD directly, on the root
partition.
For the text editor, it'd be most convenient to just use nano
since it's already installed, and connecting to outside network might take some extra effort at this point.
On the Pi, go the file /etc/network/interfaces
. Ensure this line exists:
source-directory /etc/network/interfaces.d
Create a new file on /etc/network/interfaces.d
. You're free to come up with your own name, while I'll be calling it usb-static
. Fill it with:
auto usb0
allow-hotplug usb0
iface usb0 inet static
netmask 255.255.255.0
address <rpi-ip>
gateway <router-ip>
Replace <rpi-ip>
and <router-ip>
accordingly. You may also adapt netmask value if applies.
Save the new file, and turn the Pi off properly. Unplug the USB cable from the computer.
Router
We're almost done, I swear!
SSH into the router (again, don't make me teach you) and install a package called kmod-usb-net-cdc-ether
using opkg
. You might want to run opkg update
beforehand, especially if you can't seem to find the package.
A system restart might be necessary.
Plug the USB cable into the router, and you'll see a device called usb0
.
Finally, you will add the Pi into an existing network. I find this easier to do with the web GUI.
ref:
- https://openwrt.org/docs/guide-user/hardware/usb_gadget
- https://openwrt.org/docs/guide-user/network/wan/wwan/ethernetoverusb_cdc
- https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/ethernet-gadget