Speed up your Raspberry Pi configuration
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.
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.
A small python based initialization tool to kickstart Raspberry Pi configuration. The main aim for this tool is to…
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.
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).
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.
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:
After installing git, we can clone the py-init project from Github.
We can then access the directory py-init and find the file config.json which does look like this:
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.
Here we can add the new hostname we want to give our Raspberry Pi.
We can set the swap size in MB for our Raspberry Pi.
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.
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.
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.
This changes the locale settings from the default “en_gb” to “en_us”.
This will reduce the storage used by the journald service to 50 MB.
This does change the default password from the pi user to the newly defined value.
This removes the password from the pi user and disables SSH login with a password. This setting overwrites the value from “new_user_password”.
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.
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.
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.