How to easily connect your headless Debian-based box to WiFi
Setting up WiFi on a headless Linux machine can be a pain in the ass.
Most guides you’ll find online will tell you to insert your network information into wpa_supplicant.conf and execute wpa_supplicant with the proper arguments. This is annoying especially for newbies who first have to find a suitable template, figure out which driver to specify and so on. Additionally, if the adapter disconnects from the access point for whatever reason, with this method it won’t get reconnected automatically.
A much easier way of connecting your CLI only machines to WiFi is via /etc/network/interfaces. This guide will show you how to do it.
This guide is for WPA2-PSK only, because that’s what you should use for your personal wireless network.
This tutorial uses the following sample data for illustration purposes:
WiFi password: &/(§"$23823asd§$a!0dfAe
WiFi netmask: /24 (255.255.255.0)
Wireless interface: wlan0
Desired, static IP address: 192.168.2.113
Change these values to match your actual data & needs.
Obtaining the Pre-Shared-Key
First you have to create a 256-bit PSK derived from the the WiFi network name (ESSID) and the plain password. You’ll want to do this to avoid saving the plain text password into the configuration file later. Execute the program wpa_passphrase along with your ESSID as argument.
$ wpa_passphrase test-wlan
You will be asked to enter your password for the network. Do this and press enter.
Some data will be created and presented to you. However, we only need the alphanumeric part after “psk” (here: “3af...”), this is the hash. Copy this somewhere.
network={
ssid="test-wlan"
#psk="&/(§"$23823asd§$a!0dfAe"
psk=3af27aac304d4fbdd22853f3be966fa5942992b701c40d0c8acf34356ee2acb9
}
Now open the file /etc/network/interfaces with root privileges using your favorite text editor.
$ sudo vim /etc/network/interfaces
The following snippet configures our adapter wlan0. Values required for authentication are wpa-ssid & wpa-psk. Those are your ESSID, wrapped within quotation marks and the hash you created previously, written without quotation marks. Change the values to your needs and save.
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.2.113
gateway 192.168.2.1
netmask 255.255.255.0
network 192.168.2.0
broadcast 192.168.2.255
wpa-ssid "test-wlan"
wpa-psk 3af27aac304d4fbdd22853f3be966fa5942992b701c40d0c8acf34356ee2acb9
Either reboot the device or restart the networking service to apply the configuration changes.
Your WiFi adapter should now connect automatically to your network at system start. If the device gets disconnected, it will reconnect as soon as a connection is possible again.