Skip to content

Setting the IP address on CentOS, AlmaLinux and Rocky Linux

In this article

Information

CentOS 7 and CentOS 8 have reached end of life, but the instructions for them are kept for servers that still run these versions. For new servers, use AlmaLinux or Rocky Linux 9-10 - see AlmaLinux, Rocky Linux 9-10 and CentOS Stream 9-10.

Attention

Incorrect network settings can make the server unreachable via SSH. Change the configuration of the main interface through the remote access console in Invapi, or be ready to use it to restore access.

CentOS 7

Network interfaces in CentOS 7 are stored and configured through the configuration file /etc/sysconfig/network-scripts/ifcfg-eno1, where eno1 is the name of the active network card. The names of the network cards available in the OS, link availability and the current address can be found by running the command:

ip address

How to configure DHCP

Attention

Active DHCP services on external Ethernet interfaces are forbidden by the Terms and Conditions of services and the use of the site.

Delete the IPADDR, NETMASK, GATEWAY variables, and specify the value dhcp in BOOTPROTO:

NAME="eno1"
ONBOOT=yes
BOOTPROTO=dhcp
HWADDR="ac:1f:6b:f6:3b:7e"
TYPE=Ethernet

How to set up a static IP address

The example of configuring a static IP address for an interface named eno1:

NAME="eno1"
ONBOOT=yes
BOOTPROTO=static
HWADDR="ac:1f:6b:f6:3b:7e"
IPADDR="82.148.21.50"
NETMASK="255.255.255.0"
GATEWAY="82.148.21.1"
TYPE=Ethernet

Applying a configuration

After changing the configuration file, you need to restart the network service:

systemctl restart network

Attention

After the restart, the changes will come into force. If you do not restart the network, the changes will be applied only after the server is restarted.

You can check the network settings using the command:

ip address

NetworkManager

Information

An alternative method for configuring IP is the NetworkManager utility (nmcli). You can view the list of existing connection profiles and their exact names with the nmcli connection show command.

Adding an Interface

sudo nmcli con add con-name "static-ens224" ifname ens224 type ethernet ip4 192.168.1.76/24 gw4 192.168.1.1

Set up DHCP on the interface

Attention

Active DHCP services on external Ethernet interfaces are forbidden by the Terms and Conditions of services and the use of the site.

sudo nmcli con mod "System ens192" ipv4.method auto ipv4.addresses "" ipv4.gateway ""

Set up a static IP address

sudo nmcli con mod "System ens192" ipv4.method manual ipv4.addresses 192.168.1.76/24 ipv4.gateway 192.168.1.1

After changing the profile, reconnect it for the settings to take effect:

sudo nmcli con up "System ens192"

CentOS 8

NetworkManager

Note

By default, NetworkManager is the recommended way to set up a network in CentOS 8. All other methods are outdated.

Adding an Interface

sudo nmcli con add con-name "static-ens224" ifname ens224 type ethernet ip4 192.168.1.76/24 gw4 192.168.1.1

Set up DHCP on the interface

Attention

Active DHCP services on external Ethernet interfaces are forbidden by the Terms and Conditions of services and the use of the site.

nmcli con add con-name eth3 type ethernet ifname eth3 ipv4.method auto

Set up a static IP address

nmcli con add con-name eth2 type ethernet ifname eth2 ipv4.method manual ipv4.addresses 192.168.0.15/24 ipv4.gateway 192.168.0.1

After adding or changing a profile, activate it:

sudo nmcli con up eth2

Note

Network interfaces in CentOS 8 are also stored and configured through the configuration file /etc/sysconfig/network-scripts/ifcfg-<interface_name>, for example ifcfg-enp1s0.

How to configure DHCP

Attention

Active DHCP services on external Ethernet interfaces are forbidden by the Terms and Conditions of services and the use of the site.

Delete the IPADDR, PREFIX, GATEWAY variables, and specify the value dhcp in BOOTPROTO:

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="enp1s0"
UUID="d5f41bf4-de0a-43b3-b633-7e2ec6212e58"
DEVICE="enp1s0"
ONBOOT="yes"

How to set up a static IP address

The example of configuring a static IP address:

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="enp1s0"
UUID="d5f41bf4-de0a-43b3-b633-7e2ec6212e58"
DEVICE="enp1s0"
ONBOOT="yes"
IPADDR=192.168.122.66
PREFIX=24
GATEWAY=192.168.122.1
DNS1=192.168.122.1

Applying a configuration

In CentOS 8, the ifcfg-* files are read by NetworkManager, so after changing a file you need to reload the configuration and reconnect the profile:

nmcli connection reload
nmcli connection up enp1s0

Note

The systemctl restart network command works in CentOS 8 only if the deprecated network-scripts package is installed. If you do not apply the changes, they will take effect only after the server is restarted.

You can check the network settings using the command:

ip address

AlmaLinux, Rocky Linux 9-10 and CentOS Stream 9-10

In AlmaLinux, Rocky Linux and CentOS Stream 9 and 10, the network is configured only through NetworkManager. Connection profiles are stored in keyfile format in the /etc/NetworkManager/system-connections/ directory (*.nmconnection files). The ifcfg-* files are deprecated in version 9 and are not supported in version 10.

Interface names depend on the hardware and server type, for example eno1, enp1s0 or ens18. The examples below use ens18 - replace it with the name of your interface.

When configuring your network, it's crucial to understand the difference between an active network interface and a connection profile in NetworkManager:

  1. Active Network Interface (visible through the ip addr show command) - this is the current working connection, which can be automatically configured via DHCP during system startup.

  2. NetworkManager Connection Profile (managed through nmcli connection) - this is a persistent configuration that is saved in the system and applied on each boot.

Attention

An interface can be active and functioning even without a profile in NetworkManager. This can lead to confusion when nmcli connection modify commands don't work due to the absence of a profile, although the interface is displayed and operational in the system.

Before making any configuration changes, always perform these checks:

  • Check active interfaces:

    ip addr show
    
  • Check existing NetworkManager profiles and their names (for example, System ens18, cloud-init ens18 or Wired connection 1):

    nmcli connection show
    
  • Check device status:

    nmcli device status
    

If a profile is missing, you need to create it before proceeding with further configuration:

nmcli connection add type ethernet con-name "ens18" ifname ens18

You can connect to the server via SSH - see the connection instructions - or through the remote access console in Invapi.

Location of Configuration Files

Main directories:

  • Connection files: /etc/NetworkManager/system-connections/
  • Additional configurations: /etc/NetworkManager/conf.d/
  • Main config: /etc/NetworkManager/NetworkManager.conf

How to Configure a Static IP Address

Creating a new connection:

nmcli connection add type ethernet con-name "static-ens18" \
ifname ens18 \
ipv4.method manual \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "8.8.8.8"

Modifying an existing connection (take the profile name from the nmcli connection show output):

nmcli connection modify "System ens18" \
ipv4.method manual \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "8.8.8.8"

Adding an additional IP address to an existing connection (without changing the main address and gateway):

nmcli connection modify "System ens18" +ipv4.addresses 192.168.1.101/24
nmcli connection up "System ens18"

Note

For more information on additional addresses, see Setting additional static IP address.

Editing Configuration Files:

  • Find the necessary config

    ls /etc/NetworkManager/system-connections/
    
  • Open the file for editing

    nano /etc/NetworkManager/system-connections/static-ens18.nmconnection
    
  • Set correct access permissions (NetworkManager ignores keyfiles with other permissions)

    sudo chmod 600 /etc/NetworkManager/system-connections/static-ens18.nmconnection
    

Example content of the file for a static IP:

[connection]
id=static-ens18
type=ethernet
interface-name=ens18

[ipv4]
method=manual
address1=192.168.1.100/24,192.168.1.1
dns=8.8.8.8;

How to Configure the Interface for DHCP

Attention

Active DHCP services on external Ethernet interfaces are forbidden by the Terms and Conditions of services and the use of the site.

nmcli connection modify "System ens18" \
ipv4.method auto ipv4.addresses "" ipv4.gateway ""

Applying Configuration Changes

After modifying the profile, apply the settings by following these steps:

  1. If the *.nmconnection file was edited manually, reload the configuration:

    sudo nmcli connection reload
    
  2. Restart the connection:

    sudo nmcli connection up "static-ens18"
    

    Attention

    Do not run nmcli connection down for the main interface while connected via SSH - the connection to the server will be lost. The nmcli connection up command applies the new settings without disconnecting first.

You can verify network settings using the following commands:

  • Check Connection Status:

    nmcli connection show "static-ens18"
    
  • Check IP Address:

    ip addr show ens18
    
question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×