6

From the docs:

Previous versions of Raspberry Pi OS made use of a wpa_supplicant.conf file which could be placed into the boot folder to configure wireless network settings. This is no longer possible from Raspberry Pi OS Bookworm onwards.

I'd like to know if there is an alternative method that does not need a GUI application to correctly configure WiFi on the Raspberry Pi OS image.

Question: How can I configure WiFi on an Raspberry Pi headlessly without using the Raspberry Pi Imager in current Raspberry Pi OS images?

MatsK
  • 2,882
  • 3
  • 17
  • 22
Naiky
  • 61
  • 1
  • 1
  • 3

4 Answers4

4

With Raspberry Pi OS bookworm you can configure WiFi via a custom.toml that you place in the bootfs partition (first boot only).

Note that this feature is experimental and will be replaced by cloud-init in the future.

An example gist and blog explains some of the options:

I am copying the example from the aforementioned gist (not affiliated):

# Required:
config_version = 1

[system] hostname = "raspberrypi"

[user]

If present, the default "rpi" user gets renamed to this "name"

name = "rpi"

The password can be encrypted or plain. To encrypt, we can use "openssl passwd -5 raspberry"

password = "$5$pN7oRnie.WDOHoJY$aWEYmKUytN/S/bxMza5ksBiurbSJmcvcysBKHSmYa45" password_encrypted = true

[ssh]

ssh_import_id = "gh:user" # import public keys from github

enabled = true password_authentication = false

We can also seed the ssh public keys configured for the default user:

authorized_keys = [ "ssh-rsa ... user@host", ... ]

[wlan] ssid = "mywifi" password = "$5$pN7oRnie.WDOHoJY$aWEYmKUytN/S/bxMza5ksBiurbSJmcvcysBKHSmYa45" password_encrypted = true hidden = false

The country is written to /etc/default/crda

Reference: https://wireless.wiki.kernel.org/en/developers/Regulatory

country = "IE"

[locale] keymap = "gb" timezone = "Europe/London"

You could dig more into the firstboot or init_config scripts (the latter is what loads the config).

de1
  • 141
  • 1
3

I'm sharing my steps, which helped me to correctly configure Wifi connection profile for headless RPI installation with Bookworm OS. This was tested on Raspberry Pi ZERO.

  1. download and install Raspberry PI Imager (I'm using v1.8.1, on Ubuntu)

  2. in the Imager GUI, do not select RPI device in the first step. Currently, Pi ZERO is not on the supported list in RPI Imager for Bookworm. This means, you will not be able to flash Bookworm OS, when you select filter for PI ZERO device at first step.

  3. select Raspberry PI OS Lite (32bit Bookworm), if you want to do headless installation

  4. choose your microSD card, the target device for flashing

  5. click next and you will be able to set some settings, like system username and password, hostname, wifi credentials, timezone etc. Configure this settings, without Wifi connection settings. This do no work for Bookworm at this time (december 2023)

  6. Once the card is ready, remove microSD card from the computer and connect it again

  7. If you are using for example Ubuntu, the 2 partitions will be auto mounted - bootfs and rootfs . In this steps we expect, that partitions are mounted as /media/martin/bootfs and /media/martin/rootfs. Change it in the following steps according your needs.

  8. we will activate ssh connection

    $ sudo touch /media/martin/bootfs/ssh

  9. this step I'm not sure if is needed, give it a try

    $ sudo vim /media/martin/rootfs/boot/firmware/firstrun.sh

Write following content: (change do_wifi_country to your country ISO code)

#!/bin/bash
# Stripped back version of the Bookworm firstrun.sh generated by the Raspberry Pi Imager
# also calls raspi-config to set Wifi country, which in turn calls iw on Bookworm
set +e
rfkill unblock wifi
for filename in /var/lib/systemd/rfkill/*:wlan ; do
    echo 0 > $filename
done
raspi-config nonint do_wifi_country SK
# edited to remove a bug in rpi-imager that is a hangover from Bullseye
# rm -f /boot/firstrun.sh
rm -f /boot/firmware/firstrun.sh
exit 0
  1. generate PSK for your wifi connection settings:

    $ wpa_passphrase networknameSSID yourWifipassword

  2. insert the wifi connection profile Note: Name the file according your wifi SSID

$ sudo vim /media/martin/rootfs/etc/NetworkManager/system-connections/Matho-Wifi.nmconnection

[connection]
id=Matho-Wifi
uuid=710ace81-db6e-47d9-adca-2835818c7b4b
type=wifi
interface-name=wlan0
autoconnect=true

[wifi] mode=infrastructure ssid=Matho-Wifi

[wifi-security] auth-alg=open key-mgmt=wpa-psk psk=YOUR-PSK-HERE

[ipv4] method=auto

[ipv6] method=auto

use the following content:

  • replace Matho-Wifi with your SSID name
  • you can keep the uuid the same. If you change it, the syntax need to meet this length and format
  • set psk to the value, you have generated in previous step 10
  1. Apply chmod 600 and chown the profile

    $ sudo chmod -R 600 /media/martin/rootfs/etc/NetworkManager/system-connections/Matho-Wifi.nmconnection

    $ sudo chown -R root:root /media/martin/rootfs/etc/NetworkManager/system-connections/Matho-Wifi.nmconnection

    $ sudo ls -la /media/martin/rootfs/etc/NetworkManager/system-connections/Matho-Wifi.nmconnection

  2. unmount the partitions, for example via Nautilus GUI

  3. insert the microSD card to your RPI ZERO. Give it some time, on my end it took ~6 minutes to be fully booted

  4. connect via ssh with the username and password you have set via RPI Imager tool

Matho
  • 31
  • 1
2

Bookworm now uses NetworkManager for networking.

If you preconfigure during the imaging process you can set the hostname, user/password, Locale, wireless networking and enable ssh.

For some time Raspberry Pi OS has required that a user be generated on first boot - Raspberry Pi Imager can also do this. This was explained in https://www.raspberrypi.com/news/raspberry-pi-bullseye-update-april-2022/

Raspberry Pi Imager modifies cmdline.txt to run a script on boot and writes a customised startup script firstrun.sh which sets whatever is preconfigured.

These files are deleted on first boot.

Milliways
  • 62,573
  • 32
  • 113
  • 225
0

This is not good. I work on headless Raspberry Pi in 2 locations and had to use wpa_supplicant to change my wifi settings. If it not your firstboot is there a way to change the settings?

CrabbyPete
  • 121
  • 4