Detailed Explanation of the RedHat nmcli Tool in Linux IP

RedHat <strong><span>nmcli</span></strong> Tool Overview

1. Overview

<span>nmcli</span> is a command-line interface tool provided by NetworkManager for managing network connections in Linux systems. It supports configuration, management, monitoring, and debugging of network devices and connections. For Red Hat-based distributions (such as RHEL, CentOS, etc.), <span>nmcli</span> is the standard network management tool.

2. Installation and Version Check of <strong><span>nmcli</span></strong>

In most Red Hat-based systems, <span>nmcli</span> is installed by default. If manual installation is needed:

# Install NetworkManager
sudo yum install NetworkManager

Check the version of <span>nmcli</span>:

nmcli -v

Example output:

nmcli tool, version 1.26.0

3. Usage Modes of <strong><span>nmcli</span></strong>

<span>nmcli</span> supports two working modes:

  1. Command Line Mode:

  • Execute commands once, for example:<span>nmcli connection show</span>

  • Interactive Mode:

    nmcli
    • Enter the interactive environment of <span>nmcli</span> to execute multiple commands continuously.

    4. Basic Command Structure of <strong><span>nmcli</span></strong>

    nmcli [OPTIONS] OBJECT { COMMAND | help }
    • OBJECT: Specifies the object to operate on, such as <span>connection</span>, <span>device</span>, <span>general</span>, <span>radio</span>, <span>networking</span>, <span>agent</span>, <span>monitor</span>, etc.

    • COMMAND: Specifies the command to execute, such as <span>show</span>, <span>up</span>, <span>down</span>, <span>add</span>, <span>modify</span>, <span>delete</span>, etc.

    • OPTIONS: Global options, such as <span>-p</span> (pretty), <span>-t</span> (terse), etc.

    5. Common Commands Explained

    5.1. Check Network Status

    nmcli general status

    Output example:

    STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN
    connected  full          enabled  enabled  enabled  enabled

    5.2. Check Device Information

    nmcli device status

    Output example:

    DEVICE      TYPE      STATE      CONNECTION 
    enp0s3      ethernet  connected  Wired connection 1
    wlp2s0      wifi      disconnected  --
    lo          loopback  unmanaged  --

    5.3. View Current Connections

    nmcli connection show

    Output example:

    NAME              UUID                                  TYPE      DEVICE
    Wired connection 1  2c1f451d-ef44-4bd6-8b74-2b62774a9eb5  ethernet  enp0s3

    5.4. Create a New Connection (Static IP Configuration)

    nmcli connection add type ethernet con-name static-eth ifname enp0s3 ip4 192.168.1.10/24 gw4 192.168.1.1

    Configure DNS for the connection:

    nmcli connection modify static-eth ipv4.dns "8.8.8.8,8.8.4.4"

    Enable the connection:

    nmcli connection up static-eth

    5.5. Modify an Existing Connection

    For example, modify the static IP:

    nmcli connection modify static-eth ipv4.addresses 192.168.1.20/24

    Save and apply:

    nmcli connection up static-eth

    5.6. Delete a Connection

    nmcli connection delete static-eth

    5.7. Configure Wi-Fi Network

    Scan for Wi-Fi networks:

    nmcli device wifi list

    Connect to Wi-Fi:

    nmcli device wifi connect "SSID Name" password "Wi-Fi Password"

    5.8. Disconnect

    nmcli connection down <connection name or UUID>

    6. Relationship Between <strong><span>nmcli</span></strong> and NetworkManager Configuration Files

    In Red Hat-based systems (RHEL, CentOS, Fedora), NetworkManager manages network connections persistently through configuration files by default. The configuration files are usually located at:

    /etc/sysconfig/network-scripts/

    When configuring the network using <span>nmcli</span>, all changes will ultimately be reflected in these configuration files for saving or modification.

    6.1. Configuration File Structure and Naming Rules

    Each network interface’s configuration file is named in the following format:

    ifcfg-<interface name>

    For example:

    ifcfg-eth0
    ifcfg-enp0s3
    ifcfg-wlp2s0

    6.2. Configuration File Example

    For example, a configuration file named <span>ifcfg-enp0s3</span> has the following content:

    TYPE=Ethernet
    PROXY_METHOD=none
    BROWSER_ONLY=no
    BOOTPROTO=static
    DEFROUTE=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=no
    NAME=enp0s3
    DEVICE=enp0s3
    ONBOOT=yes
    IPADDR=192.168.1.10
    PREFIX=24
    GATEWAY=192.168.1.1
    DNS1=8.8.8.8
    DNS2=8.8.4.4
    Configuration Item Explanation:
    • <span>TYPE=Ethernet</span>: Network type, common types include Ethernet, Wireless, etc.

    • <span>BOOTPROTO=static</span>: Use a static IP address. If using DHCP, set to <span>dhcp</span>.

    • <span>ONBOOT=yes</span>: Whether to activate this interface automatically at system startup.

    • <span>IPADDR</span>: Static IP address.

    • <span>PREFIX</span>: Number of bits for the subnet mask (24 corresponds to 255.255.255.0).

    • <span>GATEWAY</span>: Default gateway.

    • <span>DNS1</span> / <span>DNS2</span>: DNS server addresses.

    6.3. Relationship Between <strong><span>nmcli</span></strong> and Configuration Files

    When we use <span>nmcli</span> to configure network connections, NetworkManager automatically generates or modifies the corresponding <span>ifcfg-<interface name></span> file under <span>/etc/sysconfig/network-scripts/</span>. For example:

    nmcli connection add type ethernet con-name enp0s3 ifname enp0s3 ip4 192.168.1.10/24 gw4 192.168.1.1

    The generated configuration file <span>/etc/sysconfig/network-scripts/ifcfg-enp0s3</span> is similar to:

    TYPE=Ethernet
    PROXY_METHOD=none
    BROWSER_ONLY=no
    BOOTPROTO=none
    DEFROUTE=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=no
    NAME=enp0s3
    DEVICE=enp0s3
    ONBOOT=yes
    IPADDR=192.168.1.10
    PREFIX=24
    GATEWAY=192.168.1.1

    6.4. Principle of Modifying Configuration Files with <strong><span>nmcli</span></strong>

    When we modify network configurations using <span>nmcli</span>, for example:

    nmcli connection modify enp0s3 ipv4.addresses 192.168.1.20/24
    nmcli connection modify enp0s3 ipv4.gateway 192.168.1.254
    nmcli connection modify enp0s3 ipv4.dns "8.8.8.8, 8.8.4.4"
    nmcli connection up enp0s3

    NetworkManager will automatically update the corresponding configuration file <span>ifcfg-enp0s3</span>, with content similar to:

    TYPE=Ethernet
    PROXY_METHOD=none
    BROWSER_ONLY=no
    BOOTPROTO=none
    DEFROUTE=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=no
    NAME=enp0s3
    DEVICE=enp0s3
    ONBOOT=yes
    IPADDR=192.168.1.20
    PREFIX=24
    GATEWAY=192.168.1.254
    DNS1=8.8.8.8
    DNS2=8.8.4.4

    6.5. Manual Modification of Configuration Files and Compatibility with nmcli

    NetworkManager does not require all configurations to be done through <span>nmcli</span>. You can also manually edit the configuration files under <span>/etc/sysconfig/network-scripts/</span> and then reload them with the following command:

    nmcli connection reload

    Or restart the NetworkManager service:

    systemctl restart NetworkManager

    6.6. Comparison of nmcli and Configuration Files: Advantages and Disadvantages

    Operation Method Advantages Disadvantages
    <span>nmcli</span> Simple operation, easy to script, real-time updates to configuration files Not intuitive enough for beginners
    Manual modification of configuration files More intuitive, conforms to traditional Unix style, allows for detailed configuration Requires manual service restart after modification
    <span>nmtui</span> Graphical terminal interface, easy to use Less detailed functionality than <span>nmcli</span>
    <span>/etc/sysconfig/</span> Configuration Configuration files are easy to back up, migrate, and change Prone to errors when configurations are complex

    7. Comparison of Using nmcli with Other Tools

    Function <strong><span>nmcli</span></strong> <strong><span>nmtui</span></strong> <strong><span>ifconfig</span></strong> / <strong><span>ip</span></strong>
    Interface Type Command line Graphical terminal interface Pure command line
    Supports Static IP Configuration ✔️ ✔️ ✔️
    Supports DHCP Configuration ✔️ ✔️ ✔️
    Supports Wi-Fi Networks ✔️ ✔️ ❌ (Traditional tools do not support)
    Ease of Use Medium Simple Relatively difficult
    Configuration Persistence ✔️ (Saves configuration) ✔️ (Saves configuration) ❌ (Temporary configuration)
    System Integration High High Low
    Network Debugging Support ✔️ (More comprehensive) ✔️

    8. Related Tool Comparison

    1. <strong><span>nmtui</span></strong> (NetworkManager TUI):

    • Graphical terminal tool that provides an easy menu interface for users to configure networks.

    • Suitable for quick setups or users unfamiliar with the command line.

  • <strong><span>ifconfig</span></strong> / <strong><span>ip</span></strong> Tools:

    • Traditional network management tool (<span>ifconfig</span>) and its modern enhanced version (<span>ip</span>).

    • Does not support complex management of NetworkManager, only suitable for simple interface configurations.

    • Does not support Wi-Fi network configuration.

  • <strong><span>systemctl restart NetworkManager</span></strong>:

    • Applies configuration changes by restarting the NetworkManager service.

    • <span>systemctl</span> is a tool for managing Linux services, applicable to all modern Linux distributions.

    9. Conclusion

    • In Red Hat systems, NetworkManager is the default network management tool, and <span>nmcli</span> is its core command-line tool.

    • <span>nmcli</span> is the most recommended network management tool in Red Hat-based systems, especially suitable for server environments. Configuring networks through <span>nmcli</span> enables real-time updates and configuration persistence, which is the recommended management method by Red Hat.

    • All connections created or modified through <span>nmcli</span> will be reflected in the configuration files under <span>/etc/sysconfig/network-scripts/</span>.

    • Modifications to configuration files can be done through <span>nmcli</span> or manually editing, depending on the user’s habits and needs.

    • Compared to <span>nmtui</span> and <span>ifconfig</span>, <span>nmcli</span> provides more detailed and powerful network management features.

    • In comparison to the <span>ip</span> tool, <span>nmcli</span> offers higher integration and configuration persistence, and provides comprehensive support for Wi-Fi networks.

    • Mastering <span>nmcli</span> is an essential skill for efficient network configuration and management in a Red Hat environment.

    Leave a Comment