In this recipe, we will learn how to configure a static IP address for a new or existing CentOS server.
While a dynamically assigned IP address or DHCP reservation may be fine for most desktop and laptop users, if you are setting up a server, it is often the case that you will require a static IP address. From web pages to e-mail, databases to file sharing, a static IP address will become a permanent location from which your server will deliver a range of applications and services, and it is the intention of this recipe to show you how easily it can be achieved.
To complete this recipe, you will require a working installation of the CentOS 7 operating system with root privileges and a console-based text editor of your choice.
For the purpose of this recipe, you will be able to find all the relevant files in the directory, /etc/sysconfig/network-scripts/
. First, you need to find out the correct name of the network interface that you want to set as static. If you need to set more than one network interface as static, repeat this recipe for every device.
ip addr list
lo
(which is the loopback device). If you got more than one, having a look at the IP addresses of the different devices can help you choose the right one. In our example, the device is called enp0s3
.enp0s3
part accordingly, if your network interface is named differently):cp /etc/sysconfig/network-scripts/ifcfg-enp0s3/etc/sysconfig/network-scripts/ifcfg-enp0s3.BAK
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
NM_CONTROLLED="no" BOOTPROTO=none DEFROUTE=yes PEERDNS=no PEERROUTES=yes IPV4_FAILURE_FATAL=yes
XXX.XXX.XXX.XXX
as required:IPADDR=XXX.XXX.XXX.XXX NETMASK= XXX.XXX.XXX.XXX BROADCAST= XXX.XXX.XXX.XXX
GATEWAY=XXX.XXX.XXX.XXX
systemctl restart network
In this recipe, you have seen the process associated with changing the state of your server's IP address from a dynamic value obtained from an external DHCP provider, to that of a static value assigned by you. This IP address will now form a unique network location from which you will be able to deliver a whole host of services and applications. It is a permanent modification, and yes, you could say that the process itself was relatively straightforward.
So, what have we learned from this experience?
Having started the recipe by identifying your network interface name of choice and creating a backup of the original Ethernet configuration files, we then opened the configuration file located at /etc/sysconfig/network-scripts/ifcfg-XXX
(with XXX
being the name of your interface, for example, enp0s3
). As being static no longer requires the services of the network manager, we disabled NM_CONTROLLED
by setting the value to no
. Next, as we are in the process of moving to a static IP address, BOOTPROTO
has been set to none
, as we are no longer using DHCP. To complete our configuration changes, we then moved on to add our specific network values and set the IP address, the netmask, broadcast, and the default gateway address.
In order to assist the creation of a static IP address, the default gateway is a very important setting in as much as it allows the server to contact the wider world through a router.
When finished, we were asked to save and close the file before repeating this step for any remaining Ethernet devices. Having done this, we were then asked to restart the network service in order to complete this recipe and to enable our changes to take immediate effect.