Set a static IP address on CentOS or RHEL
Configure a static IPv4 address, gateway and DNS servers in the ifcfg file of a CentOS or RHEL interface, then restart networking.
On CentOS and RHEL, a static IPv4 address is set in the interface file under /etc/sysconfig/network-scripts/. The example uses the interface enp0s3 and documentation addresses from 192.0.2.0/24.
Edit the interface file
sudo vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
DEVICE=enp0s3
NAME=enp0s3
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
# Server IP
IPADDR=192.0.2.10
# Subnet
NETMASK=255.255.255.0
# Default gateway
GATEWAY=192.0.2.11
# DNS servers
DNS1=192.0.2.11
DNS2=192.0.2.12
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
# Disable IPv6
IPV6INIT=no
# Keep the UUID already present in the file
UUID=<connection-uuid>
Restart the network service
sudo systemctl restart network
Check the DNS configuration
Check the DNS servers used by the system:
cat /etc/resolv.conf
Notes
- Keep one
BOOTPROTOline (nonefor a static address,dhcpfor a dynamic one) and oneDEVICEline that matches the interface name. - The hardware address is not copied: it is specific to each machine.