25

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?

2 Answers 2

23

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 the wlan0 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:

9

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.

6
  • 1
    It would be cool if you could add a bit more of an explanation for a new person, so they don't have to read my answer before they understand yours.
    – Seth
    Mar 4, 2014 at 18:13
  • Will your answer work with ONLY the given changes to interfaces file? I mean doesn't that need the wpa_supplicant to be running in background etc etc and all the other things as suggested in Seth's accepted answer? Dec 24, 2016 at 7:04
  • @OverTheEdge It does. Please try it and if not, start a new question and we'll help.
    – chili555
    Dec 24, 2016 at 13:36
  • This causes serious troubles if user has static IP defined for Ethernet port ETH0 within the same /etc/network/interfaces.d
    – Pe Dro
    Jul 13, 2021 at 11:30
  • @PeDro Please start your own new question. This seven year old answer is likely no longer valid.
    – chili555
    Jul 13, 2021 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .