Setting up a Raspberry Pi from scratch

Getting started with a Raspberry Pi is basically really easy: All you need to do is get a disk image and burn it to an SD card. But of course, that’s just the very basic starting point. Theres’s plenty of setup work left to do after that, so I decided to gather it all in a blog post so I don’t need to re-invent everything every time I start a new project. Here goes:

Burn the image

1. Get the most recent Raspbian image from here: https://www.raspberrypi.org/downloads/raspbian/ .

2. Install Etcher: https://etcher.io/ .

3. Use Etcher to burn the image to a card.

4. Connect a keyboard and a display to the Pi and boot it up.

Login security

By default, it’s possible to login to the Raspberry Pi using the default credentials “pi:raspberry”. If you connect a Pi to the Internet without changing this, your Pi will be compromised in no time at all. Let’s make sure that doesn’t happen:

5. Enable SSH server (Full instructions at https://www.raspberrypi.org/documentation/remote-access/ssh)

sudo systemctl enable ssh
sudo systemctl start ssh

6. Switch from password logins to public key logins by editing /etc/ssh/sshd_config, and making sure that public key authentication is enabled and password authentication disabled:

#PubkeyAuthentication yes
PubkeyAuthentication yes
#PasswordAuthentication yes
PasswordAuthentication no

7. Once you’ve saved the file, restart the SSH server:

sudo service ssh restart

8. Add your public key to /home/pi/.ssh/authorized_keys

nano ~/.ssh/authorized_keys

This is a good time to test that you can login to the Pi using your public key, and that password logins are not accepted. Don’t close your first terminal session when testing! You need that to fix things in case you can’t log in for some reason.

It should now be safe to connect the Pi to the Internet.

Connect

Next we enable the Wifi connection on the Pi. Full instructions for this can be found at https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md .

9. Generate a passphrase for your Wifi network by running this:

wpa_passphrase "mynetworkSSID" "mynetworkpassword"

10. Remove the line with the plaintext password from the output, and add the rest to the configuration file:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

The file contents should look something like this:

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
    ssid="mynetworkSSID"
    psk=alongstringofcharactershere
}

11. Exit your editor and reconfigure the network:

wpa_cli -i wlan0 reconfigure

Update your software

The Raspbian images don’t get updated constantly, so the default software on your Pi will be out of date.

12. Run software updates:

sudo apt-get update
sudo apt-get upgrade

Prepare for the inevitable

Since doing the above is a lot of work, it makes little sense to do it every time you need to start a new project (or start an old one from scratch…) With the ready-to-rock installation on the card, let’s make a backup image from the card that we can use to fast forward the process the next time.

Full instructions for backing up the SD card for the major operating systems can be found here: https://thepihut.com/blogs/raspberry-pi-tutorials/17789160-backing-up-and-restoring-your-raspberry-pis-sd-card . Here are the relevant steps copied for Mac users:

13. To create a back-up image of your SD card, insert your SD card into a card reader
14. Run this and identify your card reader disk number:

diskutil list

15. Replace “N” with your disk number, and run this:

sudo dd if=/dev/diskN of=~/SDCardBackup.dmg

All done

That’s it! You’re now ready to start working on whatever project you had in mind, secure in the knowledge that if you stuff up, getting back on track is only a matter of minutes, instead the hour or two it might otherwise take.

Recovery

Sooner or later, it’s bound to happen: If you manage to corrupt your disk image or otherwise mess up your installation, follow these instructions:

16. To restore from a backup, insert your SD card into a card reader on your Mac.
17. Run this and identify your card reader disk number:

diskutil list

18. With the last three commands, replace the N with your disk number.
19. Unmount the card with:

diskutil unmountDisk /dev/diskN

20. Write to the card:

sudo dd if=~/SDCardBackup.dmg of=/dev/diskN

21. Eject:

sudo diskutil eject /dev/diskN

If your saved backup image is not brand new, your software is again out of date.

22. Insert your restored-from-backup SD card in your Pi
23. Login either with a keyboard and a display, or via SSH
24. Run software updates:

sudo apt-get update
sudo apt-get upgrade

And now you can resume work on your project!

This entry was posted in IT and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *