How to Connect Wi-Fi Network Interface From Linux Shell Prompt

There are several tools for managing a wireless network interface on linux but my favorite one is nmcli, a command-line tool used to create, show, edit, delete, enable, and disable network connections, as well as control and display network device status.

In this tutorial, we'll show you how to manage wireless network interfaces using nmcli tool in any linux distribution.

First start by checking the name of your network device using the following command. From the output of this command, the device name/interface is wlp1s0 as shown.

$ iw dev
Interface wlp1s0
ifindex 3
wdev 0x1
addr 33:c1:bd:5c:75:c4
type managed

Next, check the Wi-Fi device connection status using the following command.

$ iw wlp2s0 link
Not connected.

From the output above the device is not connected to any network, run the following command to scan available Wi-Fi networks.

$ sudo iw wlp2s0 scan
command failed: Network is down (-100)

Considering the output of the above command, the network device/interface is DOWN, you can turn it On (UP) with the ip command as shown.

$ sudo ip link set wlp1s0 up

If you get the following error, that means your Wifi is hard blocked on Laptop or PCs.

RTNETLINK answers: Operation not possible due to RF-kill

To remove or unblock, you need to run the following command to solve the error.

$ echo "blacklist hp_wmi" | sudo tee /etc/modprobe.d/hp.conf
$ sudo rfkill unblock all

Then try to turn ON the network device once more, and it should work this time around.

$ sudo ip link set wlp1s0 up

If you know the ESSID of the Wi-Fi network you wish to connect to, move to the next step, otherwise issue the command below to scan available Wi-Fi networks again.

$ sudo iw wlp1s0 scan

And lastly, connect to the wi-fi network using following command, where Hackernet (Wi-Fi network SSID) and localhost22 (password/pre-shared key).

$ nmcli dev wifi connect Hackernet password localhost22

Once connected, verify your connectivity by doing a ping to an external machine and analyze the output of the ping as shown.

$ ping
PING ( 56(84) bytes of data.
64 bytes from icmp_seq=1 ttl=48 time=61.7 ms
64 bytes from icmp_seq=2 ttl=48 time=61.5 ms
64 bytes from icmp_seq=3 ttl=48 time=61.6 ms
64 bytes from icmp_seq=4 ttl=48 time=61.3 ms
64 bytes from icmp_seq=5 ttl=48 time=63.9 ms
--- ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 61.338/62.047/63.928/0.950 ms

Wrapping up
In this tutorial, we demonstrated how to setup your Wi-Fi network from the Linux command line. If you feel this was helpful, please leave your thoughts in below comment section.

No comments