Setting a Static IP and DNS on CentOS

For cloud servers and VMs, letting DHCP assign the IP automatically is convenient. But the moment you need to configure port forwarding, bind a service, give a machine a fixed internal address, or keep firewall rules stable long-term, a dynamic IP becomes a headache — the address can change on the next reboot, and every config that depends on it has to follow. That is the most common reason production environments switch to a static IP.

Knowing which approach fits avoids a lot of detours:

Aspect DHCP Static IP
Setup cost Low, plug and play Must specify addresses manually
Address stability May change after reboot Fixed
Best for Temporary environments, home networks Servers, fixed internal services
Troubleshooting Depends on DHCP service state Mostly manual verification

Simple rule: if other machines need to reach this one reliably (web services, databases, an internal NAS), use a static IP; if it is a temporary stand-in, DHCP is fine.

To set a static IP on CentOS, modify the network interface configuration file, configure DNS, and restart the interface to apply the changes. Here are the complete steps.

1. Edit the Network Interface Configuration File

vi /etc/sysconfig/network-scripts/ifcfg-eth0

Content:

DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.1.8
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
HWADDR=00:15:5D:E4:A2:BE
ONBOOT=yes
TYPE=Ethernet

Field explanation:

  • DEVICE: the network device name, which must match eth0 in the filename;
  • BOOTPROTO=static: use a static IP (do not use DHCP);
  • IPADDR: the local static IP address;
  • NETMASK: the subnet mask; PREFIX=24 can replace NETMASK=255.255.255.0;
  • GATEWAY: the gateway address;
  • HWADDR: the network card MAC address;
  • ONBOOT=yes: enable this interface automatically at boot;
  • TYPE=Ethernet: the interface type is Ethernet.

Reference: Red Hat's ifcfg file documentation https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/sec-configuring_ip_networking_with_ifcfg_files

2. Configure DNS

vi /etc/resolv.conf

Add:

nameserver 8.8.8.8
nameserver 8.8.4.4

Note: on CentOS 7 and newer, when NetworkManager manages the network, resolv.conf can be regenerated and overwritten on network restart. The more stable approach is to put DNS in the DNS1 and DNS2 fields of the interface file and let NetworkManager apply them.

Also distinguish two situations: if this is a cloud VPS, the public IP is usually assigned through the provider's control panel, and editing the interface file will not change the public address — the static IP described here mainly applies to internal/private addresses. If it is a self-hosted physical machine or a VM, the LAN IP can be fixed exactly as described. Confirm which layer of address you are changing before you start, so you don't spend time editing the wrong interface.

3. Restart the Network Interface

service network restart

After restarting, verify with the following commands:

# Check whether the IP took effect
ip addr show eth0

# Test gateway connectivity
ping -c 4 192.168.1.1

# Test DNS resolution
nslookup www.example.com

If ping reaches the gateway, the link is healthy; if nslookup resolves, DNS is live. Passing both is a good sign.

A typical configuration run

Say an internal CentOS 7 server should be fixed at 192.168.1.8: first run ip link to confirm the interface is named ens33 rather than eth0, edit the matching ifcfg-ens33 with the IP, mask, and gateway, then apply DNS and manual mode together via nmcli, and finally restart networking. The whole process takes about ten minutes; once verified, swap the dynamic address in your SSH client for the fixed one and use it long-term.

If you configured via nmcli, confirm the state with:

nmcli con show ens33 | grep -E 'ipv4.method|ipv4.addresses|ipv4.gateway|ipv4.dns'

Seeing ipv4.method: manual and the expected address means the config is live; a clean systemctl status network confirms the networking service is healthy.

Troubleshooting common issues

Symptom Possible Cause Fix
Interface fails to start HWADDR does not match the real MAC Check the actual MAC with ip link and correct it
Config reset after reboot NetworkManager takes over Configure via nmcli, or disable NM management for the interface
IP conflict Duplicate address on the LAN Pick an unused address and probe with arping
Online but no DNS DNS overwritten or not applied Write DNS1/DNS2 or check resolv.conf

Check the logs first when troubleshooting — journalctl -u network (or systemctl status network) surfaces the failure reason directly, while ip addr and ip route show the address and routing table so you can confirm whether the config actually applied. Most "configured but not working" cases come down to editing the file but forgetting to restart, or ONBOOT=no preventing the interface from enabling at boot.

Notes

  1. HWADDR must match the actual network card MAC, otherwise the interface may fail to start;
  2. Network interface names vary across CentOS versions (such as ens33, eno1); use the matching filename;
  3. On CentOS 7 you can also use the nmtui GUI tool or the nmcli command to configure a static IP more conveniently:
    nmcli con mod eth0 ipv4.addresses 192.168.1.8/24 ipv4.gateway 192.168.1.1 ipv4.dns 8.8.8.8 ipv4.method manual
    nmcli con up eth0
    
  4. In virtual machines such as VMware, consider using an intranet or LAN-resolvable DNS address instead of relying on external DNS;
  5. Back up the file first with cp ifcfg-eth0 ifcfg-eth0.bak so a mistake can be reverted quickly;
  6. To configure multiple IPs, append IPADDR1/NETMASK1 (or PREFIX1) fields to the ifcfg file, or add them temporarily with ip addr add — but temporary addresses are lost on reboot;
  7. If the network also uses IPv6, append fields like IPV6INIT=yes and IPV6ADDR to the ifcfg file, or leave it to DHCPv6.

Once the IP and network are configured, you can manage the server remotely over SSH. For related tips, see the cloud servers category, such as Linux server basics and server operations tips. In short, the key to a static IP is getting the address, gateway, and DNS right in one pass, then confirming the changes took effect after a restart — after that you can use it long-term.

Original post: https://www.cnblogs.com/cqzhuomi/articles/17283241.html (cnblogs.com CQZHUOMI, repost)