4

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?

6

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!

3

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 raspberrypi@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

  • 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 at 19:26

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.