Pi Zero USB Connection

One advantage of a Raspberry Pi Zero over the other members of the Pi family is that it can be powered off it’s ‘USB’ port instead of it’s ‘PWR IN’ connector, thus requiring only a single USB cable to connect it to a host computer.  I have previously connected a Pi Model B using an ethernet cable as described in my post ‘Return to the Raspberry Pi!’, but the USB method makes for a far easier setup.  However to use the USB as a network interface requires some configuration on the Pi Zero.

To start with I imaged a micro SD card with a clean copy of Rasbian Lite.  Then to enable the network interface over USB a couple of configuration files need to be edited in the Pi’s boot partition.  You can do this by inserting the SD card into another computer and accessing the files there.  The two files for editing are config.txt and cmdline.txt.  Firstly, config.txt needs a single line adding to the end of the existing file:


Editing cmdline.txt can be a little tricky.  The entire contents must be on a single line (so don’t press return).  Also options must be entered separated by a single space. At the very end of the file enter a space after rootwait then add:


Now return the SD card to the Pi Zero and plug this into the host computer via the USB cable.  The Pi will startup and the computer should detect it as a network-able device.  On my system I needed to tweak the connection properties to change the IP4 settings method to ‘Shared to other computers’.

Previously, direct ethernet cabling to a Model B required the manual setup of static IP addresses on the Pi’s themselves.  This is no longer needed as Raspbian now includes a multicast domain name service Avahi, which implements the Bonjour protocol originally designed by Apple and is supported on all Macs and recent Linux distributions.  Windows will require additional drivers for this to work with Windows hosts.

The Avahi service will create a special network name, ‘raspberrypi.local’ by default, which can be used in place of the Pi’s IP address.  The ‘.local’ top level domains are specifically reserved for this type of local network usage.  The first part of the name is defined by the Pi’s hostname.  You can test this is working with ping:

ping raspberrypi.local
PING raspberrypi.local ( 56(84) bytes of data.
64 bytes from ( icmp_seq=1 ttl=64 time=0.262 ms
64 bytes from ( icmp_seq=2 ttl=64 time=0.263 ms
64 bytes from ( icmp_seq=3 ttl=64 time=0.275 ms
[1]+ Stopped ping raspberrypi.local

Once this connection is established, it can be used for SSH sessions in much the same way as any other network connection.  However, in my case, I have previously connected a different Pi with the same network name.  This upset SSH as you can see below:

ssh pi@raspberrypi.local
The ECDSA host key for raspberrypi.local has changed,
and the key for the corresponding IP address
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
Someone could be eavesdropping on you (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
Please contact your system administrator.
Add correct host key in .ssh/known_hosts to get rid of this message.
Offending ECDSA key in .ssh/known_hosts:14
  remove with:
  ssh-keygen -f "/home/paul/.ssh/known_hosts" -R raspberrypi.local
ECDSA host key for raspberrypi.local has changed and you have requested 
strict checking.
Host key verification failed.

As I want to be able to work with multiple Pi’s in this manner, the best solution is to give them each a unique name, rather than have to regenerate the keys every time I want to use SSH.  The simplest way to do this is with the Raspbian command-line configuration tool raspi-config.  This requires root privileges so must be started with:

sudo raspi-config

The option to change the hostname is in the ‘Advanced Options’, ‘A2 Hostname’.  I changed mine to ‘rpi0’.  A restart is required for the hostname to be updated.  Once reconnected I could then use SSH as expected:

paul@laptop01:~$ ssh pi@rpi0.local
 The authenticity of host 'rpi0.local' can't be established.
 ECDSA key fingerprint is SHA256:---------------------------------.
 Are you sure you want to continue connecting (yes/no)? yes
 Warning: Permanently added 'rpi0.local' to the list of known hosts.
 pi@rpi0.local's password:

The programs included with the Debian GNU/Linux system are free;
 the exact distribution terms for each program are described in the
 individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
 permitted by applicable law.
 pi@rpi0:~ $


Leave a Reply