Configure Networking on CentOS 8 with nmcli Command (20 Examples)

nmcli is a command-line tool that can be used for controlling NetworkManager. This tool will help you to display network device status, create, edit, activate/deactivate, delete network connections and also troubleshoot networking in your Linux system. It is very useful for server and headless machine to control system-wide connections.

In this tutorial, we will show you how to use nmcli command to control network connections with examples.

Prerequisites

  • A server running CentOS 8.
  • A root password is setup on your server.

1. Check NetworkManager Status

To check whether the Network Manager is running or not using the following command:

nmcli -t -f RUNNING general

You should see the following output:

running

To get a general status, run the following command:

nmcli general

You should see the following output:

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

2. Check All Available Device

You can display all available device in your system, run the following command:

nmcli dev status

You should see the following output:

DEVICE  TYPE      STATE      CONNECTION  
eth0    ethernet  connected  System eth0 
eth1    ethernet  connected  System eth1 
lo      loopback  unmanaged  --      

3. Check Active Connection

To list all active connection in your system, run the following command:

nmcli con show

You should get the following output:

NAME         UUID                                  TYPE      DEVICE 
System eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  ethernet  eth0   
System eth1  9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04  ethernet  eth1

4. Display Information About Ethernet Connection

You can display the brief information about the ethernet connection using the following command:

nmcli con show "System eth0"

You should get the following output:

connection.id:                          System eth0
connection.uuid:                        5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
connection.stable-id:                   --
connection.type:                        802-3-ethernet
connection.interface-name:              eth0
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.autoconnect-retries:         -1 (default)
connection.multi-connect:               0 (default)
connection.auth-retries:                -1
connection.timestamp:                   1588217245
connection.read-only:                   no
connection.permissions:                 --
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.autoconnect-slaves:          -1 (default)
connection.secondaries:                 --
connection.gateway-ping-timeout:        0
connection.metered:                     unknown
cat /etc/sysconfig/network-scripts/ifcfg-eth1 | grep BOOTPROTO
BOOTPROTO=dhcp

5. Change “Automatically Connect” Directive

By default all ethernet connection will connect automatically. You can disable it with the following command:

nmcli con mod "System eth1" connection.autoconnect no

You can verify the changes with the following command:

cat /etc/sysconfig/network-scripts/ifcfg-eth1 | grep ONBOOT

You should see the following output:

ONBOOT=no

6. Change Network Connection BOOTPROTO Directive

You can change the ethernet connection BOOTPROTO directive from static to DHCP using the following command:

nmcli con mod "System eth1" ipv4.method auto

You can now verify the changes with the following command:

cat /etc/sysconfig/network-scripts/ifcfg-eth1 | grep BOOTPROTO

You should see the following output:

BOOTPROTO=dhcp

To change the ethernet connection BOOTPROTO directive static to DHCP to static using the following command:

nmcli con mod "System eth1" ipv4.method manual ipv4.address 192.168.0.10/24 ipv4.gateway 192.168.0.1

7. Disable IPv6 Address with nmcli

By default, both IPv6 and IPv4 connection is enabled in CentOS 8. You can disable the IPv6 connection wiht the following command:

nmcli con mod "System eth1" ipv6.method ignore

8. Add DNS Server to Existing Connection

To add a new DNS server to an existing connection with the following command:

nmcli con mod "System eth1" ipv4.dns 8.8.4.4

You can verify the changes with the following command:

cat /etc/sysconfig/network-scripts/ifcfg-eth1 | grep DNS

Output:

DNS1=8.8.4.4

You can also append a new DNS server using the +ipv4.dns option:

nmcli con mod "System eth1" +ipv4.dns 4.4.4.4

9. Remove DNS Server from Existing Connection

To remove the single DNS server from the connection, run the following command:

nmcli con mod "System eth1" -ipv4.dns 8.8.4.4

To remove the multiple DNS servers from the connection, run the following command:

nmcli con mod "System eth1" -ipv4.dns 8.8.4.4,8.8.2.2

10. Add/Edit Connection Interactively

You can also create a new connection or edit an existing connection using an interactive editor.

For example, edit an existing connection, run the following command:

nmcli con edit "System eth1"

You should see the following output:

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

Editing existing '802-3-ethernet' connection: 'System eth1'

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

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

Now, display an existing IP address, run the following command:

nmcli> print ipv4.address

Output:

ipv4.addresses: 192.168.0.10/32

To set a new IP address, run the following command:

nmcli> set ipv4.address 192.168.0.11

You can verify and save the connection with the following command:

nmcli> verify

Output:

Verify connection: OK
nmcli> save

Output:

Connection 'System eth1' (9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04) successfully updated.

You can now verify the saved connection with the following command:

cat /etc/sysconfig/network-scripts/ifcfg-eth1 | grep IPADDR

You should see the following output:

IPADDR=192.168.0.10
IPADDR1=192.168.0.11

11. Monitor Connection Activity

You can also monitor NetworkManager activity using nmcli like, changes in connection state, profiles, devices, etc.

After modifying the ethernet connection, run the following command to monitor it:

nmcli con monitor "System eth1"

12. Create a New Connection with Static IP

You can also create a new static ethernet connection with nmcli. For example, create a new ethernet connection named eth2, IP 192.168.0.12/24, Gateway 192.168.0.1, "onboot=yes" by running the following command:

nmcli con add con-name eth2 type ethernet ifname eth2 ipv4.method manual ipv4.address 192.168.0.15/24 ipv4.gateway 192.168.0.1

You should see the following output:

Connection 'eth2' (cefb3f7d-424c-42f8-b4e8-ed54e7dcb880) successfully added.

Now, verify the connection with the following command:

nmcli con

Output:

NAME         UUID                                  TYPE      DEVICE 
System eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  ethernet  eth0   
System eth1  9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04  ethernet  eth1   
eth2         cefb3f7d-424c-42f8-b4e8-ed54e7dcb880  ethernet  eth2 

13. Create a New Connection with DHCP

You can also create a new DHCP connection with nmcli. For example, create a new DHCP ethernet connection named eth3 with the following command:

nmcli con add con-name eth3 type ethernet ifname eth3 ipv4.method auto

You should see the following output:

Connection 'eth3' (ff54dbd6-255d-4935-abc8-73773bef5b55) successfully added.

14. Activate a New Connection

To activate the new ethernet connection eth2, run the following command:

nmcli con up eth2

You should see the following output:

Connection successfully activated

You can now verify the active connection with the following command:

nmcli con show --active

You should see the following output:

Output:

NAME         UUID                                  TYPE      DEVICE 
System eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  ethernet  eth0   
System eth1  9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04  ethernet  eth1   
eth2         cefb3f7d-424c-42f8-b4e8-ed54e7dcb880  ethernet  eth2 

15. Deactivate a Connection

To deactivate the connection eth2, run the following command:

nmcli con down eth2

16. Delete a Connection

You can also delete a specific ethernet connection with nmcli.

For example, to delete a connection eth2, run the following command:

nmcli con del eth2

You should see the following output:

Connection 'eth2' (cefb3f7d-424c-42f8-b4e8-ed54e7dcb880) successfully deleted.

17. Change Hostname with nmcli

To find the current hostname of your system, run the following command:

nmcli general hostname

You should see the following output:

centos8

Next, change the hostname from centos8 to linux using the following command:

nmcli general hostname linux

Next, verify the hostname with the following command:

nmcli general hostname

You should see the following output:

linux

18. Change the DEFROUTE Directive

The DEFROUTE directive is used to disable and enable the default gateway of your ethernet connection.

To enable the DEFROUTE directove for eth2 run the following command:

nmcli con mod "System eth2" ipv4.never-default yes

19. Restart Ethernet Connection

You can restart or reload your ethernet connection with the following command:

nmcli con reload

20. nmcli help

To get more information about nmcli command, run the following command:

nmcli --help

You should see the following output:

Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }

OPTIONS
  -a, --ask                                ask for missing parameters
  -c, --colors auto|yes|no                 whether to use colors in output
  -e, --escape yes|no                      escape columns separators in values
  -f, --fields <field,...>|all|common      specify fields to output
  -g, --get-values <field,...>|all|common  shortcut for -m tabular -t -f
  -h, --help                               print this help
  -m, --mode tabular|multiline             output mode
  -o, --overview                           overview mode
  -p, --pretty                             pretty output
  -s, --show-secrets                       allow displaying passwords
  -t, --terse                              terse output
  -v, --version                            show program version
  -w, --wait                      set timeout waiting for finishing operations

OBJECT
  g[eneral]       NetworkManager's general status and operations
  n[etworking]    overall networking control
  r[adio]         NetworkManager radio switches
  c[onnection]    NetworkManager's connections
  d[evice]        devices managed by NetworkManager
  a[gent]         NetworkManager secret agent or polkit agent
  m[onitor]       monitor NetworkManager changes

Conclusion

In the above guide, we learned how to use nmcli to manage and control ethernet connection in CentOS 8. I hope you can now easily add, edit or create a new connection with nmcli. Feel free to ask me if you have any questions.

Share this page:

Suggested articles

0 Comment(s)

Add comment