After a fresh installation CentOS uses DHCP (dhclient -v) to assign an IP to the machine, which keep in changing on reboots, or service restarts, etc.
Use-case: Various service setups, especially involving a clustered configuration we need to set a fixed IP for each machine, so they can communication with each other, in case of DHCP the installation may break on the reboot, as anyone of the machine gets a new IP address. So the first step is to set a static IP address.
Step 0 – Get ethernet interface name
You can get it using any of the following commands – the name can be in different formats ethx, ensxx, enpxx, for example, eth0, ens03, enp0s3, etc.
Step 1 – backup resolv.conf
Can be updated by the NetworkManager service – in case DNS is specified in the interface conf.
Step 2 – Edit Ethernet script file
The usual path to the ethernet conf is /etc/sysconfig/network-scripts/ifcfg-<your-interface-name>
The content of the original file:
Now, update the script for the following parameters
Step 3 – Update network file and DNS
Set the gateway at /etc/sysconfig/network. You can get the gateway using any of the following commands:
default via 192.168.169.2 dev eno16777736 proto static metric 100
192.168.169.0/24 dev eno16777736 proto kernel scope link src 192.168.169.130 metric 100
[nahmed@localhost ~]$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.169.2 0.0.0.0 UG 100 0 0 eno16777736
192.168.169.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
Update the gateway
Update the DNS
You can use any of the Google’s nameservers i.e. 220.127.116.11, or 18.104.22.168.
# Generated by NetworkManager
Step 4 – Restart your network or reboot
Step 5 – Ping verify