CodeX
Published in

CodeX

Speed up your Raspberry Pi configuration

Photo by Harrison Broadbent on Unsplash

TL;DR

This blogpost describes how to speed up your Raspberry Pi project with an easy and automated configuration tool.

The foundation for many DYI projects

Before the Raspberry Pi 4, every time I wanted to try something on a Linux machine I created a virtual machine on my small Intel NUC. It was easy, the setup was fast and it was disposable once I was done with it. Still, maintaining the ESXi started to bother me. Since I didn’t really use it for critical applications in production, I postponed updating it and every time I wanted to try something new, I first had to fix old stuff. This is one reason why I started to run everything I could as a Docker image. It was so much easier and updating of the Docker images could be easily automated using watchtower. This way I could find peace of mind for running necessary applications without having too much to bother. Even though my own software can be easily put into Docker images that can run on ARM, this is a totally different story, I’m not going to tell today. What Docker images didn’t solve for me was the need for fast installed and disposable Linux machines. The ones you use if you quickly want to install something or mess around with critical system files. The ones you just leave running, because you couldn’t finish your tests, and still don’t want to worry about the high energy consumption of a normal computer, or about VMs eating up all the disk space of your notebook and making your fan turn like a jet engine. Raspberry Pis are a good alternative for this use case. Flashing the image to a microSD card is very fast, and with the introduction of the Raspberry Pi 4, you can even choose to have up to 8 GB of RAM.

Repetitive steps

When I first set up a Raspberry Pi years back, it took me quite a while to find the right way to configure everything I wanted. With some repetition, I got faster and exactly knew where to a certain configuration. After a few projects and setups, I wondered whether there isn’t any better way to do this. For me, it was clear that I didn’t want to use a solution that requires any kind of infrastructure to host configurations or control services. It should be as easy as editing a single config file and run a script. After I haven’t found anything that satisfied my needs I did write my own python script to kickstart the Raspberry Pi configuration and this was how py-init was born.

The Setup

I will give you a short introduction on how py-init can be used to speed up your Raspberry Pi configuration. Please be aware that py-init works on Raspberry Pi OS (formerly Raspbian) and other Debian operating systems. It might not work for Ubuntu and other OS.

Flashing the image

Every single project of mine started with flashing the microSD card using Raspberry Pi Imager.

Raspberry Pi Imager — Choose “Raspberry Pi OS (other)”

Since I rarely use my Raspberry Pis with a screen, I install the Raspberry Pi OS Lite (it uses only a little storage and flashing is therefore pretty fast).

Raspberry Pi Imager — Choose “Raspberry Pi OS Lite (32-bit)”

If you need a 64-Bit OS, you can use the beta provided by Raspberry Pi.

Automatic initial configuration using Raspberry Pi Imager

For an easier initial configuration, Raspberry Pi Imager newly provides new functionality to set, the hostname, activate SSH, set the authorized_keys, configure WiFi, and set the locale. This feature was introduced in early 2021 and already tackles some of the issues I wanted to resolve with the py-init tool. Using the shortcut ctrl+shift+x, the advanced options menu opens. After adding all the settings we just flash the microSD card. After this step, we can continue with “Start and connect via SSH”. Be aware that if you already set the SSH public key via the advanced options, there is no need to do this step manually again.

Raspberry Pi Imager, new advanced options menu

Manual Initial Configuration

If in any case, we cannot use the advanced options in the Raspberry Pi Imager, we just continue with flashing the microSD card and to the initial configuration manually.

Once the image is written to the microSD card, we can decide to set up headless WiFi settings and headless SSH. In order to do so, we have to reattach the microSD card and create the files mentioned in the next two sections in the “/boot” directory.

WiFi settings (manual step)

The WiFi settings can be configured now or afterward using py-init. If we want to configure a Raspberry Pi Zero w, we need to configure the WiFi settings in order to access it via SSH over WiFi. To configure the WiFi we have to add the file “wpa_supplicant.conf” to the “/boot” directory.

It is important to ensure that we select the correct country since WiFi settings will be configured according to it. You can find the correct Alpha-2 code here. Also, make sure to change the SSID (WiFi-name) and PSK (WiFi-Password) according to your WiFi configuration.

Headless SSH access (manual step)

To automatically activate the SSH server, we also create a second file under “/boot/ssh”. This file doesn’t need any content. Its purpose is to tell the pi to activate the SSH server service.

Start and connect via SSH

Once we have created these two files our Pi is ready to start. The next step for us is to figure out the IP address of the Raspberry Pi. This can be accomplished using an IP-Scanner such as fing which can be used on Android, iOS, or on the Desktop. We then connect to the Pi via SSH using the default credentials “pi” (username) and “raspberry” (password). For easier access, we can place our SSH public key into the “~/.ssh/authorized_keys” file.

Clone the py-init repository

To clone the py-init project via git, we first nid to make sure that git is installed:

install git

After installing git, we can clone the py-init project from Github.

git clone py-init project

We can then access the directory py-init and find the file config.json which does look like this:

py-init config.json

This config.json file is used to configure all kinds of settings on our Raspberry Pi. If we don’t need a certain configuration, we can just delete it from the config.json and it will be omitted by the configuration script. To further clarify I will explain what is going to be configured using which parameter.

hostname

Here we can add the new hostname we want to give our Raspberry Pi.

swap_mb

We can set the swap size in MB for our Raspberry Pi.

expand_fs

Usually, Raspberry Pi OS does auto-expand the filesystem on its first boot, if it didn’t happen we can trigger the command to do so again.

wifi_settings

This does accomplish the same thing we did with our wpa_supplicant.conf file earlier in the post. Please ensure to select the correct country since WiFi settings will be configured according to it. You can find the correct Alpha-2 code here.

network_config

We can change the network configuration such as IP address, default gateway and, DNS servers for the different interfaces. The template contains a list of settings, one for the wired “eth0” interface and one for the WiFi “wlan0” interface. If we only want to configure one of them, we can just delete the entry for the other one.

set_locale_en_us

This changes the locale settings from the default “en_gb” to “en_us”.

reduce_journald_size

This will reduce the storage used by the journald service to 50 MB.

new_user_password

This does change the default password from the pi user to the newly defined value.

disable_password_login

This removes the password from the pi user and disables SSH login with a password. This setting overwrites the value from “new_user_password”.

update_packages

This updates the packages installed on the Raspberry Pi after the first reboot. In order to do so, it adds a script to “/etc/rc.local” that will be removed again, once it finishes installing the updates.

Trigger the configuration

After editing the config.json to our needs we need to trigger the configuration script and wait until it finishes.

trigger the configuration
output of py-init

Depending on the settings we’ve selected, we need to restart our Raspberry Pi for the settings to take action. If we selected to update the packages, our Raspberry Pi will reboot and start updating them. This can take a while and I recommend not to power down the Raspberry Pi while the update is running.

Last Words

I really hope that py-init can help you, as much as it helped me to speed up the configuration of my Raspberry Pi when I start a new project. Let me know your thoughts and if you think there is anything missing. If you liked this blog post, make sure to follow me and support my writing. Thanks for reading.

About the author

Remo Höppli is Co-Founder and Software Engineer at Earlybyte.

Earlybyte is an IT consultancy firm specialized in developing new digital solutions for companies around the world from digitalization to IoT solutions, close to the client and its business embracing agility.

Follow me on Twitter to get informed on new blog posts.

--

--

--

Everything connected with Tech & Code. Follow to join our 900K+ monthly readers

Recommended from Medium

How to Let Your AI See

Flash Stock Rom on Xolo Q1010i

11111

Use Kali Linux tools in Ubuntu and other Debian based OS in a convenient way.

Download Browser Web

Help your Colleagues Save Time by Using NUnit Attributes in your Tests

HTTP caching with hazelcast, and other tricks how to make my angular app load faster.

Deploy Java SpringBoot App on Azure App Service Container

How to Install tmux on Linux

How to Install tmux on Linux

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Remo Hoeppli

Remo Hoeppli

Remo Höppli is co-founder and software engineer at Earlybyte. He is a technology enthusiast and minimalism advocate striving for simplicity and efficiency.

More from Medium

Installing Gnome Extensions on Ubuntu 22.04 Jammy Jellyfish

Raspberry-Pi: A web server with CI/CD pipeline (Part-2)

Docker image

Build a User-Friendly CLI from Pure Python Functions

How I switched to Ubuntu after using Windows for 20 years (the 2022 guide)