How To Manage Network Connection Using ‘nmcli’ Tool?

In this article, I will tell you how we can manage network connections in Linux. So in this article, we will learn to use nmcli tool. It is a NetworkManager Tool.  It can be used to display network device status, create, edit, activate/deactivate, and delete network connections. So it is a very important tool that helps us to save time. Because we can manage the network connections/profiles directly with the nmcli tool without accessing the GUI NetworkManager. The commands are very short and so simple. So if a beginner tries to managing the NetworkManager Connections/profiles then it wouldn’t be difficult to manage any network with nmcli tool. 

As Linux Administrators, many admins use the other tools for network management like NetworkManager, GNOME graphical interface, or the alternate of nmcli that is nmtui which is also a graphical mode of nmcli. Every Linux user has a different choice to manage the network profile depending upon the user’s preferences. But in this article, I will particularly focus on nmcli tool.  Now let’s start –

  • In the CentOS and RHEL Operating systems, the default folder for all network connections is located at /etc/sysconfig/network-scripts/.  Here you will find all of your network connection information whether it is wired or wireless.
  • In the Network-script directory, every file starting with ‘ifcfg’ is a configuration file of a device.
  • Every network device has a separate configuration file.

Network Management With ‘NMCLI Tool’

 

The usage syntx of NMCLI is 

[Linux-Kings@Server ~]#  nmcli [options] Object { Command | help }

You can check simply by typing nmcli in the Terminal or you can read man pages of nmcli.

 

 

These are the options available with the nmcli tool that we can use with NetworkManager.

When you will hit the double tab after putting any option listed above you will get some more options that you can use. So when you don’t know how to use a command then you should press the tab button twice then it will list you, available options like what i did with dev. Here dev is the abbreviation of the device and you can use much shorter of it like ‘nmcli d’

1.Check connected device with nmcli

[Linux-Kings@Server ~]# nmcli con show

2. To check the active connection

[Linux-Kings@Server ~]# nmcli con

3. Check the status of devices with nmcli

[Linux-Kings@Server ~]# nmcli dev status

4. To create a new connection profile and this profile config file.

[Linux-Kings@Server ~]# nmcli con add type ethernet con-name office ifname enp0s3

i. con = connections  ii. Add = adding a  new connection  iii. Type = type of connection wired or wireless in my case it is ethernet iv. con-name = connection name that you want to give, in my case, it is office. v. ifname = interface name, your interface name may be the same or a different one so please check, in my case, it is enp0s3.

[Linux-Kings@Server ~]#ll  cat /etc/sysconfig/network-scripts/ifcgf-office

5.To Verify, check office network profile in the Network-script directory.

[Linux-Kings@Server ~]#ll  /etc/sysconfig/network-scripts/

6. Enable/disable a connection. 

[Linux-Kings@Server ~]# nmcli con down/up connection_name

7. Assign Static IP address to a new connection And Check its profile.

[Linux-Kings@Server ~]# nmcli con add type ethernet con-name office1 ifname enp0s3 ip4  192.168.1.10/24 gw4 192.168.1.1
[Linux-Kings@Server ~]# cat /etc/sysconfig/network-scripts/ifcfg-office1

 

Note: When we add a new connection, by default it gets an IP address from DHCP.So if you have seen the config file of any network in network-scripts directory then you surely noticed this. But sometimes we need to give a static IP address. For this we have 2 options, 1 is we can add a new connection with a static Ip address and 2 is we can modify an existing connection.

8. Modify an existing profile by giving a new static IP address and gateway address.

[Linux-Kings@Server ~]# nmcli con mod office ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1

Note: When adding a new connection we use ‘ip4‘ for the IP address of version 4 and ‘gw4’ for Gateway IP of version 4. But during modification of a connection, we use ‘ipv4.addresses’ instead of ip4 and for the gateway, we use ipv4.gateway’ instead of putting gw4 directly.

9. Check the configuration of this profile.

[Linux-Kings@Server ~]# cat /etc/sysconfig/network-scripts/ifcfg-office

see above image

10. Enable/Disable Internet connectivity.

[Linux-Kings@Server ~]#nmcli networking on/off

11. To check the connectivity of the system.

[Linux-Kings@Server ~]#nmcli networking 

12. Modify the Permission of connection for a particular user.

[Linux-Kings@Server ~]# nmcli con mod home connection.permissions user_name

13. Modify permission of connection for multiple users.

[Linux-Kings@Server ~]# nmcli con mod home connection.permissions user:user1,user2.....

14. To Disable the auto-connect status of a connection.

[Linux-Kings@Server ~]# nmcli con mod home connection.autoconnect no

 Note :  By default, the new connection has permission to auto-connect. This means if we disconnect/disable a running network then other networks may get auto-connect to provide you connectivity. sometimes we don’t need such config during that time we may disable this permission. 

15. Set hostname using nmcli.

[Linux-Kings@Server ~]# nmcli general hostname host_name

So Friends this all about what is ‘nmcli’ NetworkManager Tool and  how to use ‘NMCLI Tool?’ Friends, if you have doubts or any queries about this post then don’t hesitate to contact me. If you like my posts please share them with your friends.

Leave a comment