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 (Microsoft) or MacOS/OSX (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 169.254.0.0/16 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.

ifconfig

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:10.42.0.1 Bcast:10.42.0.255 Mask:255.255.255.0
inet6 addr: xx:xx:xx:xx/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
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 10.42.0.29 netmask 255.0.0.0 broadcast 10.255.255.255
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 your 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 appending this to cmdline.txt on the boot partition on your pi:

g_ether.host_addr=8a:3e:d4:ce:89:53

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 10.42.0.xxx 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=10.42.0.29
static routers=10.42.0.1

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

Conclusion

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.

Tags:

11 Responses to “Connecting to a Raspberry Pi Zero with just an USB cable (II)”

  1. Bbear Says:

    Hi, what kind of speeds can I expect? I was thinking of maybe creating a pihole / VPN client / server with the pi zero. But I’m not sure about the speed.

  2. webonomic Says:

    I get 3/4MBs over WiFi to a Pi Zero that is connected with USB to RPI2.

    You should give it a try, a Pi-hole should run on any Rapsberry Pi model.

  3. Kriss Says:

    Hi
    Thank you for the article. Can i put two (or even three) Pi zero on a usb hub attached to a laptop the way you describe? Of course, I would configure them 1 by 1, giving them distinct MAC and IP (and distinct or same netmask?). But there are many other things that I wonder: can 2 (or 3) OTG devices be attached to an usb hub? will the laptop OS handle this? Can the 2 or 3 PiZero be put on the same network/netmask?

    Your advice would be welcome!
    Kriss

  4. Kriss Says:

    I forgot: I’m long experience in linux, but am a true beginner with the Pi…

  5. webonomic Says:

    @Kriss That’s the fun of a Pi we all play like children with our toy.

    Yes, you can put more Pi on a laptop or hub, although you probably need a powered hub.

    I think when I connected two Zero’s to my Ubuntu laptop, I gave them two different networks to each USB interface.

  6. radzimir Says:

    Hi,

    Based on this and some other descriptions I have just made a script setting up Raspberry Pi image with OTG functionality.

    https://github.com/radzimir/Raspberry-Pi-OTG

  7. Connecting to a Raspberry Pi Zero with just an USB cable (I) - dev.webonomic.nl Says:

    […] What's up? « Running browsers headless Connecting to a Raspberry Pi Zero with just an USB cable (II) […]

  8. 4 ways to connect your Raspberry Pi 4 to the internet - dev.webonomic.nl Says:

    […] For more details, see these posts Connecting to a Raspberry Pi Zero with just an USB cable (I) or Connecting to a Raspberry Pi Zero with just an USB cable (II) […]

  9. How to run or boot Raspbian on a Raspberry Pi Zero without an SD-card. - dev.webonomic.nl Says:

    […] address range, and we give the pi the fixed address 10.42.0.14. Please read my other posts about setting up an USB connnecting with the pi Zero, if you need more […]

  10. ieee488 Says:

    I am using Linux Mint 18.3

    The section titled ‘Create a stable USB ethernet connection’

    I am not sure what I am doing wrong.
    Is it required to re-boot the PC?

  11. webonomic Says:

    No, not necessarily, but you’ll need to manually disconnect and connect again.

Leave a Reply