How can I configure my headless server to connect to a wireless network automatically?
I'm setting up a headless server, but I don't have ethernet access where I want to put it, so I need it to automatically connect to my WiFi network when it boots up (it has a wireless card). I can connect to an ethernet connection to set it up, but I need it to automatically connect to my access point from then on.
How can I configure this?
After doing a lot of research I've gotten it working. Since I have an Intel wireless card I didn't have to install any extra drivers, but you might have to, depending on the card you have.
First you need to figure out what interface your wireless card is using. We use the iwconfig
command for this:
iwconfig
In my case my wireless card is the wlan0
interface, so I will be using that. Now we need to scan for wireless networks:
iwlist wlan0 s
This should give lots of output, showing the details of the various wireless networks in your area. It's usually easier to filter by ESSID. grep
helps us out here:
iwlist wlan0 s | grep ESSID
This will list the names of all the wireless networks in your area. Now it's time to connect to your network.
Unsecure and WEP networks
If your network is unsecured or is secured by the older WEP (time to upgrade your security or router!) connecting is relatively simple. If your network is unsecured you should be able to connect with this:
iwconfig wlan0 essid NAME_OF_NETWORK
If your network is WEP protected just add the key
argument followed by your password, like this:
iwconfig wlan0 essid NAME_OF_NETWORK key PASSWORD
You might have to run
dhclient
To get your router to assign you an IP address.
WPA/WPA2
WPA/WPA2 is a bit more complicated. You will need to use wpa_supplicant
. First create the config file in /etc
:
sudo wpa_passphrase NETWORK_NAME NETWORK_PASSWORD > /etc/network/wpa_supplicant.conf
Now we need to connect:
sudo wpa_supplicant -B -iINTERFACE_NAME -cPATH_TO_CONFIG -DDRIVER
For example:
sudo wpa_supplicant -B -iwlan0 -c/etc/network/wpa_supplicant.conf -Dwext
-B
runs wpa_supplicant
in the background. The wext
driver should work in most cases. To see other drivers run:
wpa_supplicant
For more information on getting connected see How to connect and disconnect to a network manually in terminal?
Get connected on startup
Now we need to edit /etc/network/interfaces
. Open it in your favorite editor (vim
, nano
, etc); you'll need to use sudo
.
Remove everything except:
auto lo
iface lo inet loopback
(the loopback device). Now add:
auto wlan0
iface wlan0 inet dhcp
pre-up <COMMAND>
Where wlan0
is your wireless interface and <COMMAND>
is the command you use to connect to your network (see above). For example if you're network is unsecured you would add:
pre-up iwconfig wlan0 essid some_network_name
If your network is secured with WPA/2 you would add something like this:
pre-up sudo wpa_supplicant -B -iwlan0 -c/etc/network/wpa_supplicant.conf -Dwext
If you're using wpa_supplicant
you should also add:
post-down sudo killall -q wpa_supplicant
Save the file, restart, unplug your ethernet and try running:
sudo apt-get update
If the command completes successfully congratulations! You're online! If the command does not complete successfully please add a comment below.
Examples and Explanations
If your network is unsecured or secured with WEP your /etc/network/interfaces
should look similar to this now:
auto lo
iface lo inet loopback
auto wlan0
iface wlan0 inet dhcp
pre-up iwconfig wlan0 essid some_network_name
# note: if WEP secured you would also have a 'key' argument with your password
If your network is WPA/2 secured your /etc/network/interfaces
should look similar to this:
auto lo
iface lo inet loopback
auto wlan0
iface wlan0 inet dhcp
pre-up sudo wpa_supplicant -B -iwlan0 -c/etc/network/wpa_supplicant.conf -Dwext
post-down sudo killall -q wpa_supplicant
Now an explanation.
-
auto wlan0
: Starts thewlan0
interface automatically. -
iface wlan0 inet dhcp
: Gets us an IP address through DHCP -
pre-up
: Specifies the command(s) to get the connection going. -
post-down
: Specifies the command(s) to be used to clean up after ourselves (if necessary).
Sources:
-
A wireless, headless Linux box
-
How to connect and disconnect to a network manually in terminal?
-
Personal experience and lots of tinkering.
I suggest a static IP address so that you can easily ssh and ftp into the server. Also, you can significantly simplify your file:
auto lo
iface lo inet loopback
auto wlan0
iface wlan0 inet static
address 192.168.1.125
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 192.168.1.1
wpa-ssid <your_network>
wpa-psk <your_key>
Be certain the address is outside the range used by the router for DHCP and, of course, substitute your appropriate details here.