This blog post will show you how to set a static IP address on CentOS 7 from scratch with no prior knowledge of any information about the network.

See if NetworkManager is running. NetworkManager is a network daemon that handles our network interfaces dynamically, but we’ll change our network scripts to set static configurations. We should stop this service.

systemctl status NetworkManager
systemctl stop NetworkManager

Check what interfaces are available.

nmcli d

Connect your Ethernet cable into the computer.

 

Getting a valid IP address from DHCP

You can use dhclient to find a DHCP server on the network and get automated network configurations like IP address. Skip if this step if you already have static configurations in mind. We’ll use the DHCP network configurations and set the info statically.

dhclient

After using dhclient, you should be able to ping 8.8.8.8 or use yum install. If these commands work, then you can find the new details about your IP address, subnet mask, and gateway with:

ip addr show

You’ll see a a part of the output:

128.197.115.7 is the IP address that we’ll use.

Before you proceed, run yum, CentOS’s package installer, and make sure that it works:

yum install vim

You can CTRL-C when you receive the Y/N prompt if you don’t want vim. The command was just a test.

 

Setting the Static IP

cd /etc/sysconfig/network-scripts/

ifcfg-enp30s0 is the name of our interface. You may have a differently named ifcfg file. Usually, the file goes by the name of ifcfg-eth0.

cat ifcfg-enp30s0

Here’s what we currently have. The interface uses DHCP, but we want the network configuration to be static.

vi ifcfg-enp30s0

In the file, we need to add/change the following lines at minimum:

We change BOOTPROTO to static instead of dhcp. GATEWAY will always have the first three parts of IP address and tend to end with .1. For example, if the IPADDR that you’re using starts with 128.197.115, then GATEWAY will also start with 128.197.115 and end with .1 and BROADCAST will end with .255. The NETMASK is usually 255.255.255.0. We change ONBOOT to yes, so that the interface boots with our new settings. Setting NM_CONTROLLED to no will mean that the interface will be managed by this script instead of by the Network Manager daemon.

Now, we’ll restart the network.

systemctl restart network

We verify that the interface is working.

ip add

You should be able to ping Google’s server at 8.8.8.8 after restarting the network.

ping 8.8.8.8

If you receive results from the ping, then setting the static IP has been successful! You’ll see that our network interface is now connected.

nmcli d

We should kill dhclient now that we have set up static IP addresses based on the DHCP given IP address.

You’ll see something like this if you have dhclient running. The process name will be on the right side, so you’ll know for sure if you see it still running.

We want to kill the process ID, which will be the first number, 13025.

If dhclient does not appear, then we have killed dhclient successfully.