How to connect to WiFi through the terminal command line?

Connect To Wifi Via CLI On Linux

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 our network devices which we are going to use to connect to our wifi network. We can list our interfaces with the ip command:

$ ip a

Or

$ iwconfig

Looking at the output of the latter, we should find a similar section in it’s 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
          Power Management:off

Here our interface is called wlan0 but it may be different for 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 wlan0 is marked DOWN which we need to turn UP with the ifconfig command:

$ sudo ifconfig wlan0 up

Alternatively, you can check the WiFi radio status with nmcli via:

$ nmcli radio wifi   
enabled

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 Netoworks

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  

Or,

$ 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>
Password: 
Device 'wlan0' successfully activated with 'f747251b-1346-48a2-ae25-1b6fd6243984'.

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 internet connection, you can just type :

$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=114 time=50.4 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=114 time=46.4 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=114 time=45.2 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=114 time=42.2 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=114 time=44.5 ms
64 bytes from 8.8.8.8: icmp_seq=6 ttl=114 time=48.8 ms

--- 8.8.8.8 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 !

Conclusion

Hence we successfully connected to a wifi network from out 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 !