How to Configure nmcli Tool

To Configure and Manage Network Connections Using nmcli Tool

" nmcli" is the command line tool, used by both users and scripts for controlling the Network Manager. This article explains how to configure and manage network connections using nmcli tool.


To Check devices

Run the following command to check our devices.

[root@linuxhelp ~]# nmcli dev status
DEVICE          TYPE        STATE           CONNECTION
eno16777736     ethernet    connected       static1    
virbr0          bridge      disconnected    --      
virbr0-nic      ethernet    unavailable     --    
vmnet1          ethernet    unmanaged       --
vmnet8          ethernet    unmanaged       --        
lo              loopback    unmanaged       --         

We should set a “ connection profiles” for our devices to achieve network connectivity and perform their functions.

[root@linuxhelp ~]# cd /etc/sysconfig/network-scripts/
[root@linuxhelp network-scripts]# ls
ifcfg-lo          ifdown-ipv6      ifdown-tunnel   ifup-isdn      ifup-TeamPort
ifcfg-Myoffice1   ifdown-isdn      ifup            ifup-plip      ifup-tunnel
ifcfg-static1     ifdown-post      ifup-aliases    ifup-plusb     ifup-wireless
ifdown            ifdown-ppp       ifup-bnep       ifup-post      init.ipv6-global
ifdown-bnep       ifdown-routes    ifup-eth        ifup-ppp       network-functions
ifdown-eth        ifdown-sit       ifup-ib         ifup-routes    network-functions-ipv6
ifdown-ib         ifdown-Team      ifup-ippp       ifup-sit
ifdown-ippp       ifdown-TeamPort  ifup-ipv6       ifup-Team

The file-name starting with ifcfg- (interface configuration) are connection profiles. When we create a new connection or modify an existing one with nmcli or nmtui, the results are saved as connection profiles.

Here we are modifying, one with a DHCP configuration and another with static IP configuration.


Static IP configuration

[root@linuxhelp network-scripts]# cat ifcfg-static1


DHCP configuration

[root@linuxhelp network-scripts]# cat ifcfg-Myoffice1


Explanation

  • GATEWAY - the gateway IP.
  • DNS1, DNS2 - to notify the two dns servers .
  • ONBOOT - helps the connection up on booting the system.
  • DEVICE - the name of the network device which is associated with this profile.
  • BOOTPROTO - if it has value “ dhcp” then our connection profile takes dynamic IP from DHCP server
  • if it has value “ none” then it takes no dynamic IP and probably we assign a static IP.
  • IPADDR - the static IP that we assign to our profile.
  • PREFIX - the subnet mask.

Run the following command to check the connection.

[root@linuxhelp network-scripts]# nmcli connection show


The last column " device" shows us which connection is ' UP' .
Run the following command to check only the active devices.

[root@linuxhelp network-scripts]# nmcli connection show -a
NAME      UUID                                   TYPE              DEVICE
static1   d984b921-d708-4029-90a1-78b24078fbd1   802-3-ethernet    eno16777736

To check the IP Address, use " ip a" command.

[root@linuxhelp network-scripts]# ip a


Here, eno16777736 took the 192.168.7.56 IP from ethernet, because in the connection profile static1 which is up has a manual configuration.
To make a new ethernet connection with name Myhome1, assigned to device eno16777736

[root@linuxhelp network-scripts]# nmcli con add type ethernet con-name Myhome1 ifname eno16777736
Connection ' Myhome1'   (19a43cb0-699d-46a3-8ad3-aed336755879) successfully added.

To make a connection profile we must define type, ifname and con-name:
Now, Check its configuration by running the following command

[root@linuxhelp network-scripts]# cat ifcfg-Myhome1


You can note that BOOTPROTO=dhcp, as we didn' t give any static IP address.

We can modify any connection with the “ nmcli con mod“ command. However if you modify a DHCP connection and change it to " static" then you have to change its “ ipv4.method” from “ auto” to “ manual” . Otherwise you will get with two IP addresses: one from DHCP server and the static one.

Now lets make a new Ethernet connection profile with static2, which will be assigned to device eno16777736, with static IP 192.168.7.137, subnet mask 255.255.255.0=24 and gateway 192.168.1.1 . Then check its configuration.

[root@linuxhelp network-scripts]#nmcli con add type ethernet con-name static2 ifname eno16777736 ip4 192.168.7.137/24 gw4 192.168.7.1
 Connection ' static2'   (9ecfadc6-d0d6-437b-9b06-6aecd2baf0e5) successfully added.
[root@linuxhelp network-scripts]# cat ifcfg-static2


Now lets modify the last connection profile and add two dns servers.

[root@linuxhelp network-scripts]# nmcli con mod static2 ipv4.dns “ 8.8.8.8 8.8.4.4” 

When you add connections you use “ ip4” and “ gw4” , when you modify them you use “ ipv4” and “ gwv4” .
To bring up this connection profile.

[root@linuxhelp network-scripts]# nmcli con up static2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/39)

As you can see, the device eno16777736 has now IP address 192.168.7.137.

[root@linuxhelp network-scripts]# ip a


Type the following command with the connection name to get result of properties to be modified.

[root@linuxhelp network-scripts]# nmcli con show static2

When you bring down a connection profile, the Network Manager searches for another connection profile and brings it up automatically. If you don’ t want your connection profile to auto connect then run the following command

[root@linuxhelp network-scripts]# nmcli con mod static2 connection.autoconnect no

If you want to provide access only to specific user then type the following command.

[root@linuxhelp network-scripts]# nmcli connection modify static2 connection.permissions user1

To give permissions for more than one users, type user:user1,user2 without blank space between them.

[root@linuxhelp network-scripts]# nmcli con modify static2 connection.permissions user:user2,user3
[root@linuxhelp network-scripts]# nmcli con show static2


If you login as another user you can’ t bring “ UP” this connection profile.

[root@linuxhelp network-scripts]#su - abc
Last login: Wed Apr 27 08:59:14 EDT 2016 on pts/1
[abc@linuxhelp ~]$
[abc@linuxhelp ~]$ nmcli con up static2
Error: Connection ' static2'  does not exist.
[abc@linuxhelp ~]$ ls /etc/sysconfig/network-scripts

FAQ
Q
What is the nmcli?
A
nmcli stands for Network Manager Command Line Interface.
Q
How to set dns? Via command nmcli line?
A
nmcli con mod static2 ipv4.dns “8.8.8.8 8.8.4.4”
Q
Is there a way to change the existing IP address immediately from one command line
A
use nmcli editor by nmcli con mod net-eth0 ipv4.addresses "X.X.X.X" similarly set as manual
Q
Connection is always done after every reboot
A
Set nmcli connectio up interface name!!!
Q
Really good article!!! Very Useful!
A
Thank you for your appreciation