Linux Basics - Set A Static IP On CentOS

Version 1.0

This tutorial explains the configuration of a static IP address (IPv4 and IPv6), the hostname and nameservers on CentOS. To be compatible with Desktop and Server Systems, we do the configuration on the shell.

1 Preliminary Note

Suppose you are working in a data center & your boss puts a minimal CentOS server setup & you need to configure it in the running environment. Yes it is little painstaking, but not very tough task. In my case I have a dumb CentOS server which was installed by someone in his networking environment & I want to make it functional in my static IP environment. Suppose I have a vacant IP & I will implement it in my environment. My IP details are as follows:


  • IP address:
  • Subnet:
  • Gateway:


  • IP address: 2001:db8::c0ca:1eaf
  • Netmask 64
  • Gateway 2001:db8::1ead:ed:beef

DNS resolving nameservers

  • Nameserver 1:
  • Nameserver 2:
The above values have to be adjusted for your local network enviroment. Just the FNS resolving nameservers can be used on most networks as these IP addresses belong to the public nameservers from Google.

2 Implementation

I will do a configuration file editing with the editor like vi. But you may use any other shell editor like nano or joe instead. The file name for the first network card (eth0) is /etc/sysconfig/network-scripts/ifcfg-eth0

I will first make backup of my original file as /etc/sysconfig/network-scripts/ifcfg-eth0.bak & then proceed for the changes in /etc/sysconfig/network-scripts/ifcfg-eth0

mv  /etc/sysconfig/network-scripts/ifcfg-eth0  /etc/sysconfig/network-scripts/ifcfg-eth0.bak

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

I will change  the file like this:

#My IP description
# IPv-4

NAME="System eth0"
IPADDR= 2001:db8::c0ca:1eaf

Only the Lines IPADDR and NETMASK have to be adjusted. Do not change the other lines.

For IPv6 You just have to add the entries below:

vi /etc/sysconfig/network


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


3 DNS configuration

DNS can be added in the file /etc/resolv.conf

vi /etc/resolv.conf


You can add 2 or more namserver lines. Your system will try the other ones in case that the first nameserver is unreachable.

4 Hostname

In my case the hostname is to set the hostname, I will edit the /etc/hosts file:

vi /etc/hosts   localhost localhost.localdomain localhost4 localhost4.localdomain4     server1

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

and the resolv.conf file:

vi /etc/resolv.conf


and reboot the server.


Check your hostname with the command below. The new hostname will be applied after reboot only


5 Advanced networking and virtual network interfaces

 I am using CentOS Linux and I would like to  create alias for eth0:0 so that I can have multiple IP address. I will implemented by creating a file as follows:

vi /etc/sysconfig/network-scripts/ifcfg-eth0:0

#IP Aliasing

DEVICE="eth0:0" BOOTPROTO="static"
HWADDR=20:89:84:c8:12:8a NM_CONTROLLED="no" ONBOOT="yes" TYPE="Ethernet" IPADDR= NETMASK= GATEWAY= DNS1= DNS1=

Here I have done the IP aliasing for the IP, it could vary as per your requirement.

6 Services

After any change in the networking files you need to restart the network services as follows:

/etc/init.d/network restart

After the service restart you can check the changes as:


The output will confirm the changes done statically. It will be almost similar like this:

root@server1:~# ifconfig
eth0      Link encap:Ethernet  HWaddr 20:89:84:c8:12:8a 
          inet addr:  Bcast:  Mask:
          inet6 addr: 2001:db8::c0ca:1eaf/64 Scope:Link
          RX packets:200197 errors:0 dropped:67 overruns:0 frame:0
          TX packets:69689 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:64103748 (64.1 MB)  TX bytes:14106191 (14.1 MB)

eth0:0    Link encap:Ethernet  HWaddr 20:89:84:c8:12:8a 
          inet addr:  Bcast:  Mask:

lo        Link encap:Local Loopback 
          inet addr:  Mask:
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:10365 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10365 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:875114 (875.1 KB)  TX bytes:875114 (875.1 KB)


Note Above values will differ in your case.

Share this page:

2 Comment(s)

Add comment


From: Luke Hamiltn

In step 2, you wanted to make a backup with the following command: 

mv  /etc/sysconfig/network-scripts/ifcfg-eth0  /etc/sysconfig/network-scripts/ifcfg-eth0.bak

However, you should have used "cp" instead of "mv".  "mv" will rename the original file instead of creating a backup of the original.  Use the folliwng instead:

cp  /etc/sysconfig/network-scripts/ifcfg-eth0  /etc/sysconfig/network-scripts/ifcfg-eth0.bak

From: dongchao

yes,you are absolutely right !but i am still wondering how can i set the specific ip address as well as the gateway address to be compatible with my environment.because of this problem i have already been haunted for  a whole day.can you help me ? thanks a lot !