logoCndocs
Network Interface and Configuration

NMCLI Command in Linux

Introduction

The nmcli (NetworkManager Command-Line Interface) is a powerful command-line tool for controlling NetworkManager and managing network connections in Linux systems. It provides a comprehensive interface for viewing, configuring, and troubleshooting network settings without requiring a graphical user interface.

NetworkManager is a daemon that manages network connections and devices, automatically connecting to available networks based on user preferences. The nmcli command serves as the primary command-line interface to interact with NetworkManager, making it essential for server administrators, network engineers, and users working in terminal environments.

Basic Syntax

The general syntax for the nmcli command is:

nmcli [OPTIONS] OBJECT { COMMAND | help }

Where:

  • OPTIONS: Additional parameters that modify the behavior of nmcli
  • OBJECT: The target of the operation (e.g., connection, device, etc.)
  • COMMAND: The action to be performed on the specified object
  • help: Displays help information for the specified object

Main Objects in NMCLI

The nmcli command works with several objects, each representing a different aspect of network configuration:

ObjectDescriptionShorthand
generalNetworkManager's general status and operationsg
networkingOverall networking controln
radioNetworkManager radio switchesr
connectionNetworkManager's connectionsc
deviceNetwork devices managed by NetworkManagerd
agentNetworkManager secret agent or polkit agent-
monitorMonitor NetworkManager changes-

Practical Examples

1. Viewing Network Connections

To list all available network connections on your system:

nmcli connection show

This command displays details such as connection name, UUID, type, and device.

NMCLI Connection Show Output

For a specific connection:

nmcli connection show "connection_name"

2. Checking Device Status

To view the status of all network devices:

nmcli device status

This command shows whether each device is connected or disconnected, along with the device type and connection it's associated with.

For detailed information about a specific device:

nmcli device show eth0

Replace eth0 with your actual device name.

3. Managing Network Connections

Creating a New Connection

To create a new Ethernet connection:

nmcli connection add type ethernet ifname eth0 con-name "My Ethernet Connection"

To create a new Wi-Fi connection:

nmcli connection add type wifi ifname wlan0 con-name "My WiFi" ssid "NetworkName"

Connecting to a Network

To connect to a saved network:

nmcli connection up "connection_name"

To connect to a new Wi-Fi network:

nmcli device wifi connect "SSID" password "password"

Disconnecting from a Network

nmcli connection down "connection_name"

4. Modifying Connection Settings

To modify connection settings:

nmcli connection modify "connection_name" setting.property value

For example, to set a static IP address:

nmcli connection modify "My Ethernet Connection" ipv4.addresses "192.168.1.100/24" ipv4.gateway "192.168.1.1" ipv4.method manual

To set DNS servers:

nmcli connection modify "My Ethernet Connection" ipv4.dns "8.8.8.8 8.8.4.4"

5. Managing Wi-Fi Networks

To scan for available Wi-Fi networks:

nmcli device wifi list

To connect to a Wi-Fi network:

nmcli device wifi connect "SSID" password "password"

Advanced Usage

1. Creating a Bridge Connection

nmcli connection add type bridge con-name "Bridge Connection" ifname br0
nmcli connection add type ethernet slave-type bridge con-name "Bridge Port 1" ifname eth0 master br0

2. Setting Up a VPN Connection

nmcli connection add type vpn con-name "My VPN" vpn-type openvpn -- ifname tun0 vpn.data "file=/path/to/config.ovpn"

3. Creating a Bond Connection

nmcli connection add type bond con-name "Bond Connection" ifname bond0 bond.options "mode=active-backup,miimon=100"
nmcli connection add type ethernet slave-type bond con-name "Bond Port 1" ifname eth0 master bond0
nmcli connection add type ethernet slave-type bond con-name "Bond Port 2" ifname eth1 master bond0

Command Options

The nmcli command supports several options to modify its behavior:

OptionDescriptionExample
-t, --terseTerse output suitable for script processingnmcli -t device status
-p, --prettyPretty output for better human readabilitynmcli -p device status
-m, --modeOutput mode: tabular or multilinenmcli -m tabular device status
-f, --fieldsSpecify fields to displaynmcli -f DEVICE,STATE device status
-e, --escapeEscape column separators in valuesnmcli -e yes device status
-v, --versionShow version informationnmcli -v
-h, --helpShow help informationnmcli -h

Examples of Options

Terse Output

nmcli -t device status

This produces output with fields separated by colons, suitable for parsing in scripts.

Pretty Output

nmcli -p device status

This formats the output in a more human-readable way with proper alignment and borders.

Specific Fields

nmcli -f DEVICE,STATE,CONNECTION device status

This displays only the specified fields (device name, state, and connection).

Troubleshooting Network Issues

1. Checking NetworkManager Status

nmcli general status

2. Restarting NetworkManager

sudo systemctl restart NetworkManager

3. Reloading a Connection

nmcli connection reload

4. Monitoring Network Changes

nmcli monitor

This command continuously monitors and displays NetworkManager activity.

Best Practices

  1. Use Connection Names: Always use descriptive connection names for easier management.

  2. Check Before Modifying: Always check the current settings before making modifications:

    nmcli connection show "connection_name"
  3. Save Changes: After modifying a connection, remember to bring it down and up again to apply changes:

    nmcli connection down "connection_name"
    nmcli connection up "connection_name"
  4. Use Tab Completion: The nmcli command supports tab completion, which can help you discover available options and commands.

  5. Script Automation: Use the terse output mode (-t) when incorporating nmcli commands in scripts for easier parsing.

Conclusion

The nmcli command is an essential tool for managing network connections in Linux environments. Its comprehensive feature set allows for complete control over NetworkManager from the command line, making it invaluable for server administration, automation, and troubleshooting network issues.

Whether you're configuring a simple home network or managing complex enterprise networking environments, mastering nmcli provides you with the flexibility and power needed to efficiently manage network connections in Linux systems.

Test Your Knowledge

Take a quiz to reinforce what you've learned

Exam Preparation

Access short and long answer questions for written exams

Share this page