By default, most instillation of Centos will end up having the default protocol set to be dynamic (DHCP) for obtaining the IP address. For a web server this is not what we want as it will be no good having the IP change and then having to change DHCP records to correctly point to the new IP address. This tutorial will show you how to change the IP protocol for a Centos 7 OS to use a static IP instead of a dynamic IP.
Start by navigating to the directory that contains the IP config files.
cd \etc\sysconfig\network-scripts\ ls
You should get a list of files that are inside this directory. Most of the time there will be a file named “ifcfg-eth0”, but its not always the case. The name may vary, but it will always begin with “ifcfg”. When you are looking at the results have a look and see what files are in this directory that begin with “ifcfg” and work from there.
Once you have found the correct file you will see that there is a variable near the top of the file called “BOOTPROTO”. This will be set to “dhcp” this needs to be changed to be static. You will then need to go to the bottom of the file and alter or add some values for the network information. Your file should look like the file below once you have finished. (be sure to replace the placeholder info with actual IP information).
TYPE=Ethernet BOOTPROTO=static 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=ens160 DEVICE=ens160 ONBOOT=yes IPADDR=18.104.22.168 NETMASK=255.255.255.0 GATEWAY=22.214.171.124 DNS1=126.96.36.199
If you are in a situation where you have no access to the machine other than command line, you will need to use VI to edit the file. Once you have a static IP you should be able to connect to the machine using SFTP and modify the file using a text editor.
Once you have made edits to the file, you will need to restart the networking on the server. The following command will restart the networking and should now allow you to access the server using the static IP that you have defined.