The Raspberry Pi Zero is a tiny single board computer with 512MB of ram and a 1GHz single core CPU. While this isn't the fastest single board computer by any means, it's tiny form factor let you build some amazing projects with it! Today I'll be showing you how you can set up your Pi Zero so you can SSH into it over USB. To achieve this we're going to emulate an Ethernet interface over USB, this is supported out of the box for Raspbian, which is really cool! I know blogs about this already exist, however they really kind of just say "do this" and not the why and what it actually does. So, hopefully you learn something!
To get started make sure you have the following:
You'll also need a way to be able to flash the MicroSD card. No matter your platform, I recommend using Etcher to flash the MicroSD, it's a FOSS based on electron that lets you easily and quickly flash storage devices.
First, you're going to need the latest version of Raspbian. At the time of writing this, the latest was Raspbian Buster. You can check and download here.
Make sure you get the Lite image! This will save more room for your project!
After downloading it, extract it, put your MicroSD card into your computer, (no need to format it) open it with Etcher and click 'Flash!' This can take a while so go ahead and make yourself some coffee. :)
Once that finishes, we're going to have to edit some files on the root of our MicroSD. Everything we're going to be editing is in the volume with the 'boot' label name. First, create a file named 'ssh' on the root of the MicroSD. This will enabled SSH without us having to actually boot the pi up and enable it manually. With SSH you can get a remote terminal, and even send files to your pi zero!
On *Unix systems:
Alright, now that we have ssh enabled, we want our Pi Zero to talk to our computer over it's USB interface and act like an Ethernet adapter, to do that we will need to modify some files.
dwc2 is a service that enables the USB host service. This lets our Pi Zero act like a USB device! There are tons of super super cool projects just with this, but for now we'll only be using this to setup a fake Ethernet adapter.
Now we have to modify cmdline.txt, this will setup the Ethernet USB services at boot.
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=17869b7d-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether quiet init=/usr/lib/raspi-config/init_resize.sh
g_ether is what emulates an Ethernet adapter. This will let us connect to our Pi over USB!
Now that you've setup your distro, you're still going to need to be able to actually SSH into it. Most *Unix distros provide a ssh client by default and can be used by opening a terminal and typing 'ssh [user]@[ip]', however since you're using Windows, you're going to need an SSH client. PuTTY is a very nice SSH client with a built-in terminal emulator.
Now, put the MicroSD into the Pi Zero and plug it into your computer. It will start booting. Once the LED stops flashing it's done booting (It might reboot because of FS resizing)
[ SSH example on pop-os, an ubuntu based distro ]
If you're on an Ubuntu-based distro and keep getting a 'raspberrypi.local: hostname not found' error, go to your wired internet settings and make sure both IPv4 and IPv6 have 'Link-Local Only' checked, like so:
(This might also apply to Windows and MacOS as well, but I didn't have access to a windows machine to test this at the time of writing this.)
Congrats! You've just successfully setup your Pi Zero to be used over USB! Have fun with your project, and I hope you've learned something new!