Skip to lesson content

Raspberry Pi Setup · Lesson 6 of 17

Auto-connect a Raspberry Pi to a Wi-Fi hotspot

If you move a monitoring Pi between home and a phone hotspot, save both networks on the Pi. On Raspberry Pi OS Bookworm and later, NetworkManager can then choose which one to join at boot. Your phone supplies the hotspot and the Pi joins it as a client.

Save the hotspot on the Pi

Turn on your phone's hotspot. Use the Pi's local terminal or connect over Ethernet so switching Wi-Fi will keep the terminal accessible. On a Pi Zero 2 W, set the hotspot to 2.4 GHz.

Check that systemctl is-active NetworkManager reports active. If you have already joined the hotspot, go straight to the saved profiles below. Otherwise, enter the name exactly as shown on your phone, then enter the hotspot password when prompted.

sudo nmcli --ask device wifi connect

Choose which network wins at boot

List the saved connections. Find the hotspot's Wi-Fi profile and copy its UUID, the identifier in the second column. The profile name can differ from the name broadcast by the phone.

nmcli -f NAME,UUID,TYPE,DEVICE connection show

Replace YOUR-UUID in the commands below with that identifier. This example enables automatic connection and gives the hotspot priority 10. A home network at the default priority of 0 stays available when the phone is away.

sudo nmcli connection modify uuid YOUR-UUID \
    connection.autoconnect yes connection.autoconnect-priority 10

NetworkManager chooses the highest-priority eligible profile when the Wi-Fi device needs a connection. To use the phone as a backup instead, give the home profile a higher priority. Turning on the phone while the Pi already has working Wi-Fi leaves the existing connection active.

Test an automatic connection

  1. Inspect the saved settings. With the values above you should see yes and 10.
    nmcli -f connection.autoconnect,connection.autoconnect-priority \
        connection show uuid YOUR-UUID
  2. When it is convenient to interrupt the Pi, leave the hotspot on and reboot the Pi. After it boots, check which network it chose.
    nmcli device wifi list
    ip -brief address
    A star in the IN-USE column should mark the hotspot. Look for an IP address on the Wi-Fi interface, usually wlan0. If you use SSH, join the same network and use the Pi's new address.

If another network wins, compare its saved priority and check that the hotspot is visible before boot. If the phone stops advertising when idle or the Pi cannot see it, re-enable the hotspot, check its band and test a manual Wi-Fi connection first.

Older installations using wpa_supplicant

Use this method only on an older Raspberry Pi OS installation, such as Bullseye, where dhcpcd and a standalone wpa_supplicant manage Wi-Fi. If NetworkManager manages the interface, use the profile commands above.

For a personal WPA2 hotspot, open the existing file with sudo nano /etc/wpa_supplicant/wpa_supplicant.conf. Keep the country setting and existing networks. Append a block with your hotspot name and password, and choose a priority higher than the other saved network if you want the phone preferred.

network={
    ssid="My Phone"
    psk="replace-with-your-hotspot-password"
    priority=10
}

Save the file and keep it readable only by root because it contains the password. Run the following from the local or wired terminal, substituting your Wi-Fi interface if it has another name.

sudo wpa_cli -i wlan0 reconfigure
sudo wpa_cli -i wlan0 status
ip -brief address show wlan0

Look for OK after the reload, then wpa_state=COMPLETED, the correct ssid and an address. If the reload fails, check which service owns the interface before editing the file again.

Documentation