Skip to main content

Pi Wi-Fi

View source on GitHub

This guide builds a runtime that brings up the onboard Wi-Fi of a Raspberry Pi 4, Pi 5 or Pi Zero 2 W under systemd-networkd, then gives the device its Wi-Fi credentials in one of two ways:

  1. Over SSH (recommended). A script writes the credentials to /var on the device. They are not in the image or in git. The device needs a network link for SSH, such as Ethernet on a Pi 4 or Pi 5.
  2. In the overlay. You write the credentials into the extension's config file before you build. Use this when the device has no network for SSH, for example a new Pi Zero 2 W with no Ethernet port. The credentials are then built into the extension image, and they are in git if you commit the file.

If both exist, the /var config wins.

Prerequisites​

  • macOS or Linux. On macOS, the Avocado CLI starts its own helper VM (avocado-vm) for the SDK. On Linux, Docker must be installed and running.
  • Avocado CLI 1.1.0 or later. See the Avocado CLI docs.
  • A Raspberry Pi 4, Pi 5 or Pi Zero 2 W, a microSD card (8 GB or larger) and an SD card reader
  • A WPA2-PSK network and its passphrase. The Pi Zero 2 W radio is 2.4 GHz only and cannot see 5 GHz networks.
  • For the SSH method: python3 on your machine, and a network link to the device
  • Optional: a 3.3 V USB-to-UART adapter for the serial console (115200 baud). If the device does not join the network, the serial console is the way in.

Initialize​

The reference defaults to the raspberrypi4 target. To target another board, pass --target raspberrypi5 or --target raspberrypi0-2w:

avocado init --reference pi-wifi --target raspberrypi4 pi-wifi
cd pi-wifi

Install​

avocado install -f

The Pi 4 and Pi 5 use the 2026 release on the edge channel. The Pi Zero 2 W is not in the 2026 feed yet, so a target-raspberrypi0-2w override in avocado.yaml keeps it on 2024.

Option A: credentials over SSH​

Use this method when the device has a network link for SSH, such as Ethernet on a Pi 4 or Pi 5. Run every command in this section on your machine, from the project directory.

Build and deploy​

Leave overlay/etc/wifi-station/wpa_supplicant.conf as it is, then build and provision:

avocado build
avocado provision -r dev --profile sd

Put the card in the board, connect Ethernet and apply power. wifi-station.service does not start yet, because there are no credentials.

To update a device that already runs Avocado OS, use avocado deploy --device root@<device-ip> dev instead.

Connect Ethernet before you deploy. If the device is on Wi-Fi with credentials from the overlay (Option B), this build replaces them with placeholders and the device drops off Wi-Fi. Until you send the credentials below, Ethernet is its only network link.

Find the device and check SSH​

Find the device's Ethernet address in your router's DHCP client list, or run ip -4 addr show eth0 on the serial console. Then check that SSH works:

ssh root@<device-ip> true

Host key changed? A freshly provisioned device has new SSH host keys. If SSH stops with REMOTE HOST IDENTIFICATION HAS CHANGED, and you know that you provisioned this device again, remove the old key and connect once more:

ssh-keygen -R <device-ip>
ssh root@<device-ip> true

Send the credentials​

Copy wifi.txt.example to wifi.txt, readable only by you, and replace its two lines: the SSID on line 1 and the passphrase on line 2. Do not add quotes. A space at the end of a line becomes part of the SSID or the passphrase.

install -m 600 wifi.txt.example wifi.txt
${EDITOR:-vi} wifi.txt

wifi.txt and its editor swap files are in .gitignore. Do not put real values in wifi.txt.example, because it is tracked in git.

Then run the script with the device's address:

scripts/provision-wifi.sh root@<device-ip>

The script prints the SSID it read, derives the 64-hex WPA key on your machine and sends only that key and the SSID to /var/lib/wifi-station/wpa_supplicant.conf on the device. The passphrase never leaves your machine. Expect:

SSID: 'my-network'
root:root 700 /var/lib/wifi-station
root:root 600 /var/lib/wifi-station/wpa_supplicant.conf
network blocks: 1 psk lines of 64 hex: 1 quoted psk lines: 0
Expected: network blocks: 1 psk lines of 64 hex: 1 quoted psk lines: 0
Start it now with: ssh root@<device-ip> systemctl restart wifi-station.service
provision-wifi: exit 0 (input file removed)

The script deletes its input file when it exits, also when it fails. This applies to a file you pass as the second argument too. If the script fails, create the file again before you run it again.

Start Wi-Fi​

ssh root@<device-ip> systemctl restart wifi-station.service

Then check the link as described in Verify. The first line of the check must say using /var/lib/wifi-station/wpa_supplicant.conf.

The credentials in /var survive reboots and later deploys, so you send them once per device. To change the network, run scripts/provision-wifi.sh again and restart the unit. The file is replaced atomically.

What this protects, and what it does not​

  • The passphrase never reaches the device, and the derived key appears in no process argument and not in the journal. The only copy on the device is the 0600 file under /var.
  • Anyone who can read the device's storage can read that file and join the network. Encrypted /var is not enabled in this reference.
  • A shared passphrase across a fleet means one stolen device exposes the network. Use a passphrase per device where you can.
  • The script does not set a country, so the world regulatory domain applies until an access point announces one. Some channels are scanned passively at first.

Option B: credentials in the overlay​

Use this method when the device has no network for SSH.

Set the network​

Edit overlay/etc/wifi-station/wpa_supplicant.conf and replace all three YOUR_ values: country, ssid and psk.

country=US

network={
ssid="my-network"
psk="my-passphrase"
key_mgmt=WPA-PSK
}

country is the two-letter code of the country where the device operates. The passphrase must be 8 to 63 characters.

On the device, this file is readable by every local user, because git does not keep file modes stricter than 0644. Option A stores the key in a file only root can read.

Do not forget country. The country line is above the network block, so it is easy to set the SSID and passphrase and miss it. If any YOUR_ value is left, wifi-station.service does not start and the device does not join the network. The unit's journal (journalctl -u wifi-station.service -b) lists each placeholder that is left:

wifi-station: no credentials. Run scripts/provision-wifi.sh, or replace these placeholders in /etc/wifi-station/wpa_supplicant.conf and rebuild:
country=YOUR_COUNTRY_CODE

To check before you build, run grep -nE '^[[:space:]]*(country=YOUR_COUNTRY_CODE|ssid="YOUR_SSID"|psk="YOUR_PASSPHRASE")[[:space:]]*$' overlay/etc/wifi-station/wpa_supplicant.conf. It must print nothing.

Build and deploy​

avocado build
avocado provision -r dev --profile sd

Put the card in the board and apply power. wifi-station.service starts on boot and joins the network.

To change the network later, edit the file, rebuild and deploy over the current Wi-Fi link with avocado deploy --device root@<device-ip> dev. The extension's on_merge hook restarts wifi-station.service. If the new values are wrong, the device loses its Wi-Fi link. Fix the file, then provision the SD card again, use the serial console, or on a Pi 4 or Pi 5 connect over Ethernet.

Verify​

Find the device's address in your router's DHCP client list, or log in on the serial console as root with an empty password and run networkctl status wlan0. Then check the link over SSH:

ssh root@<device-ip> 'journalctl -u wifi-station.service -b --no-pager | grep "using"; iw dev wlan0 link | grep -E "Connected|SSID|freq"; networkctl status wlan0 --no-pager | grep -E "State|Address|Gateway"; ping -I wlan0 -c 3 1.1.1.1'

Expect wifi-station: using /var/lib/wifi-station/wpa_supplicant.conf (Option A) or using /etc/wifi-station/wpa_supplicant.conf (Option B), Connected to <access point MAC>, your SSID, State: routable (configured), a DHCP address on wlan0, and replies from 1.1.1.1. Ethernet keeps the lower route metric, so it stays the default route when both links are up.

If the device does not join, check the unit on the serial console:

systemctl status wifi-station.service --no-pager -l
journalctl -u wifi-station.service -b --no-pager

systemctl status shows the reason when the unit was skipped.

Reboot the device and run the same check. wifi-station.service starts on its own, and its ExecStartPost (networkctl reload) makes networkd manage wlan0 even though networkd started before the extension was merged.

Customize​

Change the DHCP settings​

Edit overlay/usr/lib/systemd/network/25-wlan0.network. It uses DHCPv4 with route metric 200.

Run the host tests​

tests/test-wifi-station.sh checks how the start script picks a config. tests/test-provision-wifi.sh checks the key derivation in scripts/provision-wifi.sh. Neither needs a device.