13.06.2023

Configuring the network interface on CentOS

Configuring network interfaces in CentOS is rather different from how it is done on Ubuntu.
The network subsystem in CentOS is managing by the Network Manager (NM).

Like other system software, NM reads config files to control network interfaces from /etc/sysconfig/network-scripts/ subdirectory.
It should be mentioned that terms “network device” and “connection” differ:

Two software tools are in usage to work with NM:

Calling both utilities to view the current settings is possible under a regular user, root user rights are required to make changes.
NM is a systemd service that must always be started for the correct operation of the server network. Usually, it is added to the startup when installing the server, you can check whether it is enabled:

systemctl status NetworkManager.service

The output should contain a string with entry "Active: active (running) since" meaning NM is running.

nmtui - visually and clearly

When nmtui is started, the window appears suggesting actions:

Proceed from this point to edit profiles, activate required profile, set new server’s name.
Switching for editing, a list of created profiles is available on the left, on the right are buttons for actions – generating a new profile, editing, deleting, returning to the previous window:

In our example the only profile exists with name the "System ens0p5".
To modify the profile, click "Edit", a window will open where changes could be made for IP v4\v6 protocols settings:

The first two fields show profile's name and interface it is applied for.
On servers, IP is often set permanent manually. "Manual" type should be selected from the list on the desired IP protocol version to do this:

Click on "Show" button on the right will expand settings part.
Desired values are specified in the appropriate fields:

Additional options can be chosen with a cross sign, e.g. – "Ignore automatically acquired DNS parameters" if planning to use DNS servers that differ from those received automatically.

Important note: read the following paragraph in full to avoid losing connection to your server!
Once values are changed, click on "OK" — at this point profile’s config "ifcfg-ens0p5" is modified, but changes are not applied to the current NIC settings. To apply, it is necessary to deactivate the previous settings and activate the modified profile – to do this, click "Activate a connection" in the window with actions choice, mark the profile and click "Deactivate" on the right. After that, the "Activate" button should appear – click it to apply the new settings. If deactivated interface is the only on the server then this causes the old settings ceasing to work, and cloud server will be disconnected. This will make it impossible to activate the profile. Serverspace allows you to open a web server management console where nmtui is accessible to activate the profile – server will become available again. The new settings can be safely applied using the nmcli utility (described in the next section), so not need to click on "Activate a connection", but click "OK" to save the settings and exit. If changes are applied to additional NIC, disabling which will not lead to loss of communication, then all actions are performed in nmtui.

nmcli – fast and effectively

nmcli with desired subcommands and values also allows you to modify profiles. There is large number of subcommands available for configuring IP and VPN protocols, creating bridges, aggregating channels, etc., which are well described in man.
Basic commands are enough while setting up the IP manually.
Getting server’s NICs:

nmcli device show

Getting profiles:

nmcli connection show

The output is much shorter: name, UUID, type, and NIC’s name the profile is used for.

The names of profile and NICs may not match – pay attention to this when changing and activating profiles.
Modify subcommand is aimed to edit profile. E.g., run to set IP, gateway, DNS server:

nmcli connection modify "System enp0s5" ipv4.method manual ip4 172.16.61.10/24 gw4 172.16.61.1 ipv4.dns "208.67.220.220 208.67.222.222"

Shorter commands can run to modify individual parameters, for example, to register new DNS server:

nmcli connection modify "System enp0s5" ipv4.dns "208.67.220.220 208.67.222.222"

As in the case of configuration using nmtui, these commands are modifying config file /etc/sysconfig/network-scripts/ifcfg-enp0s5, but current settings stay unchanged. To apply it, the profile needs to be applied again (deactivate and activate):

nmcli connection down “System enp0s5” && nmcli connection up “System enp0s5”

In fact, nmcli is called twice using symbols && meaning the second call is executed only if the first one succeeds.
Sequential launch of nmcli is advised. Otherwise, as noted in the section about nmtui, if NIC is the only one on the server, the profile will not be activated, and the connection with the server will be lost.
Use this method after making changes in nmtui.
As it was said at the beginning, there can be more than one profile for one, and then the list of profiles will contain both active (which are currently being used) and inactive. For example:

It can be seen that the profile "System enp0s5" is active (entry is in green) and is applied to the enp0s5 interface. When creating the "enp0s5-new" profile, the enp0s5 interface was specified, but it is not currently used. If required, it can be applied quickly. To do this, deactivate "System enp0s5" and activate "enp0s5-new". SSH connection will be closed after running next command, so reconnection to the server is required .

nmcli connection down "System enp0s5" && sudo nmcli connection up "enp0s5-new"

In case there are settings errors and SSH connection is not available then web console can be used to fix it as noted above.

Without nmtui и nmcli

It is not necessary to use the nmtui and nmcli utilities to make changes to profiles.
You can manually create\modify config files "ifcfg-". But in this case, there is a high probability of errors and typos, which can lead to loss of connection to the server. Therefore, it is recommended to use these utilities anyway – they correctly form configs.

You may be also interested in