In this process, 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 process to show you how easily it can be achieved.
To Start With: What Do You Need?
To complete this process, you will require a working installation of the CentOS 7 operating system with root privileges and a console-based text editor of your choice.
The Process
For the purpose of this process, 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 process for every device.
- To do this, log in as root and type the following command to get a list of all of your system’s network interfaces:
ip addr list
- If you have only one network card installed, it should be very easy to find out its name; just select the one not named 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.
-
Next, make a backup of the network interface configuration file (change the enp0s3 part accordingly, if your network interface is named differently):
cp /etc/sysconfig/network-scripts/ifcfg-enp0s3/etc/sysconfig/networkscripts/ifcfg-enp0s3.BAK
-
When you are ready to proceed, open the following file in your favorite text editor by typing what is shown next:
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
-
Now, work down the file and apply the following changes:
NM_CONTROLLED="no"
BOOTPROTO=none
DEFROUTE=yes
PEERDNS=no
PEERROUTES=yes
IPV4_FAILURE_FATAL=yes -
Now, add your IP information by customizing the values of XXX.XXX.XXX.XXX as required:
IPADDR=XXX.XXX.XXX.XXX
NETMASK= XXX.XXX.XXX.XXX
BROADCAST= XXX.XXX.XXX.XXX -
We must now add a default gateway. Typically, this should be the address of your router. To do this, simply add a new line at the bottom of the file, as shown next, and customize the value as required:
GATEWAY=XXX.XXX.XXX.XXX
-
When ready, save and close the file before repeating this step for any remaining Ethernet devices that you want to make static. When doing this, remember to assign a different IP address to each device.
-
When finished, save and close this file before restarting your network service:
systemctl restart network
How it works…
In this process, 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 process 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 process and to enable our changes to take immediate effect.