October 17, 2023

Raspberry headless install with WiFi

Updated 2023-10-17 !

To make a headless, without monitor, keyboard and mouse, install of Raspberry Pi OS on a Raspberry Pi(RPi) with built in WiFi and no LAN cable connection, please follow the instructions below.

For example the RPi Zero and Raspberry Pi 3 Model A+ don't have any LAN cable port so the setup is quite usable.

To make an active WiFi connection at first boot You have to use the "Advanced options" in Raspberry Pi Imager or create, old way, a configuration file called "wpa_supplicant.conf".

Install RPi OS
Please use the instructions in this post where you also add information for the WiFi in the RPi Imager app.

If you want to know more about networking for Bookworm and coming RPi OS use this link.

Please note that the info below is just valid for RPi OS before Bookworm. 

WiFi configuration
If you didn't use "Advanced options" in Raspberry Pi Imager please read on. The "wpa_supplicant.conf" shall contain the following information

country=SE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
    ssid="WiFi Network name"
    psk="WiFi Network password"
}

Please note that You must use a plain editor that don't add "garbage" to the file and don't add any blank rows. If You use a simple editor it probably adds the .txt extension, which don't work.
  • Check Your country code here and change accordingly.
  • Change the credentials matching Your WiFi network.
When the file is created copy it to the same place as the earlier mentioned ssh file.

Then go back to the former post and continue with the headless install.

Additional Headless/WiFi information on the RPi documentation site.

More than 1 WiFi ?
It could be that you are moving the RPi between sites, with different SSID:s, and don't want to edit the configuration every time ? 

country=SE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
    ssid="WiFi Network name"
    psk="WiFi Network password"
}

network={

        ssid="WiFi Network name 2"

        psk="WiFi Network password 2"

        priority=1

}


In this example 2 WiFi:s are set up and if we are at places where only one of the two WiFi:s are active it will work.

But if both the networks are active at the same place we could decide with the "priority=1" parameter which one it shall connect to.

A network without  the parameter "priority" set will always have priority 0(zero) and the higher priority number the higher priority. 

So in this example the "WiFi Network name 2" will be the first to connect to

Additional WiFi commands
Edit the config file afterwords ? 

$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf


or if the RPi don't boot.

Checking what WiFi:s the RPi detects

$ sudo iwlist wlan0 scan

Checking, after a reboot, that the new setup works

$ ip a


What WiFi is the RPi connected to ?

$ iwconfig


The "wpa_cli"  command
Which WiFi:s are configured ??

$ wpa_cli list_networks wlan0
Selected interface 'p2p-dev-wlan0'
network id / ssid / bssid / flags
0 WiFi_1 any [CURRENT]
1 WiFi_2 any [DISABLED]

With information from the above command you now can, without reboot, swap to the other WiFi. With command 

wpa_cli -i wlan0 select_network 1

you will move the RPi to "WiFi_2".

No comments:

Post a Comment

Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !