Headless wifi for the Pi

I was setting up a headless Raspberry Pi today and was happy to find that you can preload wifi settings onto it, removing the need for any physical interaction with the thing. But there's a trick to it.

Ideally, you should just be able to add a wpa_supplicant.conf file with your network settings to the boot partition, and the Pi will move it into /etc/wpa_supplicant/ for you on boot and have Raspbian automatically connect with those settings.

Some of the most popular answers for this topic provide you with a wpa_supplicant.conf example that contains nothing but the network block. Unfortunately, those examples aren't quite complete for practical usage. As it turns out, there are a couple of magic words that make the difference between having a networked, headless machine and a brick with lights on it. As of November 2017, you need something like this:

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="Your wifi name"
    psk="Your wifi password"
    key_mgmt=WPA-PSK
}

Those first three lines were taken from the default wpa_supplicant.conf found on a fresh image of Raspbian. In my situation, it seems that the country line is not actually needed, though your mileage may vary.

And despite what some people out there on the web may claim, placing the file on the boot partition does work as intended with Raspbian Lite. Remember to also add an ssh file so you can actually mess with it once it's online!