Let’s learn to connect to WiFi through the terminal (command line method). While working with Linux, sometimes it may happen that we don’t have access to a Graphical User Interface for example during Vanilla Arch Installation. Under such circumstances, we may need to connect to Wifi through the terminal.
Steps to connect to WiFi through the terminal
In this module, we are going to learn how we can connect to a Wifi network using the command line interface.
Step 1: Identify Your Available Network Devices
First, we need to identify the network devices that we are going to use to connect to our wifi network. We can list our interfaces with the ip command:
$ ip a
Looking at the output of the latter, we should find a similar section in its output :
wlan0 IEEE 802.11 ESSID:off/any
Mode:Managed Access Point: Not-Associated Tx-Power=20 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Here our interface is called wlan0 but it may be different from yours. Now we examine the output of the former which give us the following output for wlan0 :
wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 2c:6e:85:fe:04:a7 brd ff:ff:ff:ff:ff:ff
The most important thing to notice here is that the wlan0 state is currently DOWN which we need to enable by turning the state UP using the ifconfig command:
$ sudo ifconfig wlan0 up
Alternatively, you can check the WiFi radio status with nmcli via:
$ nmcli radio wifi
If it is not enabled, you can do it with:
$ nmcli radio wifi on
Finally, you can check the status of your network interface cards with:
$ nmcli dev status
DEVICE TYPE STATE CONNECTION
wlan0 wifi disconnected
Note: If your wifi card is hard blocked on your Laptop/Computer, you need to execute the following and repeat the above steps.
$ echo "blacklist hp_wmi" | sudo tee /etc/modprobe.d/hp.conf
$ sudo rfkill unblock all
Step 2: Scan For Available Wifi Networks
Now that we have our wifi card up and ready, we need to scan for available wifi networks with which we can connect to using :
$ nmcli dev wifi list
$ sudo iw wlan0 scan | grep SSID
This should list out the names of all the available wifi networks around you. Considering the former, we should get an output like :
IN-USE BSSID SSID MODE CHAN RATE SIGNAL BARS SECURITY
60:63:4C:5D:F6:69 home-network Infra 1 270 Mbit/s 100 ▂▄▆█ WPA1 WPA2
Thus as we see we have the SSID of the wifi network we want to connect to.
Step 3: Connect To The Wifi Network
Now that we have the SSID of our we can simply connect to it with the following command:
$ sudo nmcli --ask dev wifi connect <SSID>
Device 'wlan0' successfully activated with 'f747251b-1346-48a2-ae25-1b6fd6243984'.
The above method was the easiest way to get started, however, let me also introduce you to another method where we include the SSID and password in a single command,
sudo nmcli dev wifi connect <network-ssid> password <network-password>
Well, I just showed you to load the command with both SSID and password, if you choose to you can skip entering the password with SSID and just add SSID like so,
sudo nmcli dev wifi connect <network-ssid>
After this, our device should be connected to the wifi network.
You can check if you are connected by pinging the default gateway or if you want to check for an internet connection, you can just type :
$ ping 184.108.40.206
PING 220.127.116.11 (18.104.22.168) 56(84) bytes of data.
64 bytes from 22.214.171.124: icmp_seq=1 ttl=114 time=50.4 ms
64 bytes from 126.96.36.199: icmp_seq=2 ttl=114 time=46.4 ms
64 bytes from 188.8.131.52: icmp_seq=3 ttl=114 time=45.2 ms
64 bytes from 184.108.40.206: icmp_seq=4 ttl=114 time=42.2 ms
64 bytes from 220.127.116.11: icmp_seq=5 ttl=114 time=44.5 ms
64 bytes from 18.104.22.168: icmp_seq=6 ttl=114 time=48.8 ms
--- 22.214.171.124 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5008ms
rtt min/avg/max/mdev = 42.175/46.245/50.375/2.723 ms
Since we can ping Google’s DNS servers, we can say that we now have an internet connection!
How to Manage Network Connections Using nmcli
We have learned how versatile nmcli is but nmcli command is not just limited to the use cases outlined above, we can also,
View Previously Saved Network Connections
This can be done by simply executing the following command,
nmcli con show
Disconnect the Current Connection
In case you want an alternate connection when multiple networks are available,
nmcli con down <Enter SSID or UUID>
Connect to a Saved Connection
Say we disconnected from a previously saved connection we can reconnect to that network by using the following command,
nmcli con up <Enter SSID or UUID>
Hence we have successfully connected to a wifi network from our CLI. Here we explicitly chose the nmcli command to do most of the work for us because it is available on a wide array of systems including live Arch Linux ISOs as well as is very easy to use and makes troubleshooting easy!