How To Use nmcli Command on Linux

In this tutorial, you will learn how to use nmcli command on Linux. 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. nmcli 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 or script processing. Note that NetworkManager can also execute scripts, called “dispatcher scripts”, in response to network events.
  • 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.

Syntax;

 nmcli [OPTIONS...] {help | general | networking | radio | connection | device | agent | monitor} [COMMAND] [ARGUMENTS...]

General Commands

Syntax;

 nmcli general {status | hostname | permissions | logging} [ARGUMENTS...]

These commands are used to show NetworkManager status and permissions. You can also get and change system hostname, as well as NetworkManager logging level and domains.

Check connection status using nmcli command

Show overall status of NetworkManager. This is the default action, when no additional command is provided for nmcli general.

nmcli general status
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN    
connected  full          enabled  enabled  enabled  enabled

Check Network Manager Permissions with nmcli

It show the permissions a caller has for various authenticated operations that NetworkManager provides, like enable and disable networking, changing Wi-Fi and WWAN state, modifying connections, etc.

nmcli general permission
PERMISSION                                                        VALUE   
org.freedesktop.NetworkManager.enable-disable-network             yes     
org.freedesktop.NetworkManager.enable-disable-wifi                yes     
org.freedesktop.NetworkManager.enable-disable-wwan                yes     
org.freedesktop.NetworkManager.enable-disable-wimax               yes     
org.freedesktop.NetworkManager.sleep-wake                         no      
org.freedesktop.NetworkManager.network-control                    yes     
org.freedesktop.NetworkManager.wifi.share.protected               yes     
org.freedesktop.NetworkManager.wifi.share.open                    yes     
org.freedesktop.NetworkManager.settings.modify.system             yes     
org.freedesktop.NetworkManager.settings.modify.own                yes     
org.freedesktop.NetworkManager.settings.modify.hostname           auth    
org.freedesktop.NetworkManager.settings.modify.global-dns         auth    
org.freedesktop.NetworkManager.reload                             auth    
org.freedesktop.NetworkManager.checkpoint-rollback                auth    
org.freedesktop.NetworkManager.enable-disable-statistics          yes     
org.freedesktop.NetworkManager.enable-disable-connectivity-check  yes     
org.freedesktop.NetworkManager.wifi.scan                          unknown

Check system hostname using nmcli command

Get and change system hostname. With no arguments, this prints currently configured hostname. When you pass a hostname, it will be handed over to NetworkManager to be set as a new system hostname.

nmcli general hostname
thehero

The currently configured hostname is thehero.

Networking Control Commands

Syntax;

nmcli networking {on | off | connectivity} [ARGUMENTS…]

It queries NetworkManager networking status, enable and disable networking.

Check Network Connectivity state with nmcli

It display network connectivity state. The optional check argument tells NetworkManager to re-check the connectivity, else the most recent known connectivity state is displayed without re-checking.

nmcli networking connectivity
full

The above command shows that the host is connected to a network and has full access to the Internet.

Possible states are:

  • none: the host is not connected to any network.
  • portal: the host is behind a captive portal and cannot reach the full Internet.
  • limited: the host is connected to a network, but it has no access to the Internet.
  • unknown: the connectivity status cannot be found out.

Enable or disable networking using nmcli command

It enable or disable networking control by NetworkManager. All interfaces managed by NetworkManager are deactivated when networking is disabled.

To enable networking run the following command;

nmcli networking on

To disable networking run the following command;

nmcli networking off

Radio Transmission Control Commands

Syntax;

nmcli radio {all | wifi | wwan} [ARGUMENTS…]

It show radio switches status, or enable and disable the switches.

Control WiFi radio using nmcli command

It display or set status of Wi-Fi in NetworkManager. If no arguments are supplied, Wi-Fi status is printed; on enables Wi-Fi; off disables Wi-Fi.

nmcli radio wifi
enabled

The above output shows that the wi-fi status is enabled.

If disabled run;

nmcli radio wifi on

To disable run;

nmcli radio wifi off

Control WWAN with nmcli command

It show or set status of WWAN (mobile broadband) in NetworkManager. If no arguments are supplied, mobile broadband status is printed; on enables mobile broadband, off disables it.

nmcli radio wwan
enabled

The above output shows that the wwan status is enabled.

If disabled run;

nmcli radio wwan on

To disable run;

nmcli radio wwan off

To how or set all the above radio switches at the same time.

nmcli radio all
WIFI-HW  WIFI     WWAN-HW  WWAN    
enabled  enabled  enabled  enabled

Connection Management Commands

Syntax;

nmcli connection {show | up | down | modify | add | edit | clone | delete | monitor | reload | load | import | export} [ARGUMENTS…]

NetworkManager stores all network configuration as “connections“, which are collections of data (Layer2 details, IP addressing, etc.) that describe how to create or connect to a network. A connection is “active” when a device uses that connection’s configuration to create or connect to a network. There may be multiple connections that apply to a device, but only one of them can be active on that device at any given time. The additional connections can be used to allow quick switching between different networks and configurations.

Show Connection with nmcli

List in-memory and on-disk connection profiles, some of which may also be active if a device is using that connection profile. Without a parameter, all profiles are listed.

nmcli connection show
NAME                UUID                                  TYPE       DEVICE 
Pilot 💻💻          eb055a3a-bb23-44ad-bc0d-7faecadcb223  wifi       wlp2s0 
virbr0              6df13e71-3405-4355-946e-c0177459ded0  bridge     virbr0 
Hotspot             5149f768-fd94-4dd7-b0b2-5fda98454e48  wifi       --     
netplan-enp1s0      cac41fbe-bc18-3d87-bba7-af2af7f8ffab  ethernet   --     
Pilot 💻💻 Network  e2dca5f9-56f4-4142-a494-68e8b91b09f4  bluetooth  --

When --active option is specified, only the active profiles are shown;

nmcli connection show --active
NAME        UUID                                  TYPE    DEVICE 
Pilot 💻💻  eb055a3a-bb23-44ad-bc0d-7faecadcb223  wifi    wlp2s0 
virbr0      6df13e71-3405-4355-946e-c0177459ded0  bridge  virbr0

Activate connection using nmcli command

It activate a connection. The connection is identified by its name, UUID or D-Bus path. If ID is ambiguous, a keyword id, uuid or path can be used. When requiring a particular device to activate the connection on, the ifname option with interface name should be given.

nmcli connection up eb055a3a-bb23-44ad-bc0d-7faecadcb223
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)

The above wifi is activated by UUID.

Deactivate a connection using nmcli command

It deactivate a connection from a device without preventing the device from further auto-activation. Multiple connections can be passed to the command.

nmcli connection down virbr0
Connection 'virbr0' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)

The above wifi is deactivated by name.

Delete a connection using nmcli command

It deletes a configured connection. The connection to be deleted is identified by its name, UUID or D-Bus path. If ID is ambiguous, a keyword id, uuid or path can be used.

nmcli connection delete e2dca5f9-56f4-4142-a494-68e8b91b09f4
Connection 'Pilot 💻💻 Network' (e2dca5f9-56f4-4142-a494-68e8b91b09f4) successfully deleted.

The above connection is deleted by its UUID.

Device Management Commands

Syntax;

nmcli device {status | show | set | connect | reapply | modify | disconnect | delete | monitor | wifi | lldp} [ARGUMENTS…]

It display and manage network interfaces.

Check device status with nmcli command

It print status of devices. This is the default action if no command is specified to nmcli device.

nmcli device status
DEVICE          TYPE      STATE         CONNECTION 
wlp2s0          wifi      connected     Pilot 💻💻 
virbr0          bridge    disconnected  --         
p2p-dev-wlp2s0  wifi-p2p  disconnected  --         
enp1s0          ethernet  unavailable   --         
vboxnet0        ethernet  unmanaged     --         
vboxnet1        ethernet  unmanaged     --         
vboxnet2        ethernet  unmanaged     --         
lo              loopback  unmanaged     --         
virbr0-nic      tun       unmanaged     --

Show details about network devices using nmcli command

Show detailed information about devices. Without an argument, all devices are examined. To get information for a specific device, the interface name has to be provided.

nmcli device show
GENERAL.DEVICE:                         wlp2s0
GENERAL.TYPE:                           wifi
GENERAL.HWADDR:                         74:E5:43:4A:3B:97
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     Pilot 💻💻
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/8
IP4.ADDRESS[1]:                         192.168.43.196/24
IP4.GATEWAY:                            192.168.43.217
IP4.ROUTE[1]:                           dst = 0.0.0.0/0, nh = 192.168.43.217, mt = 600
IP4.ROUTE[2]:                           dst = 192.168.43.0/24, nh = 0.0.0.0, mt = 600
IP4.ROUTE[3]:                           dst = 169.254.0.0/16, nh = 0.0.0.0, mt = 1000
IP4.DNS[1]:                             192.168.43.217
IP6.ADDRESS[1]:                         fe80::f442:30f8:ad24:a53e/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 600
IP6.ROUTE[2]:                           dst = ff00::/8, nh = ::, mt = 256, table=255

GENERAL.DEVICE:                         virbr0
GENERAL.TYPE:                           bridge
GENERAL.HWADDR:                         52:54:00:55:5A:F5
GENERAL.MTU:                            1500
GENERAL.STATE:                          30 (disconnected)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --

Activate a device using nmcli command

Connect the device. NetworkManager will try to find a suitable connection that will be activated. It will also consider connections that are not set to auto connect.

nmcli device connect wlp2s0
sudo nmcli device connect wlp2s0
Device 'wlp2s0' successfully activated with 'eb055a3a-bb23-44ad-bc0d-7faecadcb223'.

List wifi connection using nmcli command

List available Wi-Fi access points. The ifname and bssid options can be used to list APs for a particular interface or with a specific BSSID, respectively.

nmcli device wifi list
IN-USE  BSSID              SSID            MODE   CHAN  RATE       SIGNAL  BARS  SECURITY 
        EE:8C:9A:7C:0C:82  HUAWEI Y5 lite  Infra  5     65 Mbit/s  100     ▂▄▆█  WPA2     
*       22:26:81:F6:C8:5E  Pilot 💻💻      Infra  13    65 Mbit/s  100     ▂▄▆█  WPA2

Connect to wifi using nmcli command

Connect to a Wi-Fi network specified by SSID or BSSID. The command finds a matching connection or creates one and then activates it on a device. This is a command-line counterpart of clicking an SSID in a GUI client. If a connection for the network already exists, it is possible to bring up (activate) the existing profile as follows: nmcli con up id name. Note that only open, WEP and WPA-PSK networks are supported if no previous connection exists. It is also assumed that IP configuration is obtained via DHCP.

sudo nmcli dev wifi connect Sudo password '4728711234'
Device 'wlp2s0' successfully activated with '847a8b8e-0ef7-4eb5-817d-41ee4bfe1359'.

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 Sudo
Password: ••••••••••
Device 'wlp2s0' successfully activated with '847a8b8e-0ef7-4eb5-817d-41ee4bfe1359'.

Show WiFi password using nmcli command

Show the details of the active Wi-Fi networks, including the secrets.

nmcli device wifi show-password
SSID: Sudo
Security: WPA
Password: 4728711234

  █████████████████████████████████
  ██ ▄▄▄▄▄ █▀█ █▄   ▀▀█▄██ ▄▄▄▄▄ ██
  ██ █   █ █▀▀▀█ ▄▀ █▀▀█ █ █   █ ██
  ██ █▄▄▄█ █▀ █▀▀██▄▄▀▀▄██ █▄▄▄█ ██
  ██▄▄▄▄▄▄▄█▄▀ ▀▄█ █ █ ▀▄█▄▄▄▄▄▄▄██
  ██▄ ▄ ▄▀▄ ▄▄▀▄▀▀▀▀█ ▄▀█▄▀▄▀▄▀ ███
  ██ ▀ █▄▀▄███▄█▀ ▄▄▄█ ▀█  ▄▀▄█▀▀██
  ██▄ ███▀▄▀█ ▄█▄█▄█▄▄▄ ▄██  ▄ ▀███
  ███▄██▄ ▄ █▀▄ ▄█▀▄▄▄███▄▀▄█▀▄████
  ██ ▀▄ ▀▄▄█▄▀▄▄▀▀▀█▄▄▄█ █▀▄▀ █ ▄██
  ██ █▀  ▄▄▀▀▄██▀ ▄▄██▄▀▄██▄ ███▀██
  ██▄█▄███▄█  ▀█▄█▄▄█▄▄  ▄▄▄ ▄ ████
  ██ ▄▄▄▄▄ █▄▀  ▄█▀ ▀█▄▄ █▄█ ▀▄▀▀██
  ██ █   █ █ █ ▄▀▀▀▄▄▄▄▀ ▄▄▄  █ ▀██
  ██ █▄▄▄█ █ ▄▀█▀ ▄▄▄▄ ▄ ▄▄▄  ▄ ███
  ██▄▄▄▄▄▄▄█▄▄▄█▄█▄▄▄▄▄▄█▄██▄▄█████
  █████████████████████████████████

This mark the end o f our tutorial on how to use nmcli command on Linux. You can now control NetworkManager and report network status in your Command Line.

Further Reading

man nmcli

Related Tutorials;

Compress and Uncompress Files with tar Command in Linux

Compress and Uncompress Files with zip Command in Linux

Configure Nginx Reverse Proxy for Kibana

How to Install Logstash on Debian

Leave a Comment