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
1 2 3 4 | DEVICE TYPE STATE CONNECTION enp30s0 ethernet unavailable -- enp31s0 ethernet unavailable -- lo loopback unmanaged -- |
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:
1 2 3 4 5 6 | 2: enp30s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 link/ether 00:1e:0b:fd:35:68 brd ff:ff:ff:ff:ff:ff inet 128.197.115.7/24 brd 128.197.115.255 scope global dynamic enp30s0 valid_lft 2011sec preferred_lft 2011sec inet6 fe80::21e:bff:fefd:3568/64 scope link valid_lft forever preferred_lft forever |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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=enp30s0 UUID=0b1b0269-9f3d-4b2e-9dca-d0b522860c0a DEVICE=enp30s0 ONBOOT=no |
vi ifcfg-enp30s0
In the file, we need to add/change the following lines at minimum:
1 2 3 4 5 6 7 | BOOTPROTO=static IPADDR=128.197.115.7 BROADCAST=128.197.115.255 GATEWAY=128.197.115.1 NETMASK=255.255.255.0 NM_CONTROLLED=no ONBOOT=yes |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | TYPE=Ethernet BOOTPROTO=static IPADDR=128.197.115.7 BROADCAST=128.197.115.255 NETMASK=255.255.255.0 GATEWAY=128.197.115.1 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=enp30s0 UUID=0b1b0269-9f3d-4b2e-9dca-d0b522860c0a DEVICE=enp30s0 ONBOOT=yes |
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
1 2 3 4 | DEVICE TYPE STATE CONNECTION enp30s0 ethernet connected enp30s0 enp31s0 ethernet unavailable -- lo loopback unmanaged -- |
We should kill dhclient now that we have set up static IP addresses based on the DHCP given IP address.
1 | ps -ef | grep dhclient |
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.
1 | root 13025 1 0 2015 ? 00:11:03 dhclient |
We want to kill the process ID, which will be the first number, 13025.
1 2 | kill 13025 ps -ef | grep dhclient |
If dhclient does not appear, then we have killed dhclient successfully.