DEV Community

Faaiq23
Faaiq23

Posted on • Updated on

How to use nmcli command?

Computer network refers to computing devices that are interconnected and can exchange data and share resources with each other. These network devices use a system of rules, known as communication protocols, to transmit information over physical or wireless technologies.

One of the requirements of a computer network is that there is a computer that can run the operating system properly without any problems. This time I will discuss how to configure a network on a RHEL 8-based computer. As we know, computers with Linux-based OS are famous for their CLI. A command-line interface (CLI) is a text-based user interface (UI) used to run programs, manage computer files and interact with the computer. So now I will explain how to managing and configuring network using CLI.

To configure the network using the cli in RHEL 8 base, we will use the nmcli command. nmcli is a command-line tool for controlling NetworkManager and reporting network status. nmcli is used to create, view, edit, delete, enable, and disable network connections, as well as control and display the status of network devices.

Who Can Modify Network Settings

Only the root user can modify and edit the NetworkManager configuration using the CLI. A normal user can only read the NetworkManager configuration, unless he has privileges as the root user (sudo), then he will get the same benefits as the root user. If you get an error message, then you just need to add sudo before nmcli.

Viewing Networking Information

To show all of network devices, you can use this command

nmcli dev status
Enter fullscreen mode Exit fullscreen mode

It's can display devices name, types, states, and connections

Image description

To displays a list of all connections, use this command

nmcli con show
Enter fullscreen mode Exit fullscreen mode

you can add --active option to displays only active connections

Image description

Adding a network connection

The command below can add new connection named new1 for the interface eth0, which gets IPv4 networking information using DHCP and autoconnects on startup. The name of the configuration file is based on the value of the con-name option, new1, and is saved to the /etc/sysconfig/network-scripts/ifcfg-new1 file.

nmcli con add con-name new1 type ethernet ifname eth0
Enter fullscreen mode Exit fullscreen mode

You can create an new1 connection to an eth0 device using a static ipv4 address. You can use ipv4.address and ipv4.gateway options to add new static ip. it's still autoconnect on starup and save the configuration into the same file.

nmcli con add con-name new1 type ethernet ifname eth0 ipv4.address 192.168.1.5/24 ipv4.gateway 192.168.1.1
Enter fullscreen mode Exit fullscreen mode

Not only ipv4, you can also add static ipv6 configuration to new1 connection. You just need to add the ipv6.address and ipv6.gateway options.

nmcli con add con-name new1 type ethernet ifname eth0 ipv6.address 2001:db8:0:1::c000:207/64 ipv6.gateway 2001:db8:0:1::1 ipv4.address 192.168.1.5/24 ipv4.gateway 192.168.1.1
Enter fullscreen mode Exit fullscreen mode

Image description

if you get an error message like in the picture above, you just need to add sudo or you need to log in as root user. if it is successful, it will display the message "successfuly added". A collection of random letters like in the picture above is the connection id.

Controlling Network Connections

To activate the new connection configuration, you just need to use the command nmcli con up name. If you forget the name, remember that the nmcli con show command will list all connections. If the connection name contains spaces, then you need to add a ' symbol.

nmcli con up new1
nmcli con up 'new 1'
Enter fullscreen mode Exit fullscreen mode

to deactivate it, use the command nmcli con down name or nmcli dev dis devicename. Use nmcli dev dis devicename to deactivate a network interface.

nmcli con down new1
nmcli dev dis eth0
Enter fullscreen mode Exit fullscreen mode

if you get an error message, you just need to add sudo or you need to log in as root user.

Modifying Network Connection Settings

To see the details of a connection, we can use the command nmcli con show nameconnection. After the command is executed, a description of the connection will appear. Use the up arrow and down arrow on the keyboard to scroll. Use q to quit.

nmcli con show new1
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

The nmcli con mod nameconnection command can modify the configuration of a connection. This command can be added with several options. For example, the command below will change the ipv4 address and ipv4 gateway of the new1 connection.

nmcli con mod new1 ipv4.address 192.168.2.5/24 ipv4.gateway 192.168.2.1
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

You can also add more than one ip on one connection. You just need to add the + symbol before the option. For example:

nmcli con mod new1 +ipv4.address 10.1.1.11/24
Enter fullscreen mode Exit fullscreen mode

You can add several options like the one in the image below

Image description

You can also manually edit the file /etc/sysconfig/network-scripts/ifcfg-nameconnection using a text editor like nano or vim. Because I named it new1, the file will be stored in /etc/sysconfig/network-scripts/ifcfg-new1.

sudo vim /etc/sysconfig/network-scripts/ifcfg-new1
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

if you have finished configuring via vim or nano, don't forget to save it. If you are using vim, press Esc and type :wq if you are using nano, press Ctrl+x > y > Enter to save it.

You can configure IP statically or dynamically, it all depends on your needs.

Image description

After all the configuration is done, you need to run this command.

nmcli con reload
nmcli con down 'new1'
nmcli con up 'new1'
Enter fullscreen mode Exit fullscreen mode

The nmcli con reload command is used to make NetworkManager read configuration changes. The interface still needs to be restarted for changes to take effect.

Deleting a Network Connection

To delete a connection, you need to run the command nmcli con del 'nameconnection'. This will disconnect the connection and will automatically delete the file /etc/sysconfig/network-scripts/ifcfg-nameconnection. For example :

nmcli con del 'new1'
Enter fullscreen mode Exit fullscreen mode

Image description

Command Set

Image description

Top comments (0)