Setting a static IP address
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.
Getting ready
To complete this recipe, you will require a working installation of the CentOS 6 operating system with root privileges and a console-based text editor of your choice.
How to do it...
For the purpose of this recipe, you will be able to find all the relevant files in /etc/sysconfig/network-scripts/
. But before making any changes to your original configuration, it is always a good idea to create a simple backup of the existing configuration files for future reference.
- To do this, log in as root and type the following command:
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.bak
- The preceding command will make a copy of
ifcfg-eth0
and name itifcfg-eth0.bak
. Now repeat the previous step for any remaining Ethernet devices. - When you are ready to proceed, open the following file in your favorite text editor by typing:
vi /etc/sysconfig/network-scripts/ifcfg-eth0
- 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 IPv4 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
- When ready, save and close the file before repeating this step for any remaining Ethernet devices. When doing this, remember to assign a different IP address to each device.
- Having completed the configuration of your Ethernet card(s), we now need to make some additional changes starting with the following configuration file:
vi /etc/sysconfig/network
- We must now add a default gateway. Typically, this could be the address of your router and to do this, simply add a new line at the bottom of the file like so and customize the value as required:
GATEWAY=XXX.XXX.XXX.XXX
- When finished, save and close this file before restarting your network service:
service network restart
How it works...
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 creating a backup of the original Ethernet configuration files, we then opened the primary Ethernet configuration located at /etc/sysconfig/network-scripts/ifcfg-eth0
in order to make the following adjustments:
NM_CONTROLLED="no" ONBOOT=yes
Having ensured that the Ethernet device will be available at boot, we no longer require the services of the Network Manager , and for this reason we disabled NM_CONTROLLED
by setting the value to no
.
We then applied the following change:
BOOTPROTO=none
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:
IPADDR=XXX.XXX.XXX.XXX NETMASK= XXX.XXX.XXX.XXX BROADCAST= XXX.XXX.XXX.XXX
When finished, we were asked to save and close the file before repeating this step for any remaining Ethernet devices.
In the next phase we were then required to make some changes to the following configuration file:
vi /etc/sysconfig/network
The default file will look similar to the following:
NETWORKING=yes HOSTNAME= your_computername_here
However, in order to assist the creation of a static IP address we were required to add a new setting known as the default gateway . This configuration change is important (in as much that it allows the server to contact the wider world), and it was achieved by simply adding a new line at the bottom of the file like so:
NETWORKING=yes HOSTNAME=your_computername_here GATEWAY=XXX.XXX.XXX.XXX
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.
So you could say that having modified the configuration details for your Ethernet devices and by adding a default gateway, it was simply a process of telling your server not to use DHCP, to remain independent of the Network Manager and to access the world through a specific gateway address. So well done, you work is done and you can now enjoy the benefits of a static IP address.
There's more...
Having previously mentioned PEERDNS
, we shall now come back to this in order to provide you with some additional background reading that will serve to enhance your understanding of the DNS system and improve the performance of your server.
You may or may not be familiar with the /etc/resolv.conf
file. On a freshly installed system or a desktop this file is typically maintained by the Network Manager. To see the evidence of this, if you ever take the opportunity to open this file and read the default content, you will almost certainly see the words Generated by NetworkManager
close to or at the top of this file.
Also known as the resolver, resolv.conf
is used for DNS management and it is used to configure your server's access to the Internet Domain Name System (DNS). It does this by determining which name servers are to be used and in what order they are chosen and by optimizing this file we can intentionally manage how your server will experience the world at large.
Instead of editing this file directly we are going to use a trick of the trade that will modify resolv.conf
automatically and do all the hard work for us.
To begin, open the primary Ethernet configuration file in your favorite text editor by typing:
vi /etc/sysconfig/network-scripts/ifcfg-eth0
Now scroll down and modify the PEERDNS
value to look like this:
PEERDNS=yes
Now scroll down to the end of this file and add the following new lines:
DNS1=XXX.XXX.XXX.XXX DNS2=XXX.XXX.XXX.XXX DOMAIN=your_domain.com
The final addition refers to your preferred DNS name servers and you local domain name. Currently, a maximum of three DNS values are supported, but two will suffice for any typical system; so customize these values as necessary with something more appropriate to your own needs.
For example, you could use Google's Public DNS like so:
DNS1=8.8.8.8 DNS2=8.8.4.4 DOMAIN=your_domain.com
When finished, save and close the file and then type:
service network restart
Having completed these steps, you will be activating the PEERDNS
control and as a result, the server will now read the new DNS information supplied by your Ethernet configuration file and append /etc/resolv.conf
. So remember, if you ever need to change your DNS servers, simply manage them via the primary Ethernet device configuration file mentioned in the preceding steps.
It is commonly understood that you should always supply a Netmask value when determining your network configuration, but as an alternative, there is something called a prefix , and it does save time in typing out a long series of numbers.
The prefix is a shorthand way of specifying the Netmask value by implementing the Classless Inter-Domain Routing (CIDR) system. For both you and me this means:
Instead of typing this:
NETMASK= 255.255.255.0
We can replace it with this:
PREFIX=24
For the example shown here, the prefix shown is translated by the CIDR to imply the value 255.255.255.0
. Consequently, when writing your Netmask value, instead of writing this:
IPADDR=XXX.XXX.XXX.XXX NETMASK= XXX.XXX.XXX.XXX BROADCAST= XXX.XXX.XXX.XXX
You could use this:
IPADDR=XXX.XXX.XXX.XXX PREFIX= XX BROADCAST= XXX.XXX.XXX.XXX