Connecting to a Wireless Network on Linux
In this article you will learn how to connect to a wireless network on GNU/Linux through the command-line interface.
| Prerequisites: | Basic familiarity with the linux command-line and text editors. |
|---|---|
| Learning Outcomes: |
|
Motivation
Recently, I have stopped relying on the network manager provided by the Linux desktop environment because it was failing sometimes. This signaled me to find an alternate approach to connect to the wireless network.
On Linux knowing command-line tools is more important than knowing your way around the Graphical User Interface (GUI). It's on times like this when the linux desktop manager fails you that you can leverage command-line tools to connect to the wireless network. Talk about empowering the users of a platform, this is one of the reasons I have been using linux for over a decade and will continue to do so.
One last thing, this is a practical guide that aims to walk you through the process of connecting to a wireless network from the command-line interface. There's much to learn about linux and so my recommendation is to read the man pages of the commands that caught your attention. This blog does not cover everything that you might want to know about networking, instead it contains the bare minimum information you may need to connect to a wireless network.
Connecting to a Wireless Network
Open up your terminal because you are going to need it connect to a wireless network through command-line tools; however, bear in mind that this is not necessarily a walk in the park. You may need to consult other resources if authenticating with a network other than the typical home network.
Listing Wireless Capable Network Interfaces
The fastest way to know if your system has a wireless capable interface is
to execute iwconfig without arguments, you may
even perform this step as a regular user:
iwconfig
The output could be something similar to this:
-
enp0s31f6 no wireless extensions. -
wlp0s20f3 IEEE 802.11 ESSID:off/any -
lo no wireless extensions.
Some of the output was omitted for brevity but surely you have noticed that
in this example the only wireless capable interface is the
wlp0s20f3 interface.
Handling Software Blocks on Wireless Devices
It's a good idea to verify if the wireless device has a software level
block because as long as the blocking remains you won't be able to proceed
any further. To verify if there is a software block use
rfkill to list the wireless devices that have
been blocked:
rfkill list
If there's a software block you will see an output similar to this one:
0: phy0: Wireless LAN-
Soft blocked: yes -
Hard blocked: no
If there's a hardware block you will need to find out what button on your machine has the toggling functionality for the wireless device and that should be easy to do. To disable the software block in this example we would be using the device id that preceeds the device info. The command to unblock the wireless device would look like this in this particular example (note the index zero just prior to the device info):
rfkill unblock 0
It's useful to know that the rfkill tool does
not only shows the status of
network devices but also bluetooth devices as well. If you
ever encounter a software block on bluetooth devices you would know
how to address that.
Scanning for Available Wireless Networks
Now that you know the name of the wireless capable interface you can bring it up so that you can scan what wireless networks are available near your linux machine. However to achieve that you will need system administration privileges and so now it's the time to do so via:
sudo su
I am assuming that your user is in the sudoers list, if not you will need to
know the root password to login as the
root user via:
su -l root
Experienced linux users know that they can use a shorter version (they would not be reading this blog anyways because they already know), I have decided to show the most verbose way of doing it to make it evident to new linux users.
Now that you have the required system administration privileges the first thing you need to do is to bring the wireless network capable interface up so that you can scan for wireless networks in your area:
ip link set wlp0s20f3 up
Note that you would be replacing wlp0s20f3 in
the command line string with the name of the wireless capable network
interface of your linux machine.
Now you can scan for wireless networks via:
iwlist wlp0s20f3 scan
Again experienced linux users know that you can omit the network interface
name from the command-line string but again for the benefit of the new linux
users I am writing the longer version. The output of that command is going
to be lengthy so probably you would want to match
essid's and you can do that in this way:
iwlist wlp0s20f3 scan | grep -i essid
What we are doing here is to effectively filter the output of the wireless
scan so that any output line containing the
essid
string will match and will be present on the output, anything else will be
filtered out and so it will not be shown. The output would be something
like this:
ESSID:"Wireless-Network-1"ESSID:"Wireless-Network-2"ESSID:"Wireless-Network-3"
The wireless network that you may want to connect to is probably going to show up in that list. At this point you are ready to write the configuration file that the WPA supplicant, which is the client that will use to connect to the wireless network.
Connecting to the Wireless Network with WPA Supplicant
It's likely that the configuration file for the WPA supplicant is already present in your system. The file I am referring to is:
/etc/wpa_supplicant/example.conf
If not present, you can always create it, I am assuming that if you are familiar with creating files and editing them with a text editor. Here are the key-value pairs that you have to set for most home networks:
-
network={ -
ssid="home" -
scan_ssid=1 -
key_mgmt=WPA-PSK -
psk="very secret passphrase" -
}
You would be setting the network name, ssid,
and the passphrase, psk. If you are not
simply connecting to a home network I strongly recommend you to consult
the man page for wpa_supplicant.conf, for
it has various examples from which you can adapt to your case.
After writing the configuration file you can now try to connect to the wirless network via:
wpa_supplicant -c wpa_supplicant.conf -i wlp0s20f3
Note that in practice you would be providing the full path to the
configuration file, don't forget to modify the interface name (if needed)
and consult the man page of wpa_supplicant for
more information.
If that went well you will know from the console output, and it would probably look like this near the end:
wlp0s20f3: CTRL-EVENT-CONNECTED
Of course there's more output than that but you don't need to be concerned
about it in most cases (probably). Note that all that you have achieved so
far is to authenticate, you cannot yet use the internet without obtaining
an ip address. To finalize your connection to the network
you may use DHCP client:
dhclient -d -i wlp0s20f3
You are free to omit the -d flag which forces
the client to execute on the foreground. This is just my preference because
I like to see the client output on the calling console. Hopefully by now
you have been able to connect to your wireless network and now can browse
the web, connect to remote servers, or whatever else your business may be.
Bear in mind that all these commands must be run with system administrator
privileges, or simply run as root.
Hope that this article has been useful to you. See you in my next blog post. Cheers!
Conclusions
In this blog post you learned that Linux provides robust command line tools to connect to wireless networks. And that the complexity of doing so mostly depends on the nature of the network you are attempting to connect. Due to that inherent nature we just considered home networks because the intention of this blog post is not to show you how to connect to every network there is but to guide you in the right direction. As mentioned in the blog post the WPA client man page probably has the information that you need to connect to networks other than home networks. Other than that other pitfalls were mentioned like software blocks and bringing up the device for scanning.
If you encounter other issues that you think are important don't forget to reach out, on my landing page you will find ways to contact me.