Quantcast
Channel: CodeSection,代码区,Linux操作系统:Ubuntu_Centos_Debian - CodeSec
Viewing all articles
Browse latest Browse all 11063

Provisioning a headless Raspberry Pi

$
0
0

The typical way of installing a fresh Raspberry Pi is to attach power, keyboard, mouse, and an HDMI monitor. This is a pain, especially for the diminutive RPi Zero. This blogpost describes a number of options for doing headless setup. There are several options for this, including Ethernet, Ethernet gadget, WiFi, and serial connection. These examples use a Macbook as an example, maybe I’ll get around to a blogpost describing this from windows.

Burning micro SD card

We are going to edit the SD card before booting, so for completeness, I thought I’d describe the process of burning an SD card.

We are going to download the latest “raspbian” operating system. I download the “lite” version because I’m not using the desktop features. It comes as a compressed .zip file which we need to extract into an .img file. Just double-click on the .zip on Windows or Mac.

The next step is to burn the image to an SD card. On Windows I use Win32DiskImager. On Mac I use the following command-line steps :

$ sudo -s

# mount

# diskutil unmount /dev/disk2s1

# dd bs=1m if=~/Downloads/2018-06-27-raspbian-stretch-lite.img of=/dev/disk2 conv=sync

First, I need a root prompt. I then use the mount command to find out where the micro SD card is mounted in the file system. It’s usually /dev/disk2s1 , but could be disk3 or disk4 depending upon other things that may already be mounted on my Mac, such as USB drives or dmg files. It’s important to know the correct drive because the dd utility is unforgiving of mistakes and can wipe out your entire drive. For gosh’s sake, don’t use disk1 !!!! Remember dd stands for danger-danger (well, many claim it stands for disk-dump, but seriously, it’s dangerous).

The next step is to unmount the drive. Instead of the Unix umount utility use the diskutil unmount macOS tool.

Now we use good ol’ dd to copy the image over. The above example is my recently download raspbian image that’s two months old. When you do this, it’ll be a newer version with a different file name, so look in your ~/Downloads folder for the correct name.

This takes a while to write to the SD card. You can type [ctrl-T] to see progress if you want.

When we are done writing, don’t eject the card . We are going to edit the contents as described below before we stick it into our Raspberry Pi. After running dd , it’s going to become automatically mounted on your Mac, on mine it comes up as /Volumes/boot . When I say “root directory of the SD card” in the instructions below, I mean that directory.

Troubleshooting: If you get the “ Resource busy ” error when running dd , it means you didn’t unmount the drive. Go back and run diskutil unmount /dev/disk2s1 (or equivalent for whatever mount tell you which drive the SD card is using).


Provisioning a headless Raspberry Pi

You can use the “raw” disk instead of normal disk, such as /dev/rdisk2 . I don’t know what the tradeoffs are.

Ethernet

The RPi B comes with Ethernet built-in. You simply need to hook up the Ethernet cable to your network to automatically get an IP address. Or, you can directly connect the Ethernet to your laptop ― that that’ll require some additional steps.

For a RPi Zero, you can attach a USB Ethernet adapter via an OTG converter to accomplish the same goal. However, in the next section, we’ll describing using a OTG gadget instead, which is better.

We want to use Ethernet to ssh into the device, but there’s a problem: the ssh service is not enabled by default in Raspbian. To enable it, just create a file ssh (or ssh.txt ) in the root directory of the SD card. On my Macbook, it looks like:

$ touch /Volumes/boot/ssh

Eject the SD card, stick it into your Raspberry Pi, and boot it. After the device has booted, you’ll need to discover its IP address. On the local network from your Macbook, with the “Bonjour” service, you can just use the hostname “ raspberrypi.local ” (you can install Bonjour on Windows with iTunes, or the avahi service on linux). Or, you can sniff the network with tcpdump . Or, you can scan for port 22 on the network with nmap or masscan . Or, you can look on your router’s DHCP status page to see what was assigned.

When there is a direct Ethernet-to-Ethernet connection on your laptop, the RPi won’t get an IP address because there is not DHCP service running on your laptop. In that case, the RPi will have an address in the range 169.254.x.x , or a link-local IPv6 address. You can discover which one via sniffing, or again, via Bonjour using raspberrypi.local .

Or, you can turn on “connection sharing” in Windows or macOS. This sets up your laptop to NAT the Ethernet out through your laptop’s other network connection (such as WiFi). This also provides DHCP to the device. On my macBook, it assigns the RPi an address like 192.168.2.2 or 192.168.2.3.


Provisioning a headless Raspberry Pi
System preferences -> Sharing
Provisioning a headless Raspberry Pi
Allowing Ethernet devices to share WiFi connection to Internet

In the above example, the RPi is attached via my Thunderbolt Ethernet cable. I could also have used a USB Ethernet, or RNDIS Ethernet (described below).

The default login for Raspbian is username pi and password raspberry . To ssh to the device, use a command line like:

$ ssh[emailprotected]

or

$ ssh[emailprotected]

Some troubleshooting tips. If you get an error “ connection refused” , that means the remote SSH service isn’t running. It has to generate a new, random, SSH key the first time it runs, so startup can take a while. I just waited another minute and tried again, and everything worked. If you get an error “ connection closed “, then it borked generating a key on the first startup. The service is running, and allowing the connection, then closing it because it has no key to use. There’s no hope for things at this point other than reflashing the SD card and starting over from scratch, or logging in some other way and fixing the SSH installation manually. I had this problem happen once, I don’t know why, and ended up just starting over from scratch.

RPi Zero OTG Ether Gadget

The Raspberry PI Zero (not the other models) supports OTG (On-The-Go) USB. That means it can be something on either end of a USB cable, either a host or a device . Among the devices it can emulate is an Ethernet adapter, thus allowing a USB cable to act as a virtual Ethernet connection. This is useful because thePWR same USB cable can also power the RPi Zero. Just be sure to plug the cable into the port labeled “USB” instead of “PWR IN”.

I had to mess with these instructions twice . I haven’t troubleshooted why, I suspect that things failed on the first time around setting up the RNDIS drivers and Internet sharing. Once I got those configured correctly to automatically work, I reflashed the SD card and started again from scratch, and things worked slick.

As

Viewing all articles
Browse latest Browse all 11063

Trending Articles