In this tutorial, we are going to learn how to connect to Wi-fi network from the command line on Ubuntu 16.04 server and desktop using wpa_supplicant.
In a modern home wireless network, communications are protected with WPA-PSK (pre-shared key) as opposed to WPA-Enterprise, which is designed for enterprise networks. WPA-PSK is also known as WPA-Personal. wpa_supplicant is an implementation of the WPA supplicant component. A supplicant in wireless LAN is a client software installed on end-user’s computer that needs to be authenticated in order to join a network.
Now let’s see how to connect to WPA2 wireless network using wpa_supplicant.
Step 1: Enable wireless interface
First, make sure your wireless card is enabled. You can use rfkill.
sudo apt install rfkill
To check the status of wireless card, run
rfkill list
Sample output:
As you can see, my wireless card is blocked by software. To unblock it, use the following command:
rfkill unblock wifi
If you are using the desktop version of Ubuntu, then you also need to stop Network Manager with the following command, otherwise it will cause connection problem when using wpa_supplicant.
sudo systemctl stop NetworkManager
To disable NeworkManager start at boot time, run
sudo systemctl disable NetworkManager
Step 2: Find your wireless interface name and wireless network name
Run iwconfig
to find the name of your wireless interface.
iwconfig
wlan0
is a common name, but my wireless interface is called wlp3s0
. You can also see that it’s not associated with any access point right now.
Then find your wireless network name by scanning nearby networks with the command below. Replace wlp3s0 with your own interface name. ESSID means network name.
sudo iwlist wlp3s0 scan | grep ESSID
Step 3: Connect to Wi-fi network using wpa_supplicant
Now install wpa_supplicant on Ubuntu 16.04.
sudo apt install wpasupplicant
We need to create a file named wpa_supplicant.conf
using the wpa_passphrase
utility. wpa_supplicant.conf
is the configuration file describing all networks that the user wants the computer to connect to. Run the following command to create this file. Replace ESSID and Wi-fi passphrase with your own.
wpa_passphrase Your-ESSID passphrase | sudo tee /etc/wpa_supplicant.conf
The output will be piped to tee
which then write to /etc/wpa_supplicant.conf
file.
Now use the following command to connect your wireless card to wireless access point.
sudo wpa_supplicant -c /etc/wpa_supplicant.conf -i wlp3s0
By default, wpa_supplicant runs in the foreground. If the connection is completed, then open up another terminal window and run
iwconfig
You can see that the wireless interface is now associated with an access point.
You can press CTRL+C
to stop the current wpa_supplicant process and run it in the background by adding -B
option.
sudo wpa_supplicant -B -c /etc/wpa_supplicant.conf -i wlp3s0
Although we’re authenticated and connected to wireless network, but we don’t have an IP address yet. To obtain a private IP address from DHCP server, use the following command:
sudo dhclient wlp3s0
Now your wireless interface has a private IP address, which can be shown with:
ifconfig wlp3s0
Now you can access the Internet. To release the private IP address, run
sudo dhclient wlp3s0 -r
Connecting to Hidden Wireless Network
If your wireless doesn’t broadcast SSID, then you need to add the following line in /etc/wpa_supplicant.conf
file.
scan_ssid=1
Like below:
network=
ssid="LinuxBabe.Com office network"
#psk="12345qwert"
psk=68add4c5fee7dc3d0dac810f89b805d6d147c01e281f07f475a3e0195
scan_ssid=1
Auto Connect on Startup
To automatically connect to wireless network at boot time, edit the wpa_supplicant.service
file.
sudo nano /lib/systemd/system/wpa_supplicant.service
Find the following line.
ExecStart=/sbin/wpa_supplicant -u -s -O /run/wpa_supplicant
Change it to the following. Obviously you need to change wlp3s0
if that isn’t your interface name.
ExecStart=/sbin/wpa_supplicant -u -s -c /etc/wpa_supplicant.conf -i wlp3s0
Save and close the file. Then enable wpa_supplicant service to start at boot time.
sudo systemclt enable wpa_supplicant.service
Also we need to add the following line in /etc/rc.local file to obtain a private IP address at boot time.
dhclient wlp3s0
This line should be put above exit 0 like below.
#!/bin/sh -e
.......
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
dhclient wlp3s0
exit 0
That’s it!
I hope this tutorial helped you set up Ubuntu server 16.04 wifi from the command line. As always, if you found this post useful, then subscribe to our free newsletter. You can also follow us on Google+, Twitter or like our Facebook page.
Rate this tutorial
Source link
0 comments:
Post a Comment