How to Connect Wi-Fi from Linux Terminal Using Nmcli Command

There are several command-line tools for managing a wireless network interface in Linux systems. A number of these can be used to simply view the wireless network interface status (whether it is up or down, or if it is connected to any network), such as iw, iwlist, ip, ifconfig and others.

And some are used to connect to a wireless network, and these include: nmcli, is a command-line tool used to create, show, edit, delete, enable, and disable network connections, as well as control and display network device status.

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

phy#0
	Interface wlp1s0
		ifindex 3
		wdev 0x1
		addr 38:b1:db:7c:78:c7
		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 Computer.

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 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=48 time=61.7 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=48 time=61.5 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=48 time=61.6 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=48 time=61.3 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=48 time=63.9 ms
^C
--- 8.8.8.8 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

That’s It! I hope this article helped you to setup your Wi-Fi network from the Linux command line. As always, if you found this article useful, share your thoughts in the comment section below.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

13 Comments

Leave a Reply
  1. It is a well-presented tutorial. I am currently running CentOS on a desktop. I noticed that my system shows a WiFi interface wlp3s0, and I was able to scan for WiFi networks that I can connect to.

    However, the last step using nmcli to connect to my WiFi network is failing with the error message “Error: No Wi-Fi device found“.

    If there is no WiFi device, how was I able to complete all the other steps as you mentioned? I even used the command sudo iw wlp1s0 scan which listed all the WiFi networks and their related status information.

    The desktop originally had WiFi when it was running Windows, but it was removed when Windows 7 became outdated and CentOS v7 was installed.

    Reply
    • @Laks,

      I’m glad you found the tutorial helpful!

      It seems like the issue you’re encountering with nmcli could be related to the network manager not recognizing your WiFi device properly. Even though you can see the WiFi interface wlp3s0 and scan for networks using sudo iw wlp1s0 scan, nmcli might not be detecting it correctly.

      Here are a few steps you can try to resolve this issue:

      sudo systemctl restart NetworkManager
      systemctl status NetworkManager
      nmcli device
      sudo dpkg-reconfigure NetworkManager
      sudo lshw -C network
      
      Reply
  2. This is the BEST tutorial, quickest possible way to connect WiFi.THANKS A LOT for putting this together Aaron Kili – you rock !

    Reply
  3. I’ve tried hundreds of other tutorials, and I am so happy this one works!!! Thank you so much, I literally cannot thank you enough for this!!!

    Reply
  4. Hats off to you Aron Kili. I was unable to login to Linux mint 19 cinnamon, it was all black screen, nothing was there. I connected to wifi after going to Console (ctrl+alt+f1),then reinstalled cinnamon after connecting to wifi. Now i am able to use my laptop. Long live Aron.

    Reply
  5. Just curious: was this an “RPM” based system that these commands were run on? or a “DEB” system? (In case I need to install certain components to get the nmcli to run properly!? Great article!

    Reply
    • @Eddie

      Our test environment was Ubuntu a DEB system, however, nmcli should come pre-installed on all if not most mainstream Linux distros. Thanks for appreciating this piece of work.

      Reply

Leave a Reply to Sambit Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.