Solved: How to use nmcli for adding new IP and managing network in Redhat Linux

In this post we will see how to use nmcli for adding and configuring the IP. To do the same activity with nmuti refer this post.

"nmcli" is a command line interface. Benefit with both nmtui and nmcli is that you don't have to update the network configuration files manually. All the required changes are done by nmtui and nmcli themselves.
Good thing with nmcli is that you don't have to install any extra package like nmtui. nmcli is generally included with NetworkManager.
  • Let's create our new connection.
[root@cloudvedas ~]# nmcli con add type ethernet con-name cldvds-2 ifname enp0s3 ip4 192.168.99.103/24 gw4 192.168.99.1
Connection 'cldvds-2' (33de1c00-4ab4-4777-0a43-c004f0bd47ff) successfully added.
[root@cloudvedas ~]#
In the above example we are using these parameters:-
Connection name:- cldvds-2
Network Interface:- enp0s3
IP/Netmask:- 192.168.99.103/24
Gateway:- 192.168.99.1
  • Check in nmcli if our new connection is present.
[root@cloudvedas ~]# nmcli con show
NAME                 UUID                                 TYPE       DEVICE
System enp0s8    00cb8200-feb0-44b7-a378-3fdc720e0bc7 802-3-ethernet enp0s8
enp0s3           1a03478c-0307-4f23-a7fa-247ad74c37bf 802-3-ethernet --
cldvds-1         303ccf07-e77c-4770-b787-370407f73edc 802-3-ethernet enp0s3
cldvds-2         33de1c00-4ab4-4777-0a43-c004f0bd47ff 802-3-ethernet --
[root@cloudvedas ~]#
We can see the new connection cldvds-2 is created.
  • Activate the new connection
nmcli con up cldvds-2
  • If you are following up from the last post we will first deactivate our old connection cldvds-1 and then activate our new connection cldvds-2 .
[root@cloudvedas network-scripts]# nmcli con down cldvds-1 ; nmcli con up cldvds-2
  • Let's check if our ip got plumbed using command "ip addr"

Yes we can see the new IP up and running
  • As I said earlier "nmcli" will automatically update the config file. You can find the file in the directory "/etc/sysconfig/network-scripts" . Cross check the contents of the file
cd /etc/sysconfig/network-scripts
cat ifcfg-cldvds-2
You can also modify the connection properties. Let's say you want to add DNS server to the connection we created.
[root@cloudvedas ~]# nmcli con mod cldvds-2 ipv4.dns "192.168.99.254"
If you want to assign IP using DHCP you can create a new dhcp connection
[root@cloudvedas ~]# nmcli con add type ethernet con-name dhcp-1 ifname enp0s3
You can activate the new dhcp connection.(Be careful because if you are connected with the same interface which you are changing, you will lose the connection.)
nmcli con down cldvds-2 ; nmcli con up dhcp-1
Bonus:-
If you want to add an IPv6 IP to the interface you can do it with ip6 and gw6 options.
[root@cloudvedas ~]# nmcli con add type ethernet con-name cldvds-ip6 ifname enp0s3 ip4 192.168.99.104/24 gw4 192.168.99.1 ip6 fe80::cafe gw6 2001:db8::1
Connection 'cldvds-ip6' (83fb7f17-fc2f-424c-b446-33c929abcb56) successfully added.
Activate the connection
[root@cloudvedas ~]# nmcli con down cldvds-2 ; nmcli con up cldvds-ip6
If you no longer need a connection you can delete it after deactivating it.
[root@cloudvedas ~]# nmcli con down cldvds-ip6
[root@cloudvedas ~]# nmcli con delete cldvds-ip6
Hope this post is helpful to you. Do let me know if you have any query.

No comments:

Post a Comment