Configure a static IP Address in RHEL 6/Centos 6

20151224013419This is a quick post to show how to set a static IP in RHEL6 or CentOS 6. The process is fairly straight-forward however you need to be aware of the different files that need to be changed in order for it to work properly. So, to set a static IP in RHEL6 or CentOS 6 follow the steps below.

The Steps

1. The first step is to edit the interface that you want to set the static IP on. In my example I want to change the IP on eth0 from 172.16.0.21 to 10.10.10.50. This not only means that the IP address will change but the default gateway will also. So to proceed I will edit the interface config file for eth0, which is ifcfg-eth0 using vi, per the below:

[root@web01 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0

2. What you see in this file can vary. The basic requirements for networking are listed below, change them to match your desired networking information:

DEVICE=eth0
BOOTPROTO=STATIC
IPADDR=10.10.10.50
NETMASK=255.255.255.0
GATEWAY=10.10.10.1
ONBOOT=yes

We can see from the above that I have configured the static IP of 10.10.10.50. You must also ensure the correct subnet mask and gateway for your network are configured. All the other parameters must match the above.

3. I tend to remove all superfluous information from the interface file apart from what is listed in step 2 above. If you’re wondering what I mean by this, see the example below:

DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=A5:BC:CB:48:F2:05
TYPE=Ethernet
BOOTPROTO=static
NAME="System eth0"
UUID=5cb07fe0-0cd0-7ddc-45c1-d6fbb43fa6c05
IPADDR=172.16.0.21
NETMASK=255.255.255.0

You do not need NM_CONTROLLED, HWADDR, TYPE, NAME & UUID for the interface to work. In some cases, having the HWADDR parameter present could prevent the interface from working, such as when you clone a VM and it has the HWADDR or mac address of the old server in the interface file. As as personal best practice I remove all of this unneeded gumph and keep the file as clean and as simple as possible. Another major point to highlight is that in the above config the GATEWAY parameter is not present, whereas in my config in step 2 it is present.

4. The next step is to check the /etc/sysconfig/network file. As before, edit it in vi:

[root@web01 ~]# vi /etc/sysconfig/network

I see the following:

NETWORKING=yes
HOSTNAME=web01

So there is nothing to change for me. However, this is where the GATEWAY parameter can also be configured. If it is not stated in the interface config file, per the example interface config file in step 3 you should see the settings below in the /etc/sysconfig/network file:

NETWORKING=yes
HOSTNAME=web01
GATEWAY=172.16.0.1

If your interface config file doesn’t contain the GATEWAY parameter, then you will need add it here to reflect your new default gateway. So using my example it would be changed to:

NETWORKING=yes
HOSTNAME=web01
GATEWAY=10.10.10.1

5. The next step is to update the /etc/hosts file. As before, edit it in vi:

[root@web01 ~]# vi /etc/hosts

You should see a line with the server’s old static IP followed by the hostname, per the below:

127.0.0.1   localhost
172.16.0.21     web01

Change the line to reflect the new IP, in my case 10.10.10.50:

127.0.0.1   localhost
10.10.10.50     web01

Exit and save the changes to the file.

6. To commit the changes made in the network and hosts files above in steps 4 & 5 respectively you can either reboot the server or restart the network services. If it’s a production system and you want to avoid any downtime restart the network services by running:

[root@web01 ~]# service network restart

After restarting the network services the operating system will use the new IP.

References:
How To Configure Static IP On CentOS 6
How to Configure a Static IP Address in Red Hat, CentOS and Fedora
Setup static IP in redhat 6