Home Linux NetworkingEssential nmcli Command Examples To Manage Network Connections On Linux

Essential nmcli Command Examples To Manage Network Connections On Linux

A Complete Beginners Guide to nmcli Commands for Linux Network Management

By sk
2.4K views 14 mins read

Managing network connections on Linux through a terminal window might seem intimidating at first, but I've found nmcli to be one of those tools that becomes second nature once you get comfortable with its syntax.

This powerful command-line tool helps you configure and manage network connections on Linux systems easily and efficiently.

In this detailed, we will learn what nmcli is, its features, and nmcli commands usage with practical examples in Linux.

What is nmcli?

nmcli stands for NetworkManager Command Line Interface. It lets you:

  • View available network interfaces and their status
  • Connect to Wi-Fi or wired networks
  • Configure static IP addresses
  • Enable or disable networking and interfaces
  • Manage VPNs and other advanced settings

It’s lightweight, fast, and available on most modern Linux distributions by default.

nmcli is a key part of NetworkManager, which is the standard tool suite for managing networks on Linux.

Think of NetworkManager as the brain that handles all your network connections, from Wi-Fi to Ethernet and even mobile broadband. nmcli gives you direct control over this brain, all from your terminal.

Please note that nmcli is not exclusively used in Red Hat Enterprise Linux (RHEL).

It is a versatile, open-source command-line tool designed for controlling NetworkManager and reporting network status across a wide range of Linux systems

Advantages of nmcli

Many people find nmcli incredibly useful because:

  • No Graphical Interface Needed: You can use nmcli to control NetworkManager on servers, "headless" machines (those without screens), or simply from any terminal. You do not need a mouse or a fancy display.
  • Perfect for Scripts: Indeed, nmcli offers a special "terse output" format. This format displays only the essential values, making it super easy for computer scripts to read and process network information automatically.
  • Complete Control: With nmcli, you can create, display, edit, delete, activate, and deactivate network connections. You can also check and control the status of your network devices.

Understanding nmcli Command Syntax

Every nmcli command follows a simple structure:

nmcli [OPTIONS] OBJECT { COMMAND | help }
  • OPTIONS: These modify how the command works, such as changing the output style.
  • OBJECT: This tells nmcli what you want to manage. Common objects include:
    • general: For overall NetworkManager status.
    • networking: To turn networking on or off.
    • radio: To manage Wi-Fi or mobile broadband radios.
    • connection (or con for short): For managing saved network profiles.
    • device (or dev for short): For managing physical or virtual network hardware.

You can often use shorter versions (abbreviations) of the objects and commands. For example, nmcli connection help works the same as nmcli con help.

Essential nmcli Commands with Simple and Practical Examples

Let us explore some common tasks you can perform with nmcli.

1. Checking Your Network's Status

You can quickly get a snapshot of your network's health.

1.1. See NetworkManager's Overall Status

To see if NetworkManager is running and what its general status is, you can use:

nmcli general status

# Or the shorter version:

nmcli g s

This shows if your system is connected and has full internet access.

Sample Output:

STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN     METERED      
connected  full          missing  enabled  missing  enabled  no (guessed) 
Check Network Status using nmcli
Check Network Status using nmcli

For a simpler output suited for scripts, run:

nmcli -t -f STATE general

Output might look like:

Connected

1.2 Check All Network Devices

To list all network devices, whether they are managed by NetworkManager or not, run:

nmcli device status

# Or:

nmcli d s

This command shows all network interfaces and their current states. You will see devices like ens18 (Ethernet) or lo (loopback), along with their current state (e.g., connected, unmanaged).

Sample Output:

DEVICE  TYPE      STATE                   CONNECTION         
ens18   ethernet  connected               Wired connection 1 
lo      loopback  connected (externally)  lo                 
Check All Network Devices using nmcli
Check All Network Devices using nmcli

1.3. View All Saved Connections

NetworkManager stores connection details in profiles, which act like saved network configurations. You can create multiple profiles for the same interface – perhaps one for DHCP and another for static IP addressing.

To see a list of all your saved network connection profiles, type:

nmcli connection show

# Or:

nmcli con show

You can see your actual connections (not just devices). This displays their names, unique IDs (UUIDs), types, and which device they are linked to.

Sample Output:

NAME                UUID                                  TYPE      DEVICE 
Wired connection 1  2b89e468-7c07-48b0-a59a-5ebae1642386  ethernet  ens18  
lo                  5c293ac3-8bb7-45a9-98d7-cf8d80ea93c4  loopback  lo  

1.4. Show Only Active Connections

If you only want to see the connections currently in use, add the --active option:

nmcli connection show --active

# Or:

nmcli con show -a

This helps you quickly identify which profiles are actively managing your network devices.

1.5. Check Wi-Fi Radio Status

To see if your Wi-Fi radio is on or off, use:

nmcli radio wifi

Sample Output:

enabled

You can also turn it on or off with nmcli radio wifi on or nmcli radio wifi off.

2. Managing Network Connections

nmcli makes it easy to set up, change, activate, and remove network connections.

2.1. Create a New Ethernet Connection (Dynamic IP)

For a new Ethernet connection that gets its IP address automatically (DHCP):

nmcli connection add type ethernet con-name wired-connection ifname ens18

This creates a profile named wired-connection for the ens18 interface.

To start using it, activate it:

nmcli connection up wired-connection

You will then see the ens18 device connected to wired-connection.

If you want the connection name (profile name) to contain spaces, you must enclose the name in quotes. Without quotes, the shell will treat the spaces as separate arguments and the command will fail.

nmcli connection add type ethernet con-name "Office LAN Connection" ifname ens18

Once created, you can activate it like this:

nmcli connection up "Office LAN Connection"

And if you need to modify or delete it later, always quote the name.

2.2. Create a New Ethernet Connection (Static IP)

If you need to set a specific (static) IP address, gateway, and DNS servers:

nmcli connection add type ethernet con-name wired-network ifname ens18 ip4 192.168.1.13/24 gw4 192.168.1.101

This sets up a profile named wired-network with a static IPv4 address and gateway.

To add DNS servers to this profile:

nmcli connection modify wired-network ipv4.dns "8.8.8.8 8.8.4.4"

This replaces any existing IPv4 DNS servers.

To add more without replacing, use +ipv4.dns.

2.3. List Available Wi-Fi Networks

Before connecting to a WiFi network, you might want to see what Wi-Fi networks are nearby:

nmcli device wifi list

This command shows SSIDs, signal strength, and security types.

Sample Output:

IN-USE  BSSID              SSID              MODE   CHAN  RATE        SIGNAL  BARS  SECURITY 
*       F4:S7:C8:36:N2:46  Ostechnix_jio_4g  Infra  6     130 Mbit/s  100     ▂▄▆█  WPA2     
        N4:Z7:C6:96:B2:2A  Ostechnix_jio_5g  Infra  36    270 Mbit/s  100     ▂▄▆█  WPA2     
        9A:04:88:25:62:74  vivo Y39 5G       Infra  1     117 Mbit/s  54      ▂▄__  WPA2     
        D2:78:C9:C5:31:E1  Govindaraju       Infra  6     270 Mbit/s  52      ▂▄__  WPA2     

2.4. Connect to a Wi-Fi Network

To connect to a Wi-Fi network that requires a password:

nmcli device wifi connect "Your_WiFi_SSID" password "Your_Password"

If it is a hidden network, add hidden yes.

Example 1: For visible networks

nmcli device wifi connect MyWiFi password mysecretpass

Example 2: For hidden networks

nmcli device wifi connect MyHiddenWiFi password mysecretpass hidden yes

2.5. Modify an Existing Connection

You can change various properties of a saved connection. For instance, to change the mtu (Maximum Transmission Unit) for a Wi-Fi connection named MyConnection:

nmcli connection modify MyConnection 802-11-wireless.mtu 1350

NetworkManager refers to 802-11-wireless as the "setting" and mtu as its "property".

2.6. Activate a Connection

To make a saved connection (E.g. MyConnection) active on its assigned device:

nmcli connection up MyConnection

This command connects your device using the chosen profile.

2.7. Deactivate a Connection

To stop a specific connection from being active:

nmcli connection down MyConnection

Note that this command deactivates the connection but the device remains ready to connect. It will try to auto-activate another suitable connection.

2.8. Delete a Connection

If you no longer need a network profile, you can remove it:

nmcli connection delete "Old Profile Name"

Replace "Old Profile Name" with the actual profile name. This deletes the connection profile from NetworkManager.

2.9. Reload Connection Profiles

nmcli connection reload

3. Controlling Network Devices

Beyond connections, you can also manage the devices themselves.

3.1. Disconnect a Device

To disconnect a network device and stop it from automatically connecting again until you tell it to:

nmcli device disconnect ens18

This is different from connection down, as it truly prevents the device from re-connecting on its own.

3.2. Set a Device as Unmanaged

Sometimes, you might want NetworkManager to ignore a device, allowing you to manage it manually (e.g., with a script). To temporarily set a device as unmanaged:

nmcli device set enp18 managed no

This tells NetworkManager to leave enp18 alone.

3.3. Permanently Configure a Device as Unmanaged

For permanent changes, create a configuration file like /etc/NetworkManager/conf.d/99-unmanaged-devices.conf with the following content:

[keyfile]
unmanaged-devices=interface-name:enp18

To set multiple devices as unmanaged, separate the entries in the unmanaged-devices parameter with semicolon like below:

[keyfile]
unmanaged-devices=interface-name:enp18;interface-name:ens3

Finally, reload the NetworkManager service to take effect the changes:

sudo systemctl reload NetworkManager

You can verify the list of unmanaged devices using command:

nmcli device status

Sample Output:

DEVICE  TYPE      STATE      CONNECTION
enp1s0  ethernet  unmanaged  --

4. Advanced nmcli Commands Usage for Efficiency

nmcli offers useful options to tailor its output to your needs.

4.1. Terse Output (-t)

When writing scripts, you often want only the raw data without extra formatting. The -t option provides exactly that:

nmcli -t device

This makes parsing the output in scripts much simpler.

Sample Output:

ens18:ethernet:connected:Wired connection 1
lo:loopback:connected (externally):lo

4.2. Field Filtering (-f)

You can choose which specific pieces of information (fields) to display. This reduces clutter and helps you focus on what matters:

nmcli -f DEVICE,TYPE device

Sample Output:

DEVICE  TYPE     
ens18   ethernet 
lo      loopback 

Combine this with -t for script-friendly, specific data: nmcli -t -f DEVICE,TYPE device.

4.3. Pretty Output (-p)

For human readers, nmcli can make its output neat and aligned with headers. This is great for viewing information directly in the terminal:

nmcli -p device

This makes the information much easier to read at a glance.

Sample Output:

=====================
  Status of devices
=====================
DEVICE  TYPE      STATE                   CONNECTION         
-----------------------------------------------------------------------------------
ens18   ethernet  connected               Wired connection 1 
lo      loopback  connected (externally)  lo     

4.4. Interactive Editor

For more complex configurations or if you prefer a guided approach, nmcli has an interactive editor:

nmcli connection edit

It will ask you for the connection type and then lead you through setting various properties.

Valid connection types: 6lowpan, 802-11-olpc-mesh (olpc-mesh), 802-11-wireless (wifi), 802-3-ethernet (ethernet), adsl, bluetooth, bond, bridge, cdma, dummy, generic, gsm, hsr, infiniband, ip-tunnel, ipvlan, loopback, macsec, macvlan, ovs-bridge, ovs-interface, ovs-port, pppoe, team, tun, veth, vlan, vpn, vrf, vxlan, wifi-p2p, wimax, wireguard, wpan, bond-slave, bridge-slave, team-slave
Enter connection type: ethernet

===| nmcli interactive connection editor |===

Adding a new '802-3-ethernet' connection

Type 'help' or '?' for available commands.
Type 'print' to show all the connection properties.
Type 'describe [<setting>.<prop>]' for detailed property description.

You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, dcb, sriov, ethtool, match, ipv4, ipv6, hostname, link, tc, proxy
nmcli>
nmcli Interactive Editor
nmcli Interactive Editor

This is helpful for exploring options and ensuring correct syntax.

You can directly give the connection type (E.g. ethernet, wifi etc) like below:

nmcli connection edit type ethernet

For even more examples and detailed information, consider checking the nmcli manual page.

man nmcli

5. Checking Logs for Troubleshooting

When things break, you can check the logs using command:

nmcli general logging

You'll see the logging level and monitored domains, which can help pinpoint misconfigurations.

6. Setting Connection Permissions with nmcli

nmcli lets you restrict or grant permissions for specific Ethernet (or any) connection profiles. This feature is useful when multiple users share the same system but you want control over who can activate or modify a network profile.

By default, any user with access to NetworkManager can bring a connection up or down. But you can restrict this by assigning connection-specific permissions.

1. Allow a Single User to Manage a Connection

nmcli connection modify MyConnection connection.permissions user:senthil
  • MyConnection: The name (ID) of your connection profile.
  • connection.permissions: The property to define allowed users.
  • user:john: Grants permission only to user senthil.

2. Allow Multiple Users

If more than one user should manage the connection, separate usernames with commas (no spaces):

nmcli connection modify MyConnection connection.permissions user:senthil,kumar

This lets both senthil and kumar activate, deactivate, or modify the profile named MyConnection.

3. Remove All Permissions (Make It Global Again)

To remove restrictions and allow all users to manage the connection, clear the property:

nmcli connection modify MyConnection connection.permissions \"\"

4. Testing Permission Restrictions

If a user not listed in connection.permissions (E.g. satheesh) tries to bring up the connection:

nmcli connection up MyConnection

They’ll get a permission denied error, and the profile won’t activate. Because, the current user does not have permission to manage the MyConnection profile.

To resolve this, you have three options:

1. Switch to an allowed user (e.g., su senthil).

2. Add the current user to the profile:

nmcli connection modify MyConnection connection.permissions user:senthil.kumar,satheesh

3. Remove all restrictions:

nmcli connection modify MyConnection connection.permissions ""

7. Quick Reference Commands

Here are the commands I use most frequently:

  • nmcli device - Show device status
  • nmcli connection show - List all connections
  • nmcli device wifi list - Scan WiFi networks
  • nmcli connection up "name" - Activate connection
  • nmcli connection down "name" - Deactivate connection
  • nmcli connection delete "name" - Remove connection profile

nmcli Commands Cheatsheet

Here’s a practical nmcli commands cheatsheet that covers the most essential tasks.

1. General Network Status

PurposeCommand
Show overall statusnmcli general status
Show only connection statenmcli -t -f STATE general
Show NetworkManager versionnmcli --version

2. List and Manage Devices

PurposeCommand
List all network devicesnmcli device status
Disconnect a devicenmcli device disconnect eth0
Reconnect a devicenmcli device connect eth0
Scan available Wi-Fi networksnmcli device wifi list

3. Managing Connections (Profiles)

PurposeCommand
List all saved connectionsnmcli connection show
List only active connectionsnmcli connection show --active
Activate a connectionnmcli connection up my-office
Deactivate a connectionnmcli connection down my-office
Delete a connectionnmcli connection delete my-office

4. Connect to Wi-Fi

PurposeCommand
Connect to Wi-Fi using SSIDnmcli device wifi connect MyWiFi password mysecretpass
Connect to a hidden networknmcli device wifi connect HiddenSSID password mysecretpass hidden yes

5. Create a New Ethernet Connection

PurposeCommand
Add a DHCP-based connectionnmcli connection add type ethernet con-name my-office ifname ens3
Set a static IPbash\nnmcli connection modify my-office \\\n ipv4.addresses 192.168.1.100/24 \\\n ipv4.gateway 192.168.1.1 \\\n ipv4.dns 8.8.8.8,1.1.1.1 \\\n ipv4.method manual
Reactivate the modified connectionnmcli connection up my-office

6. Quick Abbreviations

Full CommandShort Form
nmcli connection shownmcli con show
nmcli connection upnmcli con up
nmcli device statusnmcli dev
nmcli general statusnmcli g

7. Troubleshooting Commands

PurposeCommand
Show NetworkManager logsnmcli general logging
Restart NetworkManagersudo systemctl restart NetworkManager
Reload connection profilesnmcli connection reload

Summary

The nmcli is truly a Swiss Army knife for Linux network management. It can handle everything from basic network status checks to complex connection setups for system administrators and power users.

By mastering nmcli commands usage, you can gain precise control over your network in Linux.

Related Read:

You May Also Like

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. By using this site, we will assume that you're OK with it. Accept Read More