Connecting to a Raspberry Pi Zero with just an USB cable (II)

October 2nd, 2017

This tutorial assumes you’re using Raspbian Strectch, and a Linux (16.04LTS) laptop, but it should also work on Raspbian Jesse for the Zero and any Linux Laptop. So it’s for everybody. Nearly every laptop can run Linux freely, (that’s not the case with Windows or Apple, that’s paid and not free software). You can always run Linux in a virtual machine, if you’re afraid to dump proprietary stuff or install Linux side a side.

In part 1 we explained how you can connect to a Raspberry Zero computer with an ordinary USB cable to power, connect (SSH) and provide internet access for the Pi with higher speeds than most WIFI connections.

That’s all been made possible with the smart USB OTG functionality that enables the Pi Zero to switch USB host/device mode and mimic an ethernet device (or webcam, or keyboard).

We showed you how to connect from a Linux laptop to the Pi by setting the connection type to Link-Local only.

There are still a few issues.

  • Connecting is still verbose.
  • The Link-local connection is creating a network in the address block. This will block the Pi from reaching the internet on most computers. We need something in a Private IPv4 address range, like ​10.42.0.xx., different from your router, which will probably be in the 192.168.xx.xx address range.

First things first.

Make SSH more comfortable

You can ssh to the Pi but you still have to type the password and  ssh pi@raspberrypi.local. We gonna make that easy.

First we gonna set the hostname on your pi to something more specific and unique, important if you have more than 1 Raspberry Pi: you have to give each their own name.

Login to your pi:

and change raspberrypi in both /etc/hostname and /etc/hosts  on your Raspberry Pi to pi0 or anything of your liking.

This will enable you to connect like ssh pi@pi0.local .

But we gonna make it even easier. Step 2.

Open up ~/.ssh/config on your laptop:

Add this:

Host pi0
HostName pi0.local
User pi

Save the file  and now you can make connection with ssh pi0. If have chosen another name instead of pi0 use that.

But you ‘re still asked for the password every time. So step 3

To fix that, use ssh keys, to do this on an Ubuntu laptop.

ssh-copy-id pi@pi0

This will copy your ssh key to the pi and add it to the `.ssh/authorized_key` file.

Next time you do

ssh pi0 

And you’re connected, no questions (=password) asked! These are all real time-savers.

Create a stable USB ethernet connection

For a stable connection we need to be in the right private address space. Disconnect the established connect with the pi, en edit the connection in the Edit connection dialog on your laptop and set method to Shared with other computers in the  IPv4 tab. If your using Fedora, this method is not in the default GNOME settings dialog, you have to fire up `nm-connection-editor` from a terminal.

Then start/select that connection again. With ifconfig check that the ip-address is now something like 10.xx.xx.xx.


There are a few other issues. An OTG USB ethernet device creates two random MAC addresses on both sides of the virtual OTG ethernet device: laptop <– OTG –> Pi Zero

You can inspect those two MAC addresses by issuing ifconfig on your laptop and your Raspberry Zero.

Your laptop will output something like:

enp0s29xxxxx Link encap:Ethernet HWaddr 8a:3e:d4:ce:89:53
inet addr: Bcast: Mask:
inet6 addr: xx:xx:xx:xx/64 Scope:Link
RX packets:459 errors:0 dropped:0 overruns:0 frame:0
TX packets:308 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:30199 (30.1 KB) TX bytes:48162 (48.1 KB)

And your Pi Zero

usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet netmask broadcast
inet6 xx:xx:xx:Xx 5 prefixlen 64 scopeid 0x20<link>
ether e6:30:e9:84:55:33 txqueuelen 1000 (Ethernet)
RX packets 287 bytes 42214 (41.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 405 bytes 32030 (31.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

One of the problems (actually a feature) is that the Raspberry Pi Zero will create a new random MAC address every times it boots, so your laptop will also see a different device every time, and will create a new connection, instead of re-using the old one, (remember it will try to connect again as if the Pis is an access point, which it isn’t.)

Probably this random MAC creation is a security feature, to prevent fingerprinting. Don’t forget we’re all tracked in shopping centers by the MAC-address of our phones. A MAC address can be seen, even if you’re not connected to a WIFI network. So disable WIFI on you’re phone when you’re out!

To solve this problem we have to set the Pi to a fixed MAC address, this can be done, by editing again, cmdline.txt on the boot partition. The important thing is to set the hardware address on the host, so your laptop doesn’t create a new connection after reboot.

You do this by appendign this to cmdline.txt on the boot partition on your pi:


Just take the address form the ifconfig command of your laptop, after you have established a connection. Now we nearly done, one more thing to fix. The Pi is still choosing a random ip-address every time it boots, giving this message every time you make connection ssh:

Warning: Permanently added the ECDSA host key for IP address to the list of known hosts.

To make the RPI choose a static IP address, edit `/etc/dhcpcd.conf `and add

interface usb0
static ip_address=
static routers=

There are tutorials on the internet that advice you to edit /etc/network/interfaces. That is out-of-date information and obsolete with Debian Jesse and Stretch. Don’t try it that way, maybe it will work, maybe it won’t, but it’s the wrong way of doing these days. Just read the first line of that file:

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d


To sum it up, make the Raspberry Zero to choose a static MAC Address on the virtual OTG ehternet device and set  a static IP-address. And don’t forget to tweak you’re ssh settings, use keys instead of a password.

In the end you can just plug in your Zero with an USB cable and after a while, when the green led is glowing, connect to your pi with `ssh pi0` That’s all.

Leave a Reply