6

How can I SSH to a headless Raspberry PI Zero (no GPIO pins, no WiFi) over a USB cable, connecting to a Mac using a shell window from Mac's Terminal program?

1
8

I found this very helpful for setting up mine: https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/ethernet-gadget

The essential steps are:

1: open up the boot partition on the micro sd with finder and in the file config.txt add dtoverlay=dwc2 to a line on the bottom and save it.

2: Create a new file called ssh using textedit and save it onto the boot partition. Use 'get info' to remove the extension on the file, and it should appear as an EXEC file. You can now eject the card and boot it on your RPi

3: Finally, open up the cmdline.txt. replace all text with this:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether quiet init=/usr/lib/raspi-config/init_resize.sh 

Be careful with this step as the formatting is very picky in this file

4: Eject your MicroSD card and boot it up in your pi! make sure you plug the usb cable from your mac into the USB input of your pi - this can also power it.

5: Once its booted you should now be able to open up terminal, and using ping raspberrypi.local you can see the RPi's IP address.

6: Using this address in terminal tpye ssh pi@<Pi's IP Address>

7: It should ask you for a password, this will default be

raspberry

I'd also recommend having a look at 'VNC Viewer' - a piece of free software that lets you run your pi as a virtual machine inside your mac, all you need is the IP address which you will get when doing the ssh connection

https://www.realvnc.com/download/viewer/macosx/

Hope this helps!

0
7

After you've done what @Barnaby Hewitt says then this issue may occur:

In my case the Ubuntu host machine could not connect to the Pi Zero via SSH because the Ubuntu machine failed to retrieve an IP address. In order to fix that I set static IP addresses on both ends.

I mounted the SD card to the Ubuntu host machine and I created a file on the mounted SD card, ^path_to_mounted_sd^/rootfs/etc/network/interfaces.d/usb0 containing:

allow-hotplug usb0
iface usb0 inet static
address 192.168.137.2
netmask 255.255.255.0
network 192.168.137.0
broadcast 192.168.137.255
gateway 192.168.137.1

After saving the file and successfully un-mounting the SD card I put it in the Pi and plugged connected my Pi to my computer via a USB cable.

Ubuntu's network manager started to show this icon: enter image description here indicating that it tries to retrieve an IP using DHCP. In the end it will fail so we will need to provide a static IP address.

Next you will need to locate the static IP address of the Ubuntu host machine. List all interfaces in order to locate the one that does not yet have an IP address via the ifconfig command. Afterwards run:

sudo ifconfig enp0s20f0u6 192.168.137.3 netmask 255.255.255.0

In my case enp0s20f0u6 is the interface that was located using the ifconfig command on the Ubuntu host. Then I could manage to connect using the command:

ssh pi@192.168.137.2

Because I did not change the default password yet I used raspberry to connect to the Pi.

In case you plug the Pi to the very same USB then you may want to have these network settings remain the same. For the configuration use the Ubuntu's /etc/network/interfaces or use the network manager in order to set these settings.

2

For me it did not work; This works (with IP 192.168.137.2) only if I also edit the file /etc/network/interfaces with

allow-hotplug usb0
iface usb0 inet static
address 192.168.137.2
netmask 255.255.255.0
network 192.168.137.0
broadcast 192.168.137.255
gateway 192.168.137.1

That I got from another place ( https://gist.github.com/gbaman/975e2db164b3ca2b51ae11e45e8fd40a ); 192.168.7.2 may be ok instead of 192.168.137.2

Hope this can be useful for others reading this question.

1

In my case Ubuntu 19.04 I didn't get IP address from Raspberrypi Zero

You can fix it easy just change "USB Ethernet" settings

**IPv4 Method** 
 Automatic (DHCP) --> Link-Local Only

After that you will get a 169.254.xxx.xxx IP address.

ping raspberrypi.local
ssh pi@raspberrypi.local

enter image description here

2
  • 1
    An address in that range is a public IP. That's somewhat suspicious, especially if you're claiming that to be a general answer. – Brick Jun 19 '19 at 19:26
  • 1
    @Brick 169.254.0.0/16 is almost always an IP address assigned automatically by an interface that's set to get its IP via DHCP but can't get one - not a public ip. – SnapShot Dec 9 '19 at 22:13
0

I'll add my 3 cents - maybe it will save someone a few hours.

My setup was still failing with ssh: Could not resolve hostname raspberrypi.local even though I've done all needed steps: (config.txt, cdmline.txt, ssh file) and "Link-Local Only" network setting. I could have connected to RPi by IP address, but I could only discover it by running mmap or arp-scan that both took forever. It turns out that for some reason this Bonjur-like auto-config didn't work when connected through my usb-hub. After connecting directly to my PC it worked like charm.

0

In case anyone else stumbles up on this in future I ran into issues with the Pi4 I had the Pi Zero plugged into. Namely that USB0 came up with a 169.x.x.x link only address. Took me a minute to figure out what was wrong and here are some snippets.

$ ifconfig usb0
usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 169.254.122.46  netmask 255.255.0.0  broadcast 169.254.255.255
...

No worries so I changed the USB0 address to the 192.168.4.3. Note my Zero config was set to 192.168.4.4.

$ sudo ifconfig usb0 192.168.4.3 netmask 255.255.255.0

Still no connectivity but TCP dump shows packets from 192.168.4.4 (Pi Zero).

$ sudo tcpdump -i usb0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on usb0, link-type EN10MB (Ethernet), capture size 262144 bytes
16:56:56.584779 ARP, Request who-has dns.google tell raspberrypi.local, length 28
16:56:57.428907 IP6 fe80::768e:30aa:7659:e902.mdns > ff02::fb.mdns: 0 PTR (QM)? 214.42.254.169.in-addr.arpa. (45)
16:56:57.429024 IP 192.168.4.3.mdns > 224.0.0.251.mdns: 0 PTR (QM)? 214.42.254.169.in-addr.arpa. (45)
16:56:57.430578 IP 192.168.4.4.mdns > 224.0.0.251.mdns: 0*- [0q] 1/0/0 (Cache flush) PTR raspberrypi.local. (70)

The route table did not setup the 192 network but did have the 169 network.

$ route -n
...
169.254.0.0     0.0.0.0         255.255.0.0     U     207    0        0 usb0
...

Lets fix that

$ sudo route del -net 169.254.0.0 gw 0.0.0.0 netmask 255.255.0.0 dev usb0
$ sudo ip route add 192.168.4.0/24 dev usb0
$ ping 192.168.4.4
PING 192.168.4.4 (192.168.4.4) 56(84) bytes of data.
64 bytes from 192.168.4.4: icmp_seq=1 ttl=64 time=1.10 ms
64 bytes from 192.168.4.4: icmp_seq=2 ttl=64 time=0.444 ms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.