Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  23] [ 0]  / answers: 1 / hits: 24309  / 2 Years ago, wed, february 16, 2022, 8:03:23

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?


More From » wireless

 Answers
0

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:



[#27447] Thursday, February 17, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
intssive

Total Points: 493
Total Questions: 119
Total Answers: 101

Location: Liberia
Member since Mon, Feb 1, 2021
3 Years ago
intssive questions
Tue, Mar 21, 23, 08:19, 1 Year ago
Wed, Aug 31, 22, 04:04, 2 Years ago
Sat, Sep 25, 21, 15:09, 3 Years ago
Fri, Dec 31, 21, 09:13, 2 Years ago
;