
How to Connect to WiFi in Linux Using NMCLI: A Step-by-Step Guide
Managing network connections from the Linux command line can feel intimidating, but it’s an essential skill for anyone working with servers, headless systems, or simply looking for more control over their machine. While graphical user interfaces (GUIs) are convenient, they aren’t always available. This is where nmcli, the command-line interface for NetworkManager, becomes an indispensable tool.
NetworkManager is the standard networking service on most modern Linux distributions, and nmcli allows you to control it entirely from your terminal. This guide will walk you through the essential steps to find, connect to, and manage WiFi networks using nmcli, empowering you to handle your connectivity like a pro.
Why Use NMCLI for WiFi?
Before diving in, it’s helpful to understand why a command-line tool is so powerful:
- Server Administration: Most Linux servers do not have a desktop environment.
nmcliis the primary way to configure networking on these headless systems. - Automation and Scripting: You can easily incorporate
nmclicommands into shell scripts to automate network connections, which is perfect for deployment or recovery tasks. - Troubleshooting: When your desktop’s network applet fails or freezes,
nmcliprovides a reliable fallback to get you back online. - Efficiency: For experienced users, typing a single command is often faster than clicking through multiple GUI windows.
Step 1: Check Your Network Devices
First, you need to identify your wireless network interface. To see a list of all network devices recognized by NetworkManager, run the following command:
nmcli device status
You will see a list of devices. Your wireless card will typically be named something like wlan0 or wlp3s0. Look for the device with the type wifi. Make a note of this device name, as you will need it for subsequent commands. You should see its state as disconnected if you are not yet connected to a network.
Step 2: Scan for Available WiFi Networks
Once you’ve identified your wireless device, you can scan for nearby WiFi networks. This command will list all visible SSIDs (network names), along with their signal strength, security type, and other useful information.
To perform a scan, use the command:
nmcli device wifi list
If the list doesn’t appear, you may need to request a fresh scan. You can do this by running nmcli device wifi rescan. Running the list command again will then show the updated results. Review the list and identify the SSID of the network you wish to join.
Step 3: Connect to a WiFi Network
This is the core step. The command you use will depend on whether the network is secured with a password (like WPA2) or is an open, unsecured network.
Connecting to a Password-Protected Network
This is the most common scenario. You will need both the network’s SSID and its password. The command structure is as follows:
nmcli device wifi connect "Your-SSID" password "Your-Password"
It is crucial to replace "Your-SSID" and "Your-Password" with the actual network name and password. Using quotes is highly recommended, especially if the SSID or password contains spaces or special characters.
If the connection is successful, NetworkManager will automatically save the connection profile, so you won’t need to re-enter the password the next time you are in range of that network.
Connecting to an Open (Unsecured) Network
To connect to a network without a password, you simply omit the password part of the command:
nmcli device wifi connect "Your-SSID"
Security Tip: Be extremely cautious when connecting to open, unsecured WiFi networks. Your traffic is not encrypted and can be easily intercepted by malicious actors on the same network. Always use a trusted VPN when connected to a public or unsecured network to encrypt your data and protect your privacy.
Step 4: Verify Your Connection Status
After running the connect command, you should receive a confirmation message. However, it’s always good practice to verify your connection.
You can check your active connections with:
nmcli connection show
This will list all configured network profiles, and your newly connected network should appear highlighted or at the top of the list.
To get more detailed information, including your assigned IP address, run:
ip addr show wlan0
(Remember to replace wlan0 with your actual wireless device name). If you see an inet address listed, you have successfully received an IP address from the network. Finally, you can test your internet connectivity with a quick ping:
ping -c 3 google.com
If you receive replies, you are successfully connected and online.
Managing WiFi Connections
nmcli is not just for connecting; it’s also a powerful tool for managing your network profiles.
- To disconnect from your current network:
bash
nmcli device disconnect wlan0
- To view all saved connection profiles:
bash
nmcli connection show
- To delete a saved network profile you no longer need:
bash
nmcli connection delete "Profile-Name"
(The “Profile-Name” is often the same as the network’s SSID).
By mastering these nmcli commands, you gain precise control over your Linux machine’s network connectivity. It’s a fundamental skill that enhances your ability to work in any environment, from a graphical desktop to a remote server, ensuring you can always get connected when you need to.
Source: https://kifarunix.com/connect-to-wifi-in-linux-using-nmcli-command/


