Setting up a Raspberry Pi from a Mac
As part of the same hardware project I wrote about yesterday, we’re looking into using a Raspberry Pi as an MQTT broker with a couple of ESP32 clients.
It turns out this is the first time I’ve ever actually had to set up a Raspberry Pi from scratch, and despite what I’d been led to believe, it’s annoyingly fiddly, especially if you want to connect to it over wifi on the first boot.
Here are the notes I made while I was learning. I expect I will need to remind myself of them at least a few times again until it becomes second nature!
You will need:
- A Raspberry Pi with wifi access
- A microSD card
- A microSD card adaptor, or external card reader, for your Mac
- A wifi network
Flash Raspbian onto the microSD card
- Download https://downloads.raspberrypi.org/raspbian_latest
- Download and install balenaEtcher if you don’t have it already.
- Plug the microSD card into your Mac.
- Open Disk Utility, and check the microSD card is FAT32 formatted. If it isn’t, erase/reformat it to FAT32.
- Open balenaEtcher, select the
raspbian-stretch.zip
file you downloaded as the “image”, and select the microSD card as the “drive”. Then press “Flash!” - Once the flashing as finished, balenaEtcher will “helpfully” unmount the volume from your Mac. Mount it again by opening Disk Utility and clicking “Mount”, or just remove and reinsert the physical SD card.
Enable SSH access to the Pi
-
Create file named
ssh
on theboot
drive. The file doesn’t need any content. For example, you could open Terminal and type:touch /Volumes/boot/ssh
Enable Wifi on the Pi
-
Create a text file called
wpa_supplicant.conf
on theboot
drive, then open it in a text editor. For example, you could open Terminal and type:nano /Volumes/boot/wpa_supplicant.conf
-
Fill it with the following text, replacing the three placeholder values with your ISO-3166-1 two-letter country code (eg:
GB
), your wifi network SSID, and your wifi network password:ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=«your_ISO-3166-1_two-letter_country_code» network={ ssid="«your_wifi_ssid»" psk="«your_wifi_password»" key_mgmt=WPA-PSK }
First boot
- Eject the microSD card from your Mac, and insert it into the microSD slot on the Pi.
- Connect the Pi to power.
- Wait a few seconds for the Pi to boot.
You should be able to tell when the Pi has booted by running this command in Terminal on your Mac:
ping raspberrypi.local
Once the Pi has booted and joined your wifi network, you can SSH in with:
ssh pi@raspberrypi.local
The password for the default pi
user is raspberry
.
Changing the hostname
If you want to change your Pi’s hostname from raspberrypi.local
to something unique, you can do it once you’re SSHed in:
-
Switch to superuser mode:
sudo su
-
Open
/etc/hostname
and replaceraspberrypi
with the hostname of your choice:nano /etc/hostname
-
Do the same with
/etc/hosts
, whereraspberrypi
appears somewhere near the end of the file:nano /etc/hosts
-
Reboot your Pi:
reboot
Now ssh pi@raspberrypi.local
won’t work any more, because your Pi has a new hostname. You’ll need to change your SSH command to use the new hostname, eg:
ssh pi@mylovelypi.local
When you’re SSHed in to the Pi, you can quickly check the current hostname by running:
hostname
Changing the username and password
Changing the pi
user’s password is easy. Once you’re SSHed in as the pi
user, just run:
passwd
It’ll lead you through the process of setting a new password.
If you want to change the pi
user’s username (to something other than pi
) then you’ll need to activate the root
user, SSH in as that, and change the pi
user’s username from there. Detailed instructions here.