How to Configure Networking on CentOS (7 & 8): An Expert’s Guide

configure networking on centos 7 & 8

Configuring the network on CentOS involves choosing the right tools—either the modern nmcli/nmtui or the classic network‑scripts. Whether you’re setting up DHCP or a static IP, this guide walks you through every step clearly, with real-world tips and troubleshooting help.

1️⃣ Know Which CentOS Version You’re Using

CentOS 7: Uses /etc/sysconfig/network-scripts/ifcfg-* files and integrates with NetworkManager.
CentOS 8: Encourages nmcli and nmtui, though the old scripts still work.

Understanding your version helps determine the best approach and avoids misconfiguring your network.

2️⃣ Identify Your Network Interface

Find your network interface name with these commands:

bash

ip link show

or

bash

nmcli device status

You’ll see names like enp0s3 or eth0. Note down the correct one before proceeding.

3️⃣ Choose Between DHCP or Static IP

DHCP (automatic IP)

With nmcli:

bash

nmcli con modify enp0s3 ipv4.method auto
nmcli con modify enp0s3 connection.autoconnect yes
systemctl restart NetworkManager

With nmtui:

  1. Run nmtui
  2. Select “Edit a connection”
  3. Set IPv4 Configuration to “Automatic (DHCP)” and save

Your VPS server will fetch an IP address automatically from your router or DHCP server.

Static IP (manual address)

A) Using ifcfg scripts (CentOS 7 & 8):

Edit /etc/sysconfig/network-scripts/ifcfg-enp0s3:

ini

DEVICE=enp0s3
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.20
PREFIX=24
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

Apply with:

bash

systemctl restart network # or NetworkManager depending on your setup

B) Using nmcli (CentOS 8 preferred):

bash
nmcli con modify enp0s3 ipv4.method manual \
ipv4.addresses 192.168.1.20/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "8.8.8.8 8.8.4.4" \
connection.autoconnect yes
systemctl restart NetworkManager

4️⃣ Verify Your Network Configuration

Check settings:

bash
ip addr show enp0s3 # Check IP assignment
nmcli con show "enp0s3" # View connection details

Test connectivity:

bash
ping -c 4 8.8.8.8 # Tests IP reachability
ping -c 4 google.com # Tests DNS resolution

Pro Tips for Real-World Usage

  • Persistent setup: Always set ONBOOT=yes or autoconnect yes to ensure your network starts on boot.
  • Name consistency: Use labels like ENP0 and ENP1 for clarity when managing multiple interfaces.
  • Avoid conflicts: When using both nmcli and ifcfg files, make sure NetworkManager isn’t trying to do double duty.
  • Multiple gateways: To control traffic routes, use a route-eth1 file or nmcli connection modify … ipv4.route... metric. This is great for complex setups or failover networking.

Troubleshooting Table

ProblemResolution
Interface doesn’t start on bootCheck ONBOOT=yes or autoconnect yes
IP didn’t change after editingRun systemctl restart NetworkManager
DNS issuesDouble-check /etc/resolv.conf or DNS lines in ifcfg-*
Firewall interferenceUse firewall-cmd --list-all to ensure network zones and ports are open

Quick FAQs

Q: Should I stick with nmcli or scripts?
For automation, go with nmcli. For straightforward, server-only configs, the classic scripts are easier to edit.

Q: Interface name changed unexpectedly?
Update the name in your config file and reload the service:

bash
systemctl reload NetworkManager

Q: What if I need IPv6 too?
Add IPv6 settings similarly. For example, ipv6.method auto or ipv6.addresses ….

Q: Can I apply these settings on a live server without downtime?
Yes—just restarting the interface or NetworkManager applies changes without reboot.

Wrapping Up

  • Identify your CentOS version
  • Choose DHCP or static based on needs
  • Configure correctly using nmcli, nmtui, or scripts
  • Test network connectivity and make it persistent
  • Apply best practices for clarity, consistency, and reliability

Need a hand optimizing enterprise network setups or tailored server configurations? Main VPS is here with specialized CentOS network expertise—just reach out.