Skip to content

Setting the IP address on CentOS

In this article

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 network card available in the OS, link availability and the current address can be found out 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:

```bash
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 reload the interface:

systemctl restart network

Attention

After the reload, the changes will come into force. If you do not reload the network, then 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.

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

Set up a static IP address:

sudo nmcli con mod "System ens192" ipv4.method manual

After adding routes, you must restart the NetworkManager service.

sudo systemctl restart NetworkManager

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.address 192.168.0.15/24 ipv4.gateway 192.168.0.1

After adding routes, you must restart the NetworkManager service.

$ sudo systemctl restart NetworkManager

Note

Network interfaces in CentOS 8 are also stored and configured through the configuration file /etc/sysconfig/network-scripts/ifcfg-eno1.

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:

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

After changing the configuration file, you need to reload the interface:

systemctl restart network

Attention

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

You can check the network settings using the command:

ip address

CentOS 9-10 Stream

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 configurations, always doing initial check:

  • Check active interfaces:

    ip addr show
    
  • Check existing NetworkManager profiles:

    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 "interface_name" ifname "interface_name" 

1. SSH Connection

Basic connection

ssh username@ip_address

2. Initial Network Diagnostics

  • View network interfaces

    ip addr show
    nmcli device status
    
  • Check current connections

    nmcli connection show
    

3. 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

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

Modify an existing one

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

Editing Configuration Files:

  • Find the necessary config

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

    nano /etc/NetworkManager/system-connections/static-eth0.nmconnection
    
  • Set correct access permissions

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

Example content of the file for a static IP:

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

[ipv4]
method=manual
addresses=192.168.1.100/24
gateway=192.168.1.1
dns=8.8.8.8

How to Configure the Interface for DHCP

Attention

Having an active DHCP server responding in your company's network is prohibited by the Terms and Conditions of Service and Website Use.

nmcli connection modify "System eth0" \
ipv4.method auto

Applying Configuration Changes

After modifying the configuration file, you need to reload the interface by following these steps:

  1. Reload Configuration:

    systemctl reload NetworkManager
    
  2. Restart Connection:

    sudo nmcli connection down "static-eth0"
    sudo nmcli connection up "static-eth0"
    

You can verify network settings using the following commands:

  • Check Connection Status:

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

    ip addr show eth0