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



If you are a Linux user and wish to connect to a Wi-Fi network from a terminal. Although it may seem scary, it is actually a simple process using the Nmcli command. In this article, I will introduce you to the steps of connecting Wi-Fi from a Linux terminal using Nmcli commands in a simple and easy-to-understand way. So let's get started!

First, if we want to check the name of our network device we can simply do that by running the following command ?

$ iw dev 

You'll get the output kind of this by running the command ?

phy#0 Interface wlan0 ifindex 3 wdev 0x1 addr 12:34:56:78:9a:bc ssid MyWiFiNetwork type managed channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz txpower 20.00 dBm 

In this case, the name is wlan0. Please consider what you're getting.

And next, we'll check our network device's status whether it's connected to the WI-Fi or not. For that run the below command ?

iw wlan0 link 

You'll get the status of your device look like this ?

Not connected. 

Before we look more into this, it's important to note that not all Linux distributions come with the Nmcli command pre-installed in the system. If you don't have it installed on your system, you can install it by using your package manager. For example, on Ubuntu or Debian-based systems, you can install Nmcli by running the following command in the terminal ?

sudo apt-get install network-manager 

When you run the above command for installation of nmcli you'll get terminal output that looks similar to this ?

Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libnma0 libteamdctl0 network-manager-config-connectivity-ubuntu network-manager-gnome Suggested packages: policykit-1-gnome The following NEW packages will be installed: libnma0 libteamdctl0 network-manager network-manager-config-connectivity-ubuntu network-manager-gnome 0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded. Need to get 2,793 kB of archives. After this operation, 11.1 MB of additional disk space will be used. Do you want to continue? [Y/n] 

Once you have Nmcli installed, you can use it to connect to Wi-Fi networks. Follow these instructions carefully!!

Step 1: Scanning for Wi-Fi Networks

Before you connect to a Wi-Fi network, you need to know its name (SSID). You can use the Nmcli command to scan for available Wi-Fi networks by running the following command in the terminal ?

nmcli device wifi list 

By running this command you'll get output that looks like this ?

IN-USE SSID MODE CHAN RATE SIGNAL BARS SECURITY MyWiFiNetwork Infra 6 54 Mbit/s 80 ???_ WPA2 OtherWiFiNetwork Infra 11 54 Mbit/s 70 ??__ WPA1 WPA2 PublicWiFi Infra 1 54 Mbit/s 50 ??__ -- 

This output lists all of the Wi-Fi networks that your computer can detect, along with their SSID (network name), mode (Infrastructure or Ad-Hoc), channel, rate, signal strength, number of bars, and security type. In this example, your computer is currently connected to a network called "MyWiFiNetwork" with WPA2 security, while there are also two other networks detected, "OtherWiFiNetwork" with WPA1/WPA2 security and "PublicWiFi" with no security.

OR

In an alternate way if you don't want to use nmcli command to scan the WI-FI network you can do the same thing with the help of sudo also. For that, the command will be this ?

sudo iw wlan0 scan 

By running this command you'll get this output ?

BSS 00:11:22:33:44:55 (on wlan0)	freq: 2462	beacon interval: 100 TUs	capability: ESS Privacy ShortPreamble ShortSlotTime (0x0431)	signal: -56.00 dBm	last seen: 204 ms ago	SSID: example_network	DS Parameter set: channel 11	RSN: * Version: 1 * Group cipher: CCMP * Pairwise ciphers: CCMP * Authentication suites: PSK * Capabilities: 16-PTKSA-RC 1-GTKSA-RC (0x000c)	HT capabilities:	Capabilities: 0x11ac	HT20/HT40	SM Power Save disabled	RX Greenfield	RX HT20 SGI	RX HT40 SGI	TX STBC	RX STBC 1-stream	Max AMSDU length: 3839 bytes	No DSSS/CCK HT40	HT operation:	Primary channel: 11	Secondary channel offset: no secondary	HT40: disabled	SSID Length: 14	SSID: example_network BSS 66:77:88:99:aa:bb (on wlan0)	freq: 2412	beacon interval: 100 TUs	capability: ESS Privacy ShortPreamble ShortSlotTime (0x0431)	signal: -72.00 dBm	last seen: 456 ms ago	SSID: another_network	DS Parameter set: channel 1	HT capabilities:	Capabilities: 0x11ac	HT20/HT40	SM Power Save disabled	RX Greenfield	RX HT20 SGI	RX HT40 SGI	TX STBC	RX STBC 1-stream	Max AMSDU length: 3839 bytes	No DSSS/CCK HT40	HT operation:	Primary channel: 1	Secondary channel offset: no secondary	HT40: disabled	SSID Length: 15	SSID: another_network 

It's important to note that the output shown here is just an example, and the output you see on your own system may differ depending on your specific Wi-Fi hardware and system configuration.

Step 2: Connecting to the Wi-Fi Network

Once you have the SSID of the Wi-Fi network you want to connect to, you can use the Nmcli command to connect to it. Run the following command in the terminal ?

nmcli device wifi connect <SSID> 

you'll get the following output by running this command

$ nmcli device wifi connect MyWiFiNetwork Device 'wlan0' successfully activated with 'd0:53:49:67:8a:16'. 

In this instance, we're establishing a connection to a Wi-Fi network identified by the SSID "MyWiFiNetwork." To do so, the nmcli command is utilizing the wlan0 device to connect to the network. After successfully connecting to the network, the command provides feedback confirming that the device has been activated, along with its unique MAC address, which in this case is d0:53:49:67:8a:16. Keep in mind that the exact output you see may differ based on your specific system and network setup.

Step 3: Managing Wi-Fi Connections

Once you've connected to a Wi-Fi network using Nmcli, you can manage your connections using the following commands ?

  • To disconnect from the current Wi-Fi network, run the following command ?

nmcli device disconnect 

Here is the output ?

If you were previously connected to a network, the output will be similar to the following ?

Device 'wlan0' successfully disconnected. 

If you were not previously connected to a network, the output will be similar to the following ?

Error: No active connection or device found. 

This output indicates that there was no active network connection to disconnect from, or that

  • To view a list of all the Wi-Fi networks you've connected to, run the following command ?

nmcli connection show 

Here is the output ?

NAME UUID TYPE DEVICE Wired connection 1 12345678-9abc-def0-1234-56789abcdef0 ethernet enp0s25 Wi-Fi network 1 12345678-9abc-def0-1234-56789abcdef1 wifi wlp3s0 Wi-Fi network 2 12345678-9abc-def0-1234-56789abcdef2 wifi wlp3s0 

This command lists all the saved network connections on your system, along with their name, UUID, type, and device. In this example, there are two Wi-Fi network connections and one wired connection.

  • To delete a saved Wi-Fi connection, run the following command ?

nmcli connection delete <connection-name> 

here's an example of the terminal output for this command ?

$ nmcli connection delete MyWifiConnection Connection 'MyWifiConnection' (94e863e8-41dc-4e63-bb02-27cf8c1fcbe0) successfully deleted. 

In this example, we are deleting a Wi-Fi connection named MyWifiConnection and the terminal output confirms that the connection has been successfully deleted. The output also includes a connection ID (94e863e8-41dc-4e63-bb02-27cf8c1fcbe0) for the connection that was deleted.

Replace <connection-name> with the name of the Wi-Fi connection you want to delete.

Conclusion

To wrap up, utilizing the Nmcli command to connect to Wi-Fi networks from the Linux terminal is an effective and expedient way to go online. By adhering to the instructions outlined in this article, you can effortlessly scan for accessible Wi-Fi networks, select the network you want, and competently manage your Wi-Fi connections.

Updated on: 2023-07-26T15:57:29+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements