Home Linux Connect And Disconnect WiFi From Commandline In Linux
Connect And Disconnect WiFi From Commandline In Linux

Connect And Disconnect WiFi From Commandline In Linux

By sk
5328 Views

Even though getting things done via graphical mode is quite easy, you should know how to perform most of the tasks from commandline in Linux. Because, there are chances that you may need to work with Linux servers with no GUI! Also some specific tasks might be uncooperative or unresponsive in graphical mode. In this brief guide, we will learn one of the basic Linux networking concept i.e. how to connect and disconnect WiFi from commandline in Linux operating system.

There are multiple ways to activate and deactivate wireless networks in Linux from commandline. Here, we are going to discuss about two commandline tools namely nmcli and nmtui. Both comes pre-installed in many modern Linux distributions.

1. Connect and disconnect WiFi from commandline with Nmcli in Linux

Nmcli is a commandline interface used to control NetworkManager and report network status in Linux. Using nmcli, we can create, display, edit, delete, activate, and deactivate network connections, as well as control and display network device status. All from commandline! No need to use any other GUI clients to configure wired or wireless network connections in Linux. Nmcli comes pre-installed with all GNOME based Linux desktops.

First, let us view the list of saved network connections and the wireless device name in your Linux system using command:

$ nmcli connection

Or, shortly use this:

$ nmcli c

The above command will list all saved network connections (both wired and wireless) in your Linux machine:

 NAME                   UUID                                  TYPE       DEVICE          
 JioFi4_12E9FE          a583ac06-30da-4e8b-b9e0-674ec33bece8  wifi       wlp9s0          
 mpqemubr0              44554d0e-a243-41ad-a298-a02737d6e8aa  bridge     mpqemubr0       
 virbr0                 5bd9c4cd-d546-4a62-8c51-051d392d062c  bridge     virbr0          
 tap-d8f1eebb39f        eae33bb5-6430-4ab8-9ebd-d2a05ec0b02d  tun        tap-d8f1eebb39f 
 HP 7 VoiceTab Network  73ef7815-0fb0-4272-8bfd-8ecf85c75225  bluetooth  --              
 sktab                  1732d713-ad4e-4ec5-8dad-49f0cd8f1cb2  wifi       --              
 Wired connection 1     410f30c5-b1ca-3e7c-9083-2b59c73be34a  ethernet   --              
List available network connections using nmcli command
List available network connections using nmcli command

As you see in the above output, my wireless network connection name is "JioFi4_12E9FE" and the Wireless network interface name is "wlp9s0".

If you want to list all wireless networks (i.e. access points, hotspots) around you, run this command:

$ nmcli device wifi list 

Or, shortly use this command:

$ nmcli d wifi list 

Sample output:

* 40:C8:CB:12:E9:FE  JioFi4_12E9FE          Infra  6     65 Mbit/s   69      ▂▄▆_  WPA2  
[...]
List wireless network connections in Linux using nmcli command
List wireless network connections in Linux using nmcli command

The WiFi network currently in use are marked with an asterisk (*) symbol.

You can optionally pass --rescan yes flag to tell nmcli to rescan all available WiFi networks near you:

$ nmcli device wifi list --rescan yes

After finding the list of WiFi connections, you can connect to a specific WiFi network, for example JioFi4_12E9FE, using the following command:

$ nmcli c up JioFi4_12E9FE

Sample output:

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/20)

To disconnect from a WiFi network, run:

$ nmcli c down JioFi4_12E9FE

Sample output:

Connection 'JioFi4_12E9FE' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/20)
Connect and disconnect WiFi from commandline with Nmcli in Linux
Connect and disconnect WiFi from commandline with Nmcli in Linux

Use -a (--ask) flag if you want to be prompted to enter the WiFi password:

$ nmcli -a c up JioFi4_12E9FE

Once the WiFi is connected, the connection is saved and it will be automatically reconnected at every reboot.

You don't have to use -a every time. The connected networks will be saved and reconnected automatically in the subsequent times.

The another way to connect to a wireless network connection using nmcli command is:

$ nmcli -a d wifi connect JioFi4_12E9FE

Here, the -a flag will ask you to enter the WiFi password.

After you entered the correct password, you will see the successfully connected message:

Device 'wlp9s0' successfully activated with 'a583ac06-30da-4e8b-b9e0-674ec33bece8'.

To disconnect from the WiFi network, simply deactivate the network card:

$ nmcli d disconnect wlp9s0

Replace "wlp9s0" with your wireless card name.

To activate the wireless card again, do:

$ nmcli d connect wlp9s0
Activate and deactivate wireless connection from commandline with Nmcli
Activate and deactivate wireless connection from commandline with Nmcli

In the above examples, we have activated and deactivated a specific Wireless network connection and interface. If you want to enable or disable the entire WiFi access in your Linux system, simply turn your wireless radio ON or OFF like below:

$ nmcli radio wifi on
$ nmcli radio wifi off

Once you turned on the WiFi radio, the saved connection will auto-connect.

To make your NetworkManager to forget (i.e. remove from saved list) the connection, do:

$ nmcli c delete JioFi4_12E9FE

You need to enter the correct password to connect to this network next time.

For more details, refer nmcli man pages.

$ man nmcli

2. Connect and disconnect WiFi from commandline with Nmtui in Linux

Nmtui is a curses-based text user interface (TUI) to control and interact with NetworkManager.

To connect or disconnect WiFi networks from commandline in a Linux system, launch nmtui tool using command:

$ nmtui

Choose "Activate a connection" option and hit ENTER key.

Nmtui text user interface
Nmtui text user interface

In the next window, choose the WiFi connection you want to activate or deactivate. If the selected WiFi connection is already connected, you will see "Deactivate" option. If it is disconnected, you will then see the "Activate" option. Choose the Activate/Deactivate option and hit ENTER to connect to or disconnect from the selected Wireless network.

Connect and disconnect WiFi from commandline with Nmtui
Connect and disconnect WiFi from commandline with Nmtui

For more details, refer man pages:

$ man nmtui

Hope this helps.

Related read:

Featured image by ivke32 from Pixabay.

You May Also Like

2 comments

j February 2, 2021 - 9:23 am

Hi,
Pretty good and easy.
Thank you so much for the great topic

Reply
Just say no to network manager February 4, 2021 - 9:00 am

This article assumes that the user uses the abomination known as network manager. Other alternatives are available eg wicked or if you have neither, then you should learn how to use wpa_supplicant and connect and disconnect from the command line using that and the ip command to set wireless interface address and route.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More