How-To Guides and Blog

How to Configure Networks with nmcli Command in Linux

NBH Support
No Comments

After going through some basic commands needed to troubleshoot your network, It is worthwhile to understand how network connections are configured in a Linux system.

It is well-known that a network interface is also called a device such as the eth0. So we are to understand that each of these devices in Linux can be configured to have multiple connections in such a way that you can switch between each connection without necessarily modifying the existing connection for a device. For example, you have a DHCP connection setup by default from your network providers, but some configuration in your system servers may not require the use of a DHCP, which means you will have to use static networking. Instead of modifying the configuration manually, each configuration can be stored as a separate connection.

Please note that multiple connections may exist for a device, but only one can be active at a particular time

In this article, we will be looking at how to create network connections and manage network devices using nmcli command.

HOW TO VIEW NETWORK INFORMATION WITH NMCLI COMMAND

A quick start is to understand the syntax of the nmcli command then we can advance further on all what we can do with our nmcli command.

The syntax goes as;

nmcli [Option] [device] [command | help]

Now, let’s view the network information in our system to understand better on the network interfaces.

The first column shows the network devices in our system; the second shows the type of network, the third shows the state (if it’s connected or not), the fourth shows the configuration.

You can see the first device in the first column shows the ens33; this may vary on different Linux system depending on the network card used. The type of network is the Ethernet network type (we could have wifi, bond, etc.)

You should also know that each of these devices is stored in a configuration file in the /etc/sysconfig/network-scripts directory.

Now, let’s take a look at the configuration file of our device ens33

That is the configuration file of our ens33 device. Let me explain a few of this as quickly as possible for a better understanding of this topic

TYPE: This is the type of connection. As I said, it can be wifi or bond or any other Ethernet type you are using

BOOTPROTO: This is the boot protocol of the interface. If the value indicates “DHCP,” it means the connection profile takes its dynamic IP address from a DHCP server, but if it shows “none,” it means the connection profile is set static or has a static IP address. The protocol can be one of the following, “None,” “dhcp,” or “bootp” (bootstrap protocol)

DEFROUTE: The defroute could be set to either yes or no. when set as “yes,” it means the interface is set as the default route for the IPV4 and IPV6 address but when set as “no,” it means the interface is not set as the default route.

IPV4_FAILURE_FATAL: This can have either “yes.” or “no.”. If the value is “yes,” it means the interface is disabled if IPV4 and IPV6 configuration fails and if the value is “no,” it means the interface is not disabled if the configuration file failed.

IPV6INIT: It could have either “yes” or “no” value. If the value is “yes,” it means you can enable IPV6 interface of the system. When this is enabled, it can take IPV6 address and the default gateway as parameters. When it shows “no,” it means to disable the IPV6 interface.

DEVICE: This indicates the name of our device, which is associated with the connection profile.

ONBOOT: This can take the value yes or no. when the value is a “yes” it means the network interface is activated at boot time but if the value is a “no,” it means the network interface is not enabled at boot time**.**

After going through this, we can now show our connection profile

Remember we said, multiple connections may exist for a device, but only one can be active a particular time. The above command shows two active devices, but if you have multiple devices in your system, and you wish to view only the active connection, you can specify the – -active option.

Such as nmcli connection show – – active. Please note that you can use “con” or “connection.” Any which you choose will work for you. Most people tend to use “con.”

You can also choose to specify the device name to view the details of that connection

Please note that the lower case settings represent the configuration of the connection and the upper case settings represents the active data as seen in the first image on this article when we used the command nmcli device status.

HOW TO CREATE NETWORK CONNECTION WITH NMCLI COMMAND

You must take into consideration the order in which the arguments appear while using the nmcli command. First, specify the common arguments such as the type and interface. Also, know that multiple IP addresses may exist on a device.

Now let’s try setting up a connection with a specified name which will auto-connect as an Ethernet connection on the eth0 device.

This is a pretty step in getting to create a connection. Please, it could be technical trying to run all the commands on your command line by merely memorizing them. It is best you type nmcli command on the command line and use the Tab completion key to get through what next to enter. The Tab completion key is very helpful while using the nmcli command to set up network connections.

We just created a dhcp connection profile that will auto-connect by default at boot time. You can see how long the command is, so you should make use of the Tab completion key to go through this.

Now let’s try to create a connection by specifying the IP address and a gateway

Specifying an IP address technically means you are setting up a static network and not a dhcp connection profile. So, now, we have successfully created a static connection profile on the eth0 device, and we have two connections in our profile (dhcp and static).

Please note that the connection name could be any name of your choice, but you should specify a name that can easily be distinguished from a static and dhcp connection.

HOW TO ACTIVATE A CONNECTION

Once you have created a connection, the connection doesn’t just become active. You will have to activate the connection. It is possible to have multiple active connections from a different device, but it is impossible to have multiple active connections on the same device.

So we have a connection, let’s try to bring the connection to live by activating it so that it will be active at boot time

Now, our static connection is up and running, and it will keep on running even at boot time. Please take note while specifying your device name when creating your network. Most devices use eth0 and while some use ens33. If there is a mistake in specifying a wrong device, you might not be able to activate the network.

You can change back to your previous device by switching to the device name

We are back to our previously used device. Please note that peradventure the static connection is lost, the default connection will attempt to auto-connect. To prevent that from happening, enter the following command on the terminal;

nmcli [device] [disconnect] [devicename]

HOW TO DEACTIVATE A CONNECTION

You can also deactivate a connection as seen below

HOW TO MODIFY A NETWORK INTERFACE WITH NMCLI COMMAND

An existing connection can be modified using the nmcli connection mod argument. You can first view the information about the connection by using the nmcli connection show. The arguments can hold the property name and the setting name of the connection.

However, there is something you need to be aware of while modifying a connection. Any connection can be modified, be it a dhcp connection or a static connection but while modifying a dhcp connection to a static connection, remember to change the IPv4.method from auto to manual. That is to say, remember to specify an IP address for it. If you fail to do that, you might end up with a dhcp and a static connection

Now let’s give a simple try by modifying some properties which was included while creating the account.

In the command, we modified the existing connection which was set to auto-connect at boot time from “yes” to “no.”

Now let’s specify a new IP address to our static connection

There is something you need to pay close attention to here. Remember when adding a new connection, we used the “ip4” and “gw4” when modifying a connection, we used the “ipv4” and “gwv4”. Also, note that any modified connection still need to be activated before it can start working. So you can always use the nmcli [connection] [up] [device] [connection name]

We can also choose to add or remove an IP address DNS address to our static connection by specifying the – or + operator.

To add, we do as follows

To remove, we do as follows

HOW TO EDIT NETWORK CONFIGURATION FILE

Aside from configuring network connections using the nmcli command, it is also possible for an administrator to configure network connections by editing the interface configuration file. These files control the software interfaces of a particular device. Each file for every device resides in a directory called /etc/sysconfig/network-scripts. Each file in the directory is named in this pattern ifcfg-name*.* Where name is the name of the connection.

Use any text editor of your choice to make changes as suit. Always remember to reload the configuration file to make the NetworkManager read changes to the configuration file. The interface still needs to be activated for the changes to take effect.

Let’s give this a try

CONCLUSION

Remember to use the Tab completion key. It is your friend while using the nmcli command. To round it up I will list the summary of the basic commands for self-practice

nmcli device status: Will list all the network devices

nmcli connection show: Will list all connections

nmcli connection reload: Will reload the network configuration file

nmcli connection up [connection name]: will activate a connection

nmcli connection down [connection name]: Will deactivate a connection

nmcli device disconnect [device name]: Will prevent a device from auto-connecting

nmcli connection add: Will add a new connection

nmcli connection delete: Will delete a connection

nmcli connection modify: Will make changes to a created connections