How to use nmcli command?
Tags: beginners, linux, tutorial
Published: 2022-09-18 03:52:33

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
```
It's can display devices name, types, states, and connections

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/df3lvkbf5jbx8xcjmdr9.png)

To displays a list of all connections, use this command

```
nmcli con show
```
you can add --active option to displays only active connections

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qj2t0sl7gn4zxl806drh.png)

## **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
```
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
```
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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42o96li5pjwoozt02aow.png)

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'
```
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
```
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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sn90kyfouy5khlyrh5rk.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kur0ib9zlnhbdjea4sl.png)

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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4vb3bqr68mujtjm1ab4.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8delz31wz9ngfj6o4un.png)

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
```

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itk4e62c23s7fkzf23th.png)

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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9n4rswghsmjo0q0mjlm.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gg1fksljrakraji9jigv.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lf469y6elmvcv0csawll.png)

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

```
nmcli con reload
nmcli con down 'new1'
nmcli con up 'new1'
```
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'
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wfn5hqomaw5lbxag58jv.png)

## **Command Set**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdvms2rg0a3rz5x5ihn3.png)