Learning About nmcli (Nework Manager Command Line Interface)

0
3261

nmcli is a command-line tool for controlling NetworkManager and reporting network status. It can be utilized as a replacement for nm-applet or other graphical clients. nmcli is used to create, display, edit, delete, activate, and deactivate network connections, as well as control and display network device status.

Typical uses include:

Scripts: Utilize NetworkManager via nmcli instead of managing network connections manually. nmcli supports a terse output format which is better suited for script processing. Note that NetworkManager can also execute scripts, called “dispatcher scripts”, in response to network events.

Servers, headless machines, and terminals: nmcli can be used to control NetworkManager without a GUI, including creating, editing, starting and stopping network connections and viewing network status.

Install if not Installed

yum install NetworkManager -y

Start and enable Network Manager

systemctl start NetworkManager
systemctl enable NetworkManager

Lets use nmcli

“nmcli g status” to know overall status of NetworkManager.

[04:37][[email protected] ~]# nmcli g status
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN
connected  full          enabled  enabled  enabled  enabled

“nmcli g hostname” to fetch the hostname of the system

[04:24][[email protected] ~]# nmcli g hostname
mail.bullten.work

To change the hostname we will pass new hostname as parameter to the above argument i.e “nmcli g hostname mail2.bullten.work”

nmcli g hostname mail2.bullten.work

“nmcli dev status” to print the status of device

[04:41][[email protected] ~]# nmcli dev status
DEVICE  TYPE      STATE      CONNECTION
eth0    ethernet  connected  eth0
lo      loopback  unmanaged  --

“nmcli dev show” Shows detailed information about devices. Without an argument, all devices are examined. To get information for a specific device, the interface name has to be provided.

[04:45][[email protected] ~]# nmcli dev show
GENERAL.DEVICE:                         eth0
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         02:00:00:52:58:D0
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     eth0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         164.132.77.188/32
IP4.ADDRESS[2]:                         176.31.20.253/32
IP4.GATEWAY:                            217.182.175.254
IP4.ROUTE[1]:                           dst = 176.31.20.253/32, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]:                           dst = 164.132.77.188/32, nh = 0.0.0.0, mt = 100
IP4.ROUTE[3]:                           dst = 0.0.0.0/0, nh = 217.182.175.254, mt = 100
IP4.ROUTE[4]:                           dst = 217.182.175.254/32, nh = 0.0.0.0, mt = 100
IP4.DNS[1]:                             8.8.8.8
IP4.DNS[2]:                             8.8.4.4
IP6.ADDRESS[1]:                         fe80::1571:d892:f055:a4a8/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 100
IP6.ROUTE[2]:                           dst = ff00::/8, nh = ::, mt = 256, table=255

GENERAL.DEVICE:                         lo
GENERAL.TYPE:                           loopback
GENERAL.HWADDR:                         00:00:00:00:00:00
GENERAL.MTU:                            65536
GENERAL.STATE:                          10 (unmanaged)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
IP4.ADDRESS[1]:                         127.0.0.1/8
IP4.GATEWAY:                            --
IP6.ADDRESS[1]:                         ::1/128
IP6.GATEWAY:                            --

Now to get status of just specific device we will run “nmcli dev show eth0”

[04:46][[email protected] ~]# nmcli dev show eth0
GENERAL.DEVICE:                         eth0
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         02:00:00:52:58:D0
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     eth0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         164.132.77.188/32
IP4.ADDRESS[2]:                         176.31.20.253/32
IP4.GATEWAY:                            217.182.175.254
IP4.ROUTE[1]:                           dst = 176.31.20.253/32, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]:                           dst = 164.132.77.188/32, nh = 0.0.0.0, mt = 100
IP4.ROUTE[3]:                           dst = 0.0.0.0/0, nh = 217.182.175.254, mt = 100
IP4.ROUTE[4]:                           dst = 217.182.175.254/32, nh = 0.0.0.0, mt = 100
IP4.DNS[1]:                             8.8.8.8
IP4.DNS[2]:                             8.8.4.4
IP6.ADDRESS[1]:                         fe80::1571:d892:f055:a4a8/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 100
IP6.ROUTE[2]:                           dst = ff00::/8, nh = ::, mt = 256, table=255

“nmcli con show” list all the connections profile both active and inactive.

[04:47][[email protected] ~]# nmcli con show
NAME  UUID                                  TYPE      DEVICE
eth0  e7d20bc3-3d53-4a40-ace6-01f940112ce9  ethernet  eth0

If you want to show just active connection then you need to use “nmcli con show -a”

If you want to show full information of a network interface you can use command “nmcli con show eth0”

[04:56][[email protected] ~]# nmcli con show eth0
connection.id:                          eth0
connection.uuid:                        e7d20bc3-3d53-4a40-ace6-01f940112ce9
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:                   1586494531
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
connection.lldp:                        default
connection.mdns:                        -1 (default)
connection.llmnr:                       -1 (default)
802-3-ethernet.port:                    --
802-3-ethernet.speed:                   0
802-3-ethernet.duplex:                  --
802-3-ethernet.auto-negotiate:          no
802-3-ethernet.mac-address:             02:00:00:52:58:D0
802-3-ethernet.cloned-mac-address:      --
802-3-ethernet.generate-mac-address-mask:--
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:            --
802-3-ethernet.wake-on-lan:             default
802-3-ethernet.wake-on-lan-password:    --
ipv4.method:                            manual
ipv4.dns:                               8.8.8.8,8.8.4.4
ipv4.dns-search:                        --
ipv4.dns-options:                       ""
ipv4.dns-priority:                      0
ipv4.addresses:                         176.31.20.253/32
ipv4.gateway:                           217.182.175.254
ipv4.routes:                            { ip = 217.182.175.254/32 }
ipv4.route-metric:                      -1
ipv4.route-table:                       0 (unspec)
ipv4.routing-rules:                     --
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-timeout:                      0 (default)
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.dhcp-fqdn:                         --
ipv4.never-default:                     no
ipv4.may-fail:                          yes
ipv4.dad-timeout:                       -1 (default)
ipv6.method:                            auto
ipv6.dns:                               --
ipv6.dns-search:                        --
ipv6.dns-options:                       ""

Lets editeth0 interface using nmcli. We will be adding IP 164.132.77.188 with mac address 02:00:00:52:58:d0 and gateway 217.182.175.254

nmcli con mod eth0 type ethernet ip4 164.132.77.188 gw4 217.182.175.254 
nmcli con mod eth0 ipv4.dns "8.8.8.8,8.8.4.4"
nmcli con mod eth0 mac 02:00:00:52:58:d0
nmcli con mod eth0 con.autoconnect yes
nmcli con mod eth0 ipv4.method manual

Now we will bring the network eth1 up using “nmcli con up eth0”

nmcli con down eth0
nmcli con up eth0

Adding additional IP address using nmcli.

nmcli con mod eth0 +ipv4.addr 164.132.77.188

This will add additional IP to your pre existing network interface eth0. You can remove additional IP from interface using this below command

nmcli con mod eth0 -ipv4.addr 164.132.77.188

We can also add a new interface using

nmcli con add type ethernet con-name eth1 ifname eth0 ip4 164.132.77.188/32 gw4 217.182.175.254

To delete a network interface use the below command. We are deleting eth1 interface from connection profile.

nmcli con del eth1

This was a small tutorial on how to use nmcli.