Connect to WiFi in Linux Using NMCLI command

1
10327

This tutorial will guide you on how to connect to WiFi in Linux using NMCLI command. NMCLI is a command-line tool for controlling NetworkManager and reporting network status. It can be utilized as a replacement for nm-applet or other graphical clients. It is used to create, display, edit, delete, activate, and deactivate network connections, as well as control and display network device status.

Typical uses include:

  • Scripts: Utilize NetworkManager via nmcli instead of managing network connections manually. nmcli supports a terse output format which is better suited for script processing
  • Servers, headless machines, and terminals: nmcli can be used to control NetworkManager without a GUI, including creating, editing, starting and stopping network connections and viewing network status.

Want to use NETPLAN on Ubuntu 18.04 to check set static IP address? Check our previous guide by following the link below;

How to Configure Static IP Addresses on Ubuntu 18.04 Server using Netplan

Connect to WiFi in Linux using NMCLI command

Check Wi-Fi Radio Status

Well, before you can connect to a wireless network in Linux, you need to check if WiFi radio status.

This can be done by executing the command below;

nmcli radio wifi
enabled

If the WiFi radio is disabled, then you can enable it by running the command below;

nmcli radio wifi on

You can as well check status of the network interface cards by running the command below;

nmcli dev status
DEVICE  TYPE      STATE         CONNECTION 
wlp2s0  wifi      disconnected  --         
enp1s0  ethernet  unavailable   --         
lo      loopback  unmanaged     -- 

Check Available Wi-Fi Access Points

NetworkManager scans Wi-Fi networks periodically. To check if the SSID of Wi-Fi access point you want to connect to can be seen your Linux system, run the command below;

nmcli dev wifi list
SSID            MODE   CHAN  RATE       SIGNAL  BARS  SECURITY 
Kmibey          Infra  2     54 Mbit/s  100     ▂▄▆█  WPA2     
Ncheches house  Infra  1     54 Mbit/s  37      ▂▄_   WPA2 

If you are unable to see the SSID of the Wi-Fi access point you want to connect to, you can run a rescan by executing the command below. After that, check again to if you can see the Access point SSID by re-executing the command above.

nmcli dev wifi rescan

Connect to WiFi using NMCLI

Assuming that you already have the SSID and the connection password for the Access point you want to connect to, then execute the command below to connect.

sudo nmcli dev wifi connect Kmibey password 'mypassword'
Device 'wlp2s0' successfully activated with 'a1900bed-baa9-47a3-affb-b640d0effe5d'.

If you do not want to display password in plain text, then you can pass the –ask option as shown below.

sudo nmcli --ask dev wifi connect Kmibey
Password: ENTER PASS
Device 'wlp2s0' successfully activated with 'f747251b-1346-48a2-ae25-1b6fd6243984'.

Check Device status

nmcli dev status
DEVICE  TYPE      STATE        CONNECTION 
wlp2s0  wifi      connected    Kmibey     
enp1s0  ethernet  unavailable  --         
lo      loopback  unmanaged    -- 

Check Active Connections

Once you have successfully connected to your AP, then you can check active connections using the command below;

nmcli con show --active
NAME    UUID                                  TYPE             DEVICE 
Kmibey  a1900bed-baa9-47a3-affb-b640d0effe5d  802-11-wireless  wlp2s0

You can as well omit the –active option to list all the connections and their status.

Delete Established Connections

If you need to delete the connection that is already established, you can specify the connection UUID or the connection name. To obtain the connection UUID or the username, run the nmcli con show command as shown below;

nmcli con sh
NAME                UUID                                  TYPE             DEVICE 
Kmibey              41dc9830-dd20-4deb-92be-371bfb5d16f0  802-11-wireless  wlp2s0 
Beats               949762b0-c6e0-4004-918a-55fb6bcf6610  802-11-wireless  --

To delete using a connection using connection name;

sudo nmcli con del Kmibey
Connection 'Kmibey' (41dc9830-dd20-4deb-92be-371bfb5d16f0) successfully deleted.

To delete a connection using connection UUID;

sudo nmcli con del 41dc9830-dd20-4deb-92be-371bfb5d16f0
Connection 'Kmibey' (41dc9830-dd20-4deb-92be-371bfb5d16f0) successfully deleted.

You can also take down or bring up a previous connection using the commands below respectively.

nmcli con down <SSID or UUID>
nmcli con up <SSID or UUID>

That is all about how to connect to WiFi in Linux using NMCLI command.

Read more on:

man nmcli
man nmcli-examples

Other Tutorials

Install and use NMCLI tool on Debian Linux

Create Virtual/Secondary IP addresses on an Interface in Linux

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here