Connect To A Raspberry Pi Zero With A USB Cable And SSH

As some of you may know, I have a hoarding problem. I am hoarding Raspberry Pi microcomputers. In my personal collection I have one from each generation, making four standard units. Well, I recently picked up another unit, but this time a Raspberry Pi Zero. These things are about the size of a nine volt battery, but pack some serious punch. The problem is they are incredibly rare because they retail for only $5.00.

Unlike the standard Raspberry Pi units, these do not have WiFi, ethernet, or standard sized USB ports. This changes things when it comes to connecting. We’re going to take a look at getting set up with one of these Pi Zero IoT devices and be on your way towards some cool hack projects.

Like with the standard Raspberry Pi units, it takes a micro SD card with the same flavors of Linux flashed to it. You’ll note in my other tutorials, I am using Raspbian, which is a flavor of Debian Linux. I am using the same for the Raspberry Pi Zero.

Flash the Pi Zero however you see fit. If you’re using a Mac or Linux computer, you might check out my previous tutorial on the subject.

This is where things get a little different. Before we get ahead of ourselves, a lot of the material that follows will be from a set of instructions that helped me. These instructions can be found on GitHub by Andrew Mulholland.

Configuring to Emulate Ethernet Over USB

Our long term goal will be to use SSH over USB. This means that we have to configure Raspbian to treat the USB port like an ethernet port. Mount the micro SD card in a computer (not Pi Zero) and open it with Finder, or Windows Explorer, or whatever it is that you use.

The first thing that you want to do is open a file at the root of the mounted drive called config.txt. In this file you want to add the following line at the very bottom:

dtoverlay=dwc2

The above line will set us up for the next file that we alter. The next file we alter is cmdline.txt, but it is a bit different. Parameters in this file are not delimited by new lines or commas, they are delimited by space characters. In this file we want to add the following:

modules-load=dwc2,g_ether

The above parameter should be added after the rootwait parameter. Yes the above parameter is a single parameter, meaning don’t add a bunch of space characters to it. More information on networking over USB on Linux can be found here.

UPDATE 12/11/2016: As pointed out by some readers, the Raspberry Pi Foundation is disabling SSH by default in Raspbian Pixel as a security precaution. More information on the subject can be found here. To enable SSH, create a file called ssh and save it to the root directory of the boot mount on the SD card. The file can be blank, and it has no extensions. It should exist at the same location as the other files that were edited.

At this point the micro SD can be inserted into the Pi Zero.

Connecting to the Pi Zero with USB and SSH

To connect to the Raspberry Pi Zero over USB you’ll need Bonjour or similar installed on your host computer. I’m using a Mac so I was fortunate enough to already be in the clear. For Windows you should be fine installing iTunes or QuickTime and for Linux the Avahi Daemon. Many Linux distributions should have it already installed.

With it installed, power on the Pi Zero with the USB data cable. I made sure to use the port labeled USB, not PWR. This port allows you to power the Pi Zero and do data transfer. Once connected, give it some time because it will have to configure some things for the first time.

When you feel the Pi Zero is ready, enter the following from your Mac or Linux Terminal:

If you’re using Windows you’ll have to use PuTTY or similar. Notice in my SSH command I provided the pi user? If you’re using Raspbian, it is the only user on a fresh installation. The password will be raspberry until you change it or add a new user.

You should be connected! You won’t be able to install anything or run updates because you’re not connected to the internet, but at least you have full headless Linux control.

Conclusion

Raspberry Pi and IoT is awesome. As a long time Raspberry Pi fanatic, the Pi Zero, if you can get a hold of one, is $5.00 well spent. You get a fast IoT device at the size of a pack of gum. Since the hardware doesn’t have WiFi or BLE, we had to configure Raspbian to allow ethernet emulation over USB. Once we did this we were able to SSH into the Pi Zero as if it was somewhere on our network. A perfect jumpstart for our Internet of Things adventure.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.