Software >> OS >> Unix >> Linux >> RHEL >> 7 >>  nmcli command examples

 

 Example 1. Listing available Wi-Fi APs
 $ nmcli device wifi list

 

 Example 2. Showing general information and properties for a Wi-Fi interface
$ nmcli -p -f general,wifi-properties device show wlan0

 

 Example 3. Listing NetworkManager polkit permissions
$ nmcli general permissions

 

Example 4. Listing NetworkManager log level and domains
$ nmcli general logging

 

 Example 5. Changing NetworkManager logging
$ nmcli g log level DEBUG domains CORE,ETHER,IP
$ nmcli g log level INFO domains DEFAULT

 

Example 6. Adding a bonding master and two slave connection profiles
$ nmcli con add type bond ifname mybond0 mode active-backup
$ nmcli con add type ethernet ifname eth1 master mybond0
$ nmcli con add type ethernet ifname eth2 master mybond0

 

Example 7. Adding a team master and two slave connection profiles

$ nmcli con add type team con-name Team1 ifname Team1 config team1-master-json.conf
$ nmcli con add type ethernet con-name Team1-slave1 ifname em1 master Team1
$ nmcli con add type ethernet con-name Team1-slave2 ifname em2 master Team1

Activate the whole setup by activating both slaves:

  $ nmcli con up Team1-slave1
  $ nmcli con up Team1-slave2

 

 Example 8. Adding a bridge and two slave profiles
$ nmcli con add type bridge con-name TowerBridge ifname TowerBridge
$ nmcli con add type ethernet con-name br-slave-1 ifname ens3 master TowerBridge
$ nmcli con add type ethernet con-name br-slave-2 ifname ens4 master TowerBridge
$ nmcli con modify TowerBridge bridge.stp no

 

Example 9. Adding an ethernet connection profile with manual IP configuration
$ nmcli con add con-name my-con-em1 ifname em1 type ethernet \
  ip4 192.168.100.100/24 gw4 192.168.100.1 ip4 1.2.3.4 ip6 abbe::cafe

$ nmcli con mod my-con-em1 ipv4.dns "8.8.8.8 8.8.4.4"
$ nmcli con mod my-con-em1 +ipv4.dns 1.2.3.4
$ nmcli con mod my-con-em1 ipv6.dns "2001:4860:4860::8888 2001:4860:4860::8844"
$ nmcli -p con show my-con-em1

 

 

Example 10. remove the manual ip configuration for connection "my-con-em1"
## the quick way delete and re-add
$ nmcli con delete my-con-em1
$ nmcli con add con-name my-con-em1 ifname em1 type ethernet

 

Example 11. Escaping colon characters in tabular mode
$ nmcli -t -f general -e yes -m tab dev show eth0
GENERAL:eth0:ethernet:Intel Corporation:82567LM Gigabit Network Connection:
e1000e:2.1.4-k:1.8-3:00\:22\:68\:15\:29\:21:1500:100 (connected):0 (No reas
on given):/sys/devices/pci0000\:00/0000\:00\:19.0/net/eth0:eth0:yes:yes:no:
ethernet-13:89cbcbc6-dc85-456c-9c8b-bd828fee3917:/org/freedesktop/NetworkManager/ActiveConnection/9

 

 

Example 12. nmcli usage in a NetworkManager dispatcher script to make Ethernet and Wi-Fi mutually exclusive
#!/bin/bash
export LC_ALL=C

enable_disable_wifi ()
{
    result=$(nmcli dev | grep "ethernet" | grep -w "connected")
    if [ -n "$result" ]; then
        nmcli radio wifi off
    else
        nmcli radio wifi on
    fi
}

if [ "" = "up" ]; then
    enable_disable_wifi
fi

if [ "" = "down" ]; then
    enable_disable_wifi
fi
     

 

Example 13. Adding an ethernet connection profile in interactive editor (a)
$ nmcli connection edit type ethernet

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

Adding a new '802-3-ethernet' connection

Type 'help' or '?' for available commands.
Type 'describe [<setting>.<prop>]' for detailed property description.

You may edit the following settings: connection, 802-3-ethernet (ethernet),
802-1x, ipv4, ipv6, dcb
nmcli> print
===========================================================================
                          Connection details
===========================================================================

connection.id:                      ethernet-4
connection.uuid:                    de89cdeb-a3e1-4d53-8fa0-c22546c775f4
connection.interface-name:          --
connection.type:                    802-3-ethernet
connection.autoconnect:             yes
connection.autoconnect-priority:    0
connection.timestamp:               0
connection.read-only:               no
connection.permissions:
connection.zone:                    --
connection.master:                  --
connection.slave-type:              --
connection.secondaries:
connection.gateway-ping-timeout:    0
---------------------------------------------------------------------------
802-3-ethernet.port:                --
802-3-ethernet.speed:               0
802-3-ethernet.duplex:              --
802-3-ethernet.auto-negotiate:      yes
802-3-ethernet.mac-address:         --
802-3-ethernet.cloned-mac-address:  --
802-3-ethernet.mac-address-blacklist:
802-3-ethernet.mtu:                 auto
802-3-ethernet.s390-subchannels:
802-3-ethernet.s390-nettype:        --
802-3-ethernet.s390-options:
---------------------------------------------------------------------------

ipv4.method:                        auto
ipv4.dns:
ipv4.dns-search:
ipv4.addresses:
ipv4.gateway:                       --
ipv4.routes:
ipv4.route-metric:                  -1
ipv4.ignore-auto-routes:            no
ipv4.ignore-auto-dns:               no
ipv4.dhcp-client-id:                --
ipv4.dhcp-send-hostname:            yes
ipv4.dhcp-hostname:                 --
ipv4.never-default:                 no
ipv4.may-fail:                      yes
---------------------------------------------------------------------------
ipv6.method:                        auto
ipv6.dns:
ipv6.dns-search:
ipv6.addresses:
ipv6.gateway:                       --
ipv6.routes:
ipv6.route-metric:                  -1
ipv6.ignore-auto-routes:            no
ipv6.ignore-auto-dns:               no
ipv6.never-default:                 no
ipv6.may-fail:                      yes
ipv6.ip6-privacy:                   -1 (unknown)
ipv6.dhcp-hostname:                 --
---------------------------------------------------------------------------

nmcli> goto ethernet
You may edit the following properties: port, speed, duplex, auto-negotiate,
 mac-address, cloned-mac-address, mac-address-blacklist, mtu, s390-subchann
els, s390-nettype, s390-options
nmcli 802-3-ethernet> set mtu 1492
nmcli 802-3-ethernet> b
nmcli> goto ipv4.addresses
nmcli ipv4.addresses> desc

=== [addresses] ===
[NM property description]
Array of IP addresses.

[nmcli specific description]
Enter a list of IPv4 addresses formatted as:
  ip[/prefix], ip[/prefix],...
Missing prefix is regarded as prefix of 32.

Example: 192.168.1.5/24, 10.0.0.11/24

nmcli ipv4.addresses> set 192.168.1.100/24
Do you also want to set 'ipv4.method' to 'manual'? [yes]: yes
nmcli ipv4.addresses>
nmcli ipv4.addresses> print
addresses: 192.168.1.100/24
nmcli ipv4.addresses> back
nmcli ipv4> b
nmcli> set ipv4.gateway 192.168.1.1
nmcli> verify
Verify connection: OK
nmcli> print

===========================================================================
                          Connection details
===========================================================================
connection.id:                      ethernet-4
connection.uuid:                    de89cdeb-a3e1-4d53-8fa0-c22546c775f4
connection.interface-name:          --
connection.type:                    802-3-ethernet
connection.autoconnect:             yes
connection.autoconnect-priority:    0
connection.timestamp:               0
connection.read-only:               no
connection.permissions:
connection.zone:                    --
connection.master:                  --
connection.slave-type:              --
connection.secondaries:
connection.gateway-ping-timeout:    0
---------------------------------------------------------------------------
802-3-ethernet.port:                --
802-3-ethernet.speed:               0
802-3-ethernet.duplex:              --
802-3-ethernet.auto-negotiate:      yes
802-3-ethernet.mac-address:         --
802-3-ethernet.cloned-mac-address:  --
802-3-ethernet.mac-address-blacklist:
802-3-ethernet.mtu:                 1492
802-3-ethernet.s390-subchannels:
802-3-ethernet.s390-nettype:        --
802-3-ethernet.s390-options:

---------------------------------------------------------------------------
ipv4.method:                        manual
ipv4.dns:
ipv4.dns-search:
ipv4.addresses:                     192.168.1.100/24
ipv4.gateway:                       192.168.1.1
ipv4.routes:
ipv4.route-metric:                  -1
ipv4.ignore-auto-routes:            no
ipv4.ignore-auto-dns:               no
ipv4.dhcp-client-id:                --
ipv4.dhcp-send-hostname:            yes
ipv4.dhcp-hostname:                 --
ipv4.never-default:                 no
ipv4.may-fail:                      yes
---------------------------------------------------------------------------
ipv6.method:                        auto
ipv6.dns:
ipv6.dns-search:
ipv6.addresses:
ipv6.routes:
ipv6.route-metric:                  -1
ipv6.ignore-auto-routes:            no
ipv6.ignore-auto-dns:               no
ipv6.never-default:                 no
ipv6.may-fail:                      yes
ipv6.ip6-privacy:                   -1 (unknown)
ipv6.dhcp-hostname:                 --
---------------------------------------------------------------------------
nmcli> set ipv4.dns 8.8.8.8 8.8.4.4
nmcli> print
===========================================================================
                          Connection details
===========================================================================
connection.id:                      ethernet-4
connection.uuid:                    de89cdeb-a3e1-4d53-8fa0-c22546c775f4
connection.interface-name:          --
connection.type:                    802-3-ethernet
connection.autoconnect:             yes
connection.autoconnect-priority:    0
connection.timestamp:               0
connection.read-only:               no
connection.permissions:
connection.zone:                    --
connection.master:                  --
connection.slave-type:              --
connection.secondaries:
connection.gateway-ping-timeout:    0
---------------------------------------------------------------------------
802-3-ethernet.port:                --
802-3-ethernet.speed:               0
802-3-ethernet.duplex:              --
802-3-ethernet.auto-negotiate:      yes
802-3-ethernet.mac-address:         --
802-3-ethernet.cloned-mac-address:  --
802-3-ethernet.mac-address-blacklist:
802-3-ethernet.mtu:                 1492
802-3-ethernet.s390-subchannels:
802-3-ethernet.s390-nettype:        --
802-3-ethernet.s390-options:
---------------------------------------------------------------------------
ipv4.method:                        manual
ipv4.dns:                           8.8.8.8,8.8.4.4
ipv4.dns-search:
ipv4.addresses:                     192.168.1.100/24
ipv4.gateway:                       192.168.1.1
ipv4.routes:
ipv4.route-metric:                  -1
ipv4.ignore-auto-routes:            no
ipv4.ignore-auto-dns:               no
ipv4.dhcp-client-id:                --
ipv4.dhcp-send-hostname:            yes
ipv4.dhcp-hostname:                 --
ipv4.never-default:                 no
ipv4.may-fail:                      yes
---------------------------------------------------------------------------

ipv6.method:                        auto
ipv6.dns:
ipv6.dns-search:
ipv6.addresses:
ipv6.gateway:                       --
ipv6.routes:
ipv6.route-metric:                  -1
ipv6.ignore-auto-routes:            no
ipv6.ignore-auto-dns:               no
ipv6.never-default:                 no
ipv6.may-fail:                      yes
ipv6.ip6-privacy:                   -1 (unknown)
ipv6.dhcp-hostname:                 --
---------------------------------------------------------------------------
nmcli> verify
Verify connection: OK
nmcli> save
Connection 'ethernet-4' (de89cdeb-a3e1-4d53-8fa0-c22546c775f4) successfully
 saved.
nmcli> quit

 

Example - Activating a new connection non-interactively

## e.g. new network interface (enp0s9) added to the system

[root@rhel7mgmt1 ~]# nmcli device status
DEVICE      TYPE      STATE         CONNECTION
enp0s3      ethernet  connected     enp0s3    
virbr0      bridge    connected     virbr0    
enp0s8      ethernet  connected     enp0s8    

enp0s9      ethernet  disconnected  --        
lo          loopback  unmanaged     --        
virbr0-nic  tun       unmanaged     --


## activate the connection

[root@rhel7mgmt1 ~]# nmcli con add con-name enp0s9 type ethernet ifname enp0s9
Connection 'enp0s9' (155866d3-4ce7-4d71-8ca9-c6d709be6e03) successfully added.


## if there is valid DHCP on that interface's network, by default dynamic IP will be assigned.



## To assign Static IP

[root@rhel7mgmt1 ~]# nmcli con mod enp0s9 ipv4.addresses "192.168.1.30/24"

[root@rhel7mgmt1 ~]# nmcli con mod enp0s9 ipv4.method manual

[root@rhel7mgmt1 ~]# nmcli con show
NAME    UUID                                  TYPE      DEVICE
enp0s3  6ba6c0b1-ea4e-4dd5-aa19-cf8f69a10c07  ethernet  enp0s3
enp0s9  155866d3-4ce7-4d71-8ca9-c6d709be6e03  ethernet  enp0s9
enp0s8  a58f03fd-1707-4bd2-8e1d-767ec3b304f2  ethernet  enp0s8
virbr0  cae7ac83-8810-4f46-bc2a-9eb4e20834c3  bridge    virbr0