Networking HowTos
Networking HowTos

Setting a static IP address in CentOS

January 23, 2012 CentOS, Linux

During a default install of CentOS it will try to either automatically obtain an IP address using DHCP, or it wont even set up the network interface automatically. While that may be fine for most desktop users, if you are wanting to set up a server, the chances are you will be wanting it to always have the same IP address.
There are two options for ensuring you get/use the same IP address at every boot. You could set up a IP address reservation on the DHCP server based on the hardware/mac address of the network interface, or alternativly, set the operating system to use a static IP address. If for whatever reason you dont want to go down the path of setting a DHCP reservation (you may not have access to the DHCP server; unsure how to set it up; etc), the only option is to set a static IP address on the PC itself.
The following steps will guide you through setting up your computer to use a static IP address on CentOS.
Assuming the network interface is supported and detected under CentOS, you will find that the configuration files required to set the IP address can be found under /etc/sysconfig/network-scripts.
The files to look for are ifcfg-eth# where # is the network interface number, starting from 0.
This example will assume that you want to set up the static IP address on the first network interface.
Open up the /etc/sysconfig/network-scripts/ifcfg-eth0 file in your favorite editor. This guide will be using the “vi” editor, but you can use “nano” or another editor.
Make sure you are logged in as “root”, or a user with root privileges.

# vi /etc/sysconfig/network-scripts/ifcfg-eth0

You should see the lines similar to the following in the editor.


Most of the lines in this file will be changing, so your best option is to remove all the lines except the first one, and copy the example format from the example below.
If you dont want to delete the lines, simply comment them out. This allows you to change it back easily at a later date if you want to. The # character at the beginning of the line indicates that it is commented out, and will be ignored.
Make sure you change the relevant settings to suit your network.


If you are unsure what settings should go in this file, you can always run the ‘ifconfig’ command, which will output the current IP address settings obtained via DHCP (if DHCP is set up). ┬áThe default gateway address can be obtained by using the ‘route’ command.
Once you have finished with the changes to the file, save and quit the editor.
You will also need to set up the DNS nameserver settings manually. These are stored in the /etc/resolv.conf file.
Open this file in your editor.

# vi /etc/resolv.conf

you will want at least 1 nameserver set up in this file. If one already exists, and is valid, great. If not, add one or more nameserver records in the format below.


Depending on your setup, this could be your router’s IP, your ISP’s DNS server IP, or a 3rd party’s DNS server, such as Google’s ( and, or OpenDNS.
Save and exit the editor.
To activate the changes you will need to reboot the PC, or simply restart networking (recommended).

# /etc/init.d/network restart


# service network restart

Sample output:

# /etc/init.d/network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]

Your PC should now retain its IP address across reboots, or even when a DHCP server is not working. Use the ‘ifconfig’ command to confirm the IP address has been updated successfully.

You Might Also Like

  • Stanly Steinberg January 14, 2013 at 8:48 am

    I am trying to set eth1 to a static IP address and set up the files as directed.
    ifconfig -a
    tells me that this didn’t work.
    By the way there was an eth0, but no eth1 file.
    Cannot receive email till this is fixed.

  • Stanly Steinberg January 14, 2013 at 8:50 am

    I am trying to set eth1 to a static IP address and set up the files as directed.
    ifconfig -a
    tells me that this didn’t work.
    By the way there was an eth0, but no eth1 file.
    Cannot receive email till this is fixed.
    Oh, I am installing a new clean centos 6.3 on a dell machine.

    • admin January 14, 2013 at 9:07 am

      Hi Stan,
      You should be able to copy the eth0 file and make it ifcfg-eth1, and edit the file to reference eth1 instead (and take out the hwaddr line if it exists).

  • Stanly Steinberg January 14, 2013 at 10:18 pm

    Thanks for the reply.
    I did try what you suggest. Reasonably sure I had this right as I had looked
    at several examples and also at things on the web. I restarted the network and ifconfig
    didn’t show a static link. On my old computer I get inet addr:
    I also restarted the computer without any luck. I started to think that maybe CentOS 6.3 had some “special” features. I will be on vacation for a couple of weeks, but will return to getting this computer working correctly.
    By the way, for years I have run my own mail and http servers on my personal computer that only served me. This was because other servers such as my universities had restrictions that caused me problems. I am assuming that even in this simple case, I need a static IP???