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

Change hostname on Ubuntu Server 14.04

20151229113855Sometimes I forget some of the steps to changing the hostname on Ubtuntu as they vary slightly to other Linux distros such as RHEL or CentOS. The process is just as simple but there is a slight difference in that we need to amend the hostname file. So without further ado, here are the steps to change the hostname on Ubunutu Server 14.04:

The Steps

1. The first step is to run the hostname command followed by the new server name and hit Enter:

admin@web01:~$ sudo hostname web02

2. As you can see from the above the current hostname was web01, I changed it to web02. Running the hostname command on it’s own will show you the current and new hostname of web02:

admin@web01:~$ hostname
web02

The hostname has now been changed, however this change is not permanent. In other words if you reboot the server the changes will not survive, the hostname will revert back to its previous name. To make the hostname change permanent continue to step 3 below.

3. The next step is to update the /etc/sysconfig/network file by opening it with vi or any other editor of your choice:

admin@web01:~$ sudo vi /etc/hostname

4. Within the file you will see your old hostname, in my case web01:

web01

Simply change the hostname to the new name, in my case it is web02:

web02

Exit and save the changes to the file.

5. We have one final file to update, which is /etc/hosts. Edit it vi:

admin@web01:~$ sudo vi /etc/hosts

You should see the lines below, the bottom line showing your old hostname, in my case web01:

127.0.0.1   localhost
127.0.1.1   web01

Once again, change it to reflect the new desired hostname, in my case web02:

127.0.0.1   localhost
127.0.1.1   web02

Exit and save the changes to the file. The changes so far should be enough to ensure your system recognises the new hostname. I like to restart networking services to ensure that the new changes take effect, to do this proceed to step 6 below.

6. To commit the changes made in the network, hostname and hosts files above in steps 4 & 5 respectively you can either reboot the server or restart the network services. Restart the network services by running:

admin@web01:~$ sudo ifdown eth0 && sudo ifup eth0

Please note that in previous version of Ubuntu server you could run service networking restart to restart services. This no longer works in Ubuntu Server 14.04. You need to shut down the interface and then bring it back up per step 6 above.

After restarting the network services the operating system and all applications will use the new hostname.

References:
How to successfully restart a network without reboot over SSH?
How do I change the hostname without a restart?