Technotes

Technotes for future me

Network NMCLI

Configuring Network Configuration with nmcli

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.

Check NetworkManager Status

nmcli general
nmcli -t -f RUNNING general

Check All Available Device

nmcli dev status

Check Active Connection

nmcli con show

Output:

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

Display Information About Ethernet Connection

nmcli con show "System eth0"

Change “Automatically Connect” Directive

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

nmcli con mod "System eth1" connection.autoconnect no

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

Disable IPv6 Address with nmcli

nmcli con mod "System eth1" ipv6.method ignore

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 also append a new DNS server using the +ipv4.dns option:

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

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

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

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"

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

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.

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:

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

Deactivate a Connection

To deactivate the connection eth2, run the following command:

nmcli con down eth2

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.

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:

```text
linux

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

Restart Ethernet Connection

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

nmcli con reload

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

Source: https://www.howtoforge.com/20-nmcli-command-examples-centos-8/

Last updated on 31 Jan 2021
Published on 12 Jun 2020
Edit on GitHub