Open In App

Manage Network Connections From the Linux Command Line with nmcli

Last Updated : 11 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Network connectivity is a critical aspect of modern computing, and Linux provides a robust command-line tool for managing network connections: nmcli. Network Manager Command-Line Interface (nmcli) is a powerful and versatile utility that allows users to control and configure network settings directly from the terminal. In this article, we will delve into the syntax, usage, and various capabilities of nmcli for efficient network management on Linux.

What is nmcli

nmcli is a command-line tool which is used for controlling NetworkManager. nmcli command can also be used to display network device status, create, edit, activate/deactivate, and delete network connections. 

Typical Uses:  

  • Scripts: Instead of manually managing the network connections it utilize NetworkManager via nmcli.
  • Servers, headless machine and terminals: Can be used to control NetworkManager with no GUI and control system-wide connections.

Syntax of nmcli

The nmcli command follows a specific syntax, enabling users to interact with Network Manager through the command line. The general syntax for nmcli is as follows: 

nmcli [OPTIONS] OBJECT { COMMAND | help }

Here,

  • OPTIONS: Additional options that modify the behavior of nmcli.
  • OBJECT: Specifies the target of the operation (e.g., connection, device, etc.).
  • COMMAND: Defines the action to be performed on the specified object.

Where the OBJECT can be any one of the following:  

  • nm: NetworkManager’s status.
  • connection/cn: NetworkManager’s connection.
  • d[evice]: devices managed by NetworkManager.

Pratical Implementation of nmcli Command in linux

1. How to View Connections using nmcli

nmcli connection show

This command lists all the available network connections on your system. It provides details such as the connection name, UUID, device, type, and status.

Viewing connections

Viewing connections

2. How to Check the Device Status using nmcli

To check the device status using nmcli command. 

nmcli dev status
check device status

check device status

This command displays the current status of all network devices on your system. It shows whether each device is connected or disconnected, along with additional information like the device type and the connection type it is associated with.The output might vary with different machines. 

3. How to Display Device Details using nmcli

Syntax:

nmcli device show <device_name>

Example:

nmcli device show eno1

Replace “eno1” with the actual name of your network device. This command provides detailed information about the specified network device, including its IP address, MAC address, driver details, and more.

Display Device Details

Display Device Details

4. How to Add a New Ethernet Conncection using nmcli

To add a new connection, you can use the following command:

Syntax:

nmcli connection add type <connection_type> ifname <interface_name> con-name <connection_name>

Here,

  • connection_type: Specifies the type of connection (e.g., ethernet, wifi).
  • interface_name: The name of the network interface.
  • connection_name: A user-defined name for the new connection.

Example:

nmcli connection add type ethernet ifname eth1 con-name my_eth_connection

This command creates a new Ethernet connection named “my_eth_connection” associated with the network interface “eth1”. Adjust the interface name and connection name as needed.

202Adding new ethernet connection4-01-11_17-11

Adding new ethernet connection

To verify we can use this command “nmcli connection show”

nmcli connection show

nmcli connection show

5. How to Adjust Connection Settings using nmcli

To modify connection settings, you can use the following command:

Syntax:

nmcli connection modify <connection_name> <setting_name> <setting_value>

Here,

  • connection_name: The name of the connection to be modified.
  • setting_name: The specific setting to be adjusted.
  • setting_value: The new value for the setting.

Example:

nmcli connection modify my_eth_connection ipv4.addresses "192.168.1.2/24" ipv4.gateway "192.168.1.1"

This example modifies the IPv4 settings of the “my_eth_connection” connection, setting a static IP address of “192.168.1.2” with a subnet mask of “/24” and a gateway of “192.168.1.1”.

Options Available in nmcli Command in Linux

Options

Description

-t, –terse

Terse output suitable for script processing.

-p, –pretty

Prints organized and human-readable output.

-m, –mode

Switches between tabular and multiline output. Defaults to tabular if not specified.

-f, –fields

Specifies fields to print as output. Use ‘all’ for all fields or ‘common’ for common fields.

-e, –escape

Escapes column separators in values.

-v, –version

Displays version information.

-h, –help

Prints help-related information.

1. -t, –terse option

This option is used to terse the output i.e. when we want the output to be very brief and in very few words. It is suitable for script processing. 

Example: 

nmcli -t device list

 Input: 

Output: 

2. -p, –pretty option

This option prints the output in an organized format which is convenient and easily readable to humans. 

Example: 

Input: 

Output: 

3. -m, –mode {tabular|multiline} option

Used to switch the output mode between tabular and multiline. If the option is not used, nmcli will print the output in tabular form by default.

4. -f, –fields {fields1, fields2…. |all|common} option

This option is used to specify the fields to print as output. Where the field is the column that we want to print as output. all is used when we want all the value field to be displayed. 

To print device list with field GENERAL. 
 

5. -v, –version option

Show the version information version. 

Example: 

6. -help option

Print help related information. 

Example: 

Conclusion

In this article we discussed nmcli which is a powerful command-line tool for managing network connections in Linux. With a structured syntax, it enables users to control and configure network settings efficiently. This article covered practical examples, including viewing connections, checking device status, displaying device details, adding new Ethernet connections, and adjusting settings. Key options such as terse, pretty, mode, fields, escape, version, and help were highlighted, showcasing the versatility of nmcli for diverse network administration tasks. Whether used in scripts or on headless machines, nmcli proves essential for streamlined and efficient network connectivity on Linux systems.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads