Setup WiFi

Configure the Interface

Add the following to /etc/network/interfaces:

allow-hotplug wlan0
iface wlan0 inet manual
  wpa-roam /etc/wpa_supplicant/wlan0.conf

WPA Supplicant

Add the following to /etc/wpa_supplicant/wlan0.conf:

ctrl_interface=DIR=/run/wpa_supplicant GROUP=netdev
update_config=1

Fixup the permissions:

chown root:root /etc/wpa_supplicant/wlan0.conf
chmod 600 /etc/wpa_supplicant/wlan0.conf

Configure WiFi Credentials

Run the following snippet once for each access point:

(
    read -p "interface: " IFACE;
    read -p "ssid: " SSID;
    read -p "passphrase: " PASSPHRASE;
    read -p "name: " NAME;
    wpa_passphrase "${SSID}" "${PASSPHRASE}" | sed -re '/ssid="([^"]+)"/i \\tid_str="'${IFACE}_${NAME,,}'"' | tee -a "/etc/wpa_supplicant/${IFACE}.conf";
    echo "iface ${IFACE}_${NAME,,} inet dhcp" | tee -a "/etc/network/interfaces";
)