Setup Raspberry Pi for Headless Operation (see this pending revision note)
It seems that many follow instructions to set up their microSD card, but don't give much thought to what comes afterwards. That's understandable, and it's generally not that big a deal if you're connecting a keyboard, mouse and TV/monitor. In the "GUI" environment, you can issue commands to your RPi, and there are tools and tips and cues to point you toward connecting to your network, and other basic tasks that users typically perform.
However, some don't want (or aren't able) to connect keyboards, mice and monitors. This is how I usually do it; I run my RPi's "headless"... I have one "head" on my Mac, and I don't want any more heads to deal with :) But I digress. Here, for what it's worth, is how I do things:
-
Download the latest Raspbian image file from the Raspberry Pi website. Unzip the downloaded
.zip
file to reveal a.img
file. The.img
file is the one that will be burned or flashed to the microSD card. -
Choose your weapon: There are several methods & tools that may be used to copy the Raspbian image file to your uSD card:
- you can do it manually in Mac, Windows or Linux/Unix
- you can use rufus on your Windows PC
- you can use Etcher on Mac or Windows
- "Burn" the Raspbian image file to the media you're going to boot from. Typically, this will be a microSD memory card of 16 GB or larger.
After you've flashed your downloaded image file to the uSD card, you will need to make some changes to it. Before you can make these changes, the /boot
partition must be mounted. You can mount
the /boot
drive using the Finder
app on mac OS, or its equivalent in Linux or Windows. N.B. that /boot
is a FAT32 partition, and therefore may be read or written to by virtually any system on the planet. Unless you're certain you know what you're doing, you should leave the root
partition (/
) alone. It is formatted as an ext4
filesystem, and can only be read & written by certain Linux OS-based distros.
Having mounted /boot
on the just-flashed uSD card, make these changes:
-
create an empty file named
ssh
in/boot
; e.g. using the mac OS terminal app:% cd /Volumes/boot % touch ssh %
-
If you plan to connect your RPi using WiFi, you must also add a file named
wpa_supplicant.conf
. In a text editor (one that uses standard Linux/Unix line endings), copy and paste the following, then save it in the/boot
partition aswpa_supplicant.conf
. Use your country code (US, UK, FR, etc), and your WiFi SSID & password instead of these 'dummy' values.
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
network={
ssid=MyWiFi_SSID
psk=MyWiFiPassword
}
Once you've made these changes, eject/umount
the microSD card.
- If you plan to connect your RPi using Ethernet, nothing additional need be done. The
/etc/dhcpcd.conf
file contains the appropriate configuration to connect to your LAN & communicate with your DHCP server.
Having completed the above steps, your RPi should boot successfully upon application of a suitable power source, and connect itself to your WiFi or LAN (once you've connected the RPi to a router or switch using a standard Ethernet patch cable).
In either case, before you initiate an SSH connection to your new RPi, you may need to know its IP address on the network. If you're on a "zero configuration" network, things may "just work", and your first login is as simple as ssh [email protected]
. Try that; if it works, go to the next step. If not, no worries as there are numerous ways to find the IP address of your RPi:
-
dns-sd -q raspberrypi.local
(OS X only) -
arp -a |grep -E --ignore-case 'b8:27:eb|dc:a6:32'
NOTE: As of this writing: The OUI portion of the MAC address is as follows:
- RPi ver 3B+ and earlier have MAC addresses beginning with
b8:27:eb
; - RPi ver 4B has MAC address beginning with
dc:a6:32
- RPi ver 3B+ and earlier have MAC addresses beginning with
-
arp raspberrypi.local
-
if you have access to it, look through the DHCP server's log of active leases
-
I've developed this "recipe" for IP address discovery
Note: Using a simple arp
will be "hit-or-miss"; here's why that's so. If you've reached the end of the list, and you still don't have your Pi's IP address, then something may be broken or misconfigured. Try one of the forums for support, and as always, please try to be as specific as you can in describing your problem.
There are several things you should tend to soon after the installation. We'll use the terminal for all this, but an alternative approach is to use raspi-config
. What follows is a reasonably complete transcript of the entire post-install process. The terminal output can get verbose, and in consequence the actual commands may get lost in this transcript. In an effort to avoid obscuring the required command sequence, each command prompt is designated by the following character string:
-
Change the default password for user pi:
==> pi@raspberrypi:~ $ passwd Changing password for pi. Current password: New password: Retype new password: passwd: password updated successfully ==> pi@raspberrypi:~ $
-
Change the RPi
hostname
to something meaningful & login using changed hostname:==> pi@raspberrypi:~ $ hostnamectl # verify hostname & details Static hostname: raspberrypi Icon name: computer Machine ID: 6add6f07727f4a44d249ce70609f281a Boot ID: d31c9d0a692d4ecfa17becd144f59328 Operating System: Raspbian GNU/Linux 11 (bullseye) Kernel: Linux 5.10.92+ Architecture: arm ==> pi@raspberrypi:~ $ sudo hostnamectl set-hostname raspberrypi0w # change hostname ==> pi@raspberrypi:~ $ hostname # verify raspberrypi0w ==> pi@raspberrypi:~ $ sudo nano /etc/hosts # edit hosts file # # change from: '127.0.1.1 raspberrypi' # Change to: '127.0.1.1 raspberrypi0w' # save file, exit editor # sudo: unable to resolve host raspberrypi0w: Name or service not known # not to worry! ==> pi@raspberrypi:~ $ sudo reboot ... # login from macos using new hostname: ==> seamus@Dung-Pro ~ % ssh [email protected] ... # ignore warnings,,, we should see the new name in the prompt (and we do!) ==> pi@raspberrypi0w:~ $ # note new hostname in shell prompt
-
Copy your public RSA key from your client (Mac, Windows, etc) to your Raspberry Pi:
==> pi@raspberrypi0w:~ $ logout Connection to raspberrypi0w.local closed. ==> seamus@Dung-Pro ~ % ssh-copy-id [email protected] /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '[email protected]'" and check to make sure that only the key(s) you wanted were added. ==> seamus@Dung-Pro ~ % ssh [email protected] ... ==> pi@raspberrypi0w:~ $ # SUCCESS!
-
Perform an update & upgrade to bring your system's software to a current level
==> pi@raspberrypi0w:~ $ sudo apt update ... 18 packages can be upgraded. ... ==> pi@raspberrypi0w:~ $ sudo apt upgrade ... ==> Do you want to continue? [Y/n] Y ... ==> pi@raspberrypi0w:~ $ sudo reboot
-
Reboot & login - your new system is current & ready to go. ⬇️
There are new changes to be factored in light of this news announcement. IOW, a big revision is forthcoming!