Linux Commands

How to Connect to WiFi Network from the Command-Line on Linux Using NetworkManager

In this article, we will show you how to connect to your WiFi network from the command line on modern Linux distributions that use the NetworkManager to manage the network devices.

This article should work on the following listed Linux distributions and other Linux distributions (not listed) that use the NetworkManager for managing the networks and have the “nmcli” command-line tool available.

  • Ubuntu
  • Debian
  • Linux Mint
  • Elementary OS
  • Fedora
  • RHEL
  • CentOS Stream
  • AlmaLinux
  • Rocky Linux
  • openSUSE
  • SUSE Linux Enterprise Server (SLES)
  • Oracle Linux

Topic of Contents:

  1. Listing All the Network Devices of Linux from the Command Line Using Nmcli
  2. Searching for Available WiFi Networks on Linux from the Command Line Using Nmcli
  3. Connecting to a WiFi Network on Linux from the Command Line Using Nmcli
  4. Checking for WiFi/Internet Connectivity from the Command Line
  5. Disconnecting from the WiFi Network on Linux from the Command Line Using Nmcli
  6. Conclusion

Listing All the Network Devices of Linux from the Command Line Using Nmcli

To list all the network devices that are installed on your Linux computer, run the following command:

$ sudo nmcli device

You should find your WiFi network interface in the list. In our case, the WiFi network interface is called “wlp7s27u1”.

Searching for Available WiFi Networks on Linux from the Command Line Using Nmcli

To list all the WiFi networks in your area, run the following command:

$ sudo nmcli device wifi list

As you can see, all the WiFi networks in our area are listed.

If, for some reason, your desired WiFi SSID is not displayed on the list, do a rescan with the following command:

$ sudo nmcli device wifi rescan

As you can see, a lot more WiFi networks are listed after the rescan.

$ sudo nmcli device wifi list

Connecting to a WiFi Network on Linux from the Command Line Using Nmcli

To connect to the “NodeKite-2.4G” WiFi network SSID (let’s say) that’s configured with the WPA2 “secret” password, run the following command:

$ sudo nmcli device wifi connect "NodeKite-2.4G" password "secret"

You can also use the BSSID of your WiFi router to connect to the WiFi network as follows:

$ sudo nmcli device wifi connect "68:22:BB:41:B6:A9" password "secret"

If you want to connect to a hidden WiFi network with the “HiddenNet” SSID (let’s say), you can do so as follows:

$ sudo nmcli device wifi connect "HiddenNet" password "secret" hidden yes

If you have multiple WiFi network interfaces on your computer and you want to connect to a WiFi network using a specific WiFi network interface, you can do so by adding the “ifname” flag at the end as follows:

$ sudo nmcli device wifi connect "NodeKite-2.4G" password "secret" ifname wlp7s27u1

A new NetworkManager connection should be created and activated and your computer should be connected to the WiFi network.

As you can see, a new “NodeKite-2.4G” NetworkManager connection profile (same name as the WiFi SSID that you configured to connect) is created.

$ sudo nmcli connection

Checking for WiFi/Internet Connectivity from the Command Line

Once you’re connected to your desired WiFi network, your WiFi network interface (wlp7s27u1 in this case) should receive an IP address from your router via DHCP as you can see in the following screenshot:

$ ip a

You should also be able to ping “google.com” (or the domain names of any other popular websites).

$ ping -c 3 google.com

Disconnecting from the WiFi Network on Linux from the Command Line Using Nmcli

To disconnect from the WiFi network SSID NodeKite-2.4G, run the following command:

$ sudo nmcli connection down "NodeKite-2.4G"

As you can see, the “NodeKite-2.4G” NetworkManager connection is down[1] as well as the “wlp7s27u1”[2] WiFi network interface. You won’t be able to ping the DNS names of any website as well once you’re disconnected from the WiFi network[3].

$ sudo nmcli connection

$ ip a

$ ping -c 3 google.com

Conclusion

In this article, we showed you how to use the “nmcli” NetworkManager command-line tool to list the available WiFi networks in your area and connect to your desired WiFi network. We also showed you how to check for internet connectivity once you’re connected to a WiFi network from the command line on Linux. After reading this article, you should be able to manage the WiFi networks on your Linux system from the command line using the “nmcli” NetworkManager tool.

References:

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.