Prepare a headless Raspberry Pi from scratch

Our step-by-step guide to prepare a lean yet versatile SD card image based on Raspberry Pi OS Lite.

Prepare a reelyActive SD card image for Raspberry Pi

The TL;DR (Too Long; Didn't Read)

Learn how we at reelyActive prepare a SD card image for a headless Pi.


What will this accomplish?
A clean, lean installation of the latest Raspberry Pi OS Lite build ready to run Node.js programs like our Pareto Anywhere middleware.
Is there an easier way?
Yes, you can simply flash a ready-made disk image to a SD card .
So why would I read this?
To understand and follow the process to create your own SD card image.

Flashing the OS to a SD Card   Step 1 of 5

Download the latest version of Raspberry Pi OS Lite and flash it to an SD card with Etcher.


Why Raspberry Pi OS?
As the default operating system for the Pi (formerly called Raspbian), it is accessible to the broadest audience.
Why Lite?
It includes all the key ingredients for headless operation while keeping the disk image small.
Flashing Raspbian to SD card using Etcher

Prerequisites

We'll flash the SD card with Etcher, a friendly open source tool that you can download here.

Download and unzip the latest Raspberry Pi OS Lite Part 1

From the computer with which you intend to flash the SD card :

  1. Browse to www.raspberrypi.com/software/operating-systems/
  2. Scroll down to find the Lite version
  3. Download the ZIP or Torrent, as you prefer
  4. Unzip the downloaded file

You should now have a file entitled 20xx-xx-xx-raspios-bullseye-armhf-lite.img on your computer . Let's flash!

Flash the Raspberry Pi OS image to the SD card Part 2

We recommend using micro SD cards from reputable vendors of at least 4GB size.

From the computer with which you intend to flash the SD card :

  1. Insert the SD card (via adapter if required) and observe that it is recognised/mounted by the operating system
  2. Run Etcher (see Prerequisites above) by double-clicking its executable AppImage or desktop icon
  3. In the Etcher interface, select the 20xx-xx-xx-raspios-bullseye-armhf-lite.img file
  4. In the Etcher interface, confirm that the SD card is automatically selected, or select manually
  5. In the Etcher interface, click Flash!     4 mins

Proceed to the next step once the SD card is flashed and verified.

Pre-configuring the OS   Step 2 of 5

Configure Raspberry Pi OS before the first boot on the Pi, ensuring a painless initial connection.


Why more pre-config?
Unless you have a monitor and keyboard for the Pi, good luck with the first connection otherwise!
Why so difficult to connect?
By default SSH is disabled (for security) so even if you can reach the Pi over a network, it will refuse to connect!
Predictable network names and eth0 fallback

Following the flashing of the SD card in the previous step, the computer should automatically mount two partitions: boot and rootfs. We'll make minor changes to each partition as follows.

If the partitions aren't automatically mounted, eject the SD card and then re-insert into the computer .

Enable SSH and predictable network interface names Part 1

Open a terminal and browse to the root of the bootfs partition (ex: cd /media/user/bootfs/), then:

  1. Enable SSH with the command sudo touch ./ssh
  2. Open the file cmdline.txt and append the following to the first line: net.ifnames=0

This will allow us to SSH into the Pi on the first boot, and will use predictable network interface names (ex: eth0, wlan0) which we'll need for Part 3.

Explicitly set a username and password Part 2

As of April 2022, Raspberry Pi OS no longer ships with a default username & password!

From the same terminal in the root of the bootfs partition (ex: /media/user/bootfs/):

  1. Create and open for editing a file called userconf (for example with the command sudo nano userconf)
  2. Paste the following line into the userconf file and save
pi:$6$XW.DLlBAlSF1wtdM$qpqLFBieq0toz9JviJm4.RvBjGN4yaxtrpcXMn8XdDa48.w7Ji6GeP.BWyhfae0NY074GmVMItEy9kNPdPaSz.

This will set the default username to pi with password berryinsecure (encrypted above), which will be required when SSHing into the Pi in Step 3—and you can change it to a less-insecure password in that step too!

Fallback to static Ethernet IP address Part 3

Open a terminal and browse to the root of the rootfs partition (ex: cd /media/user/rootfs/). Edit the /etc/dhcpcd.conf file to include the following (simply append or uncomment and edit):

# It is possible to fall back to a static IP if DHCP fails:
# define static profile
profile static_eth0
static ip_address=10.0.50.100/24
static routers=10.0.50.1
static domain_name_servers=10.0.50.1

# fallback to static profile on eth0
interface eth0
fallback static_eth0

This will ensure that the Pi boots with the static Ethernet address of 10.0.50.100 in the absence of DHCP, as will be the case, and will prove helpful on any occasion where it is necessary to directly connect to the Pi over Ethernet, as in the following step.

Unmount both partitions and eject the SD card which is now ready to find its way into the Pi.

First boot and raspi-config   Step 3 of 5

Boot the Pi for the first time and SSH in via Ethernet to run raspi-config.


Why connect over Ethernet?
Because we know the Pi's static IP address, making this the easiest way to connect at first.
Why use SSH?
Unless you have a display and keyboard connected to the Pi, the only way to remotely execute commands is over SSH!
raspi-config

The SD card should only be inserted into (or removed from) the Pi when it is disconnected from power.

Boot the Pi and connect via SSH over Ethernet Part 1

With the SD card inserted into the Pi, complete the following:

  1. Apply power to the Pi
  2. Connect the computer with the Pi using a network (Ethernet) cable
  3. Set the computer to use the static Ethernet IP address 10.0.50.101
  4. Open a terminal and SSH into the Pi with the command ssh pi@10.0.50.100 and, when prompted, enter the password berryinsecure (before April 2022, the default password was raspberry)

Now that you're logged in to the Pi, it is possible to execute commands such as raspi-config.

Complete the basic configuration with the raspi-config tool Part 2

From the command prompt on the Pi , enter the command sudo raspi-config which will open a text-based menu.

raspi-config menu

Update the configuration as per the following table where the settings are indicated in [ ].

# Option Action(s)
1 System Options S1 Wireless LAN
  • Set country* [CA]
  • Please enter SSID* [reelyActive]
  • Please enter passphrase* [owl-in-one]

*adjust as per your region and local WiFi network

1 System Options

S3 Password*   [berryinsecure]

*this is a good time to choose something less insecure

3 Interface Options I6 Serial Port
  • Would you like a login shell to be accessible over serial? [No]
  • Would you like the serial port hardware to be enabled? [Yes]
5 Localisation Options

L2 Change Timezone*   [America | Montreal]

*adjust as per your region (reelyActive is "Made in Montréal")

6 Advanced Options A1 Expand Filesystem [Select]

Upon completing the above, select <Finish> and select <Yes> when prompted to reboot. If you entered valid WiFi settings, the Pi should connect to this network automatically, which will be required for the next step.

Second boot and DHCP server config   Step 4 of 5

SSH into the Pi over WiFi and configure its Ethernet DHCP server to facilitate future direct connections.


Why reconnect over WiFi?
The Pi will require an Internet connection in subsequent steps. A local area network (LAN) connection, such as over WiFi, affords both a local SSH login and Internet access for the Pi.
Why a DHCP server?
Configuring a static IP address on a computer can be a pain (remember Step 3?). A DHCP server enables the Pi to automatically assign an IP address to any computer with which it is directly connected.
Raspberry Pi DHCP either way

The Pi should have rebooted from the previous step and connected to both the local WiFi network and again to the computer connected directly via Ethernet .

SSH to the Pi over WiFi after determining its IP address Part 1

With the Pi still connected to the computer via Ethernet , and from the same terminal window as before:

  1. SSH back into the Pi with the command ssh pi@10.0.50.100, using the new password (or berryinsecure if you didn't change it)
  2. Determine the Pi's WiFi IP address with the command ifconfig. It will be the inet value under wlan0 (ex: 192.168.0.123)
  3. Exit the current SSH session with the command exit
  4. SSH into the Pi over WiFi with the command ssh pi@xxx.xxx.xxx.xxx, replacing the x values with the Pi's WiFi IP address.
  5. Disconnect the network cable between the Pi and the computer .

The computer is now connected to the Pi over WiFi .

Be sure to disconnect the network cable between the two, else they may ignore their Internet connection over WiFi, instead selecting Ethernet as their default gateway.

Install and configure the DHCP server using dnsmasq Part 2

From the same terminal connected to the Pi via SSH over WiFi :

  1. Install dnsmasq with the command sudo apt-get install dnsmasq
  2. Save a copy of the default configuration with the command sudo mv /etc/dnsmasq.conf /etc/dnsmasq.default
  3. Create the file /etc/dnsmasq.conf with the command sudo nano /etc/dnsmasq.conf then paste in the following two lines and save:
    interface=eth0
    dhcp-range=10.0.50.2,10.0.50.99,255.255.255.0,12h
  4. Restart the service with the command sudo service dnsmasq restart

The Pi will now behave as follows on Ethernet connection:

External DHCP? Behaviour of the Raspberry Pi
Yes The Pi will accept the DHCP-assigned IP address
No The Pi will fallback to static IP address 10.0.50.100 and it will assign IP addresses in the range 10.0.50.2-99 to any connected devices

The core configuration of the Pi is now complete. On to the software!

Install Node.js, npm and git   Step 5 of 5

Install all the prerequisites to run Node.js programs such as our Pareto Anywhere middleware.


Why install Node.js?
Our open source Pareto Anywhere middleware runs on Node.js.
Why install git?
To be able to clone GitHub repositories like that of Pareto Anywhere and other open source projects.
Node.js, npm and git

Download and install Node.js and npm Part 1

To simplify installing and updating Node.js, the NodeSource binary distributions will be used. From the same terminal connected to the Pi via SSH over WiFi :

  1. Create a folder for the NodeSource GPG key with the command sudo mkdir -p /etc/apt/keyrings
  2. Import the NodeSource GPG key with the command curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
  3. Specify the version of Node.js (ex: 20*) to use with the command NODE_MAJOR=20
  4. Create the .deb repository with the command echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
  5. Update and install Node.js with the command sudo apt-get update && sudo apt-get install -y nodejs     1 min

Confirm that node and npm are successfully installed with the commands node --version and npm --version respectively.

*it is recommended to specify the current LTS version as indicated on nodejs.org

Update/upgrade all packages and install git Part 2

Since the latest release of Raspberry Pi OS Lite, there are inevitably newer versions of some of the packages available. From the same terminal connected to the Pi via SSH over WiFi :

  1. Update the list of available packages and their versions with the command sudo apt-get update     1 min
  2. Upgrade existing packages to the latest version with the command sudo apt-get upgrade     5 mins+
  3. Install git with the command sudo apt-get install git

All software packages are now installed and at the latest version. The Pi is prepared for the (optional) installation of Pareto Anywhere, which is the subject of the next tutorial:

Tutorial prepared with by jeffyactive.

You can reelyActive's open source efforts directly by contributing code & docs, collectively by sharing across your network, and commercially through our packages.

Where to next?

Continue exploring our open architecture and all its applications.