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.
or,
or,
Output:
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:
HWADDR=00:0C:29:1B:C3:18
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=a95dfdb5-15c8-4a81-b187-b9fee24246db
ONBOOT=no
Now, update the script for the following parameters
IPADDR=192.168.169.130
NAME=eno16777736
ONBOOT=yes
Output:
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:
or,
Output:
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
[nahmed@localhost ~]$
Update the gateway
NETWORKING=yes
GATEWAY=192.168.169.2
Update the DNS
You can use any of the Google’s nameservers i.e. 8.8.8.8, or 8.8.4.4.
# Generated by NetworkManager
nameserver 8.8.8.8
Step 4 – Restart your network or reboot