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 to 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:


We can see from the above that I have configured the static IP of 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:

NAME="System eth0"

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:


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:


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:


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:   localhost     web01

Change the line to reflect the new IP, in my case   localhost     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.

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

Set environment variable for a specific user in RHEL 6/CentOS 6 using csh shell

I was asked by one of the SAP Basis engineers in my project team to create an environment variable for a specific user (psoadm). He wanted me to create a variable called SECUDIR and set it to /home/psoadm/sec, per the below:

SECUDIR = /home/psoadm/sec

So the first thing to do was find out the type of shell was being used. To do this I logged into the server as root and then ran:

su - psoadm

This allows me to switch user from root to the psoadm user and assume psoadm’s own home directory and environment variables – it’s the equivalent of psoadm logging into a new session.

To confirm the shell being used run the following:

ps -p $$

As you can see from the output below the shell was csh (C Shell):

 PID  TTY          TIME CMD
 7872 pts/0    00:00:00 csh

Now that we know that our shell is csh the next step is to add the SECUDIR variable to the following files: $HOME/.cshrc and $HOME/.login. The $HOME variable relates to psoadm’s home directory and is the equivalent of ~. So to edit the $HOME/.cshrc file run the following:

nano ~/.cshrc

then scroll to the bottom of the file and add the following:

# Set SECUDIR for psoadm user
setenv SECUDIR /home/psoadm/sec

Hit CTRL + x to exit and you will see the following:

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?
Y Yes
N No  ^C Cancel

enter Y to save and then it will ask to confirm the file to write these changes to, per the below:

File Name to Write: /home/psoadm/.cshrc

Hit Enter to confirm.

Now follow the exact same steps to update the $HOME/.login file. In order for the SECUDIR variable to be available you, either logout of the current shell and log back in or run the source command against the files, per the below:

source ~/.cshrc
source ~/.login

Now enter echo $SECUDIR at the shell prompt like so:


And you will now see that the response correctly returns the value of SECUDIR as:


If your shell is bash, check the links directly below for advise on how to sent environment variables for the bash shell:

How to permanently export a variable in Linux
How do I set a user environment variable permanently not session?

That’s all folks! I hope this was useful.

C shell
What shell am I using?
Set environment variable in Unix
How to permanently export a variable in Linux
How do I set a user environment variable permanently not session?