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 192.168.0.100 & I will implement it in my environment. My IP details are as follows:


IPv4

  • IP address: 192.168.0.100
  • Subnet: 255.255.255.0
  • Gateway: 192.168.0.1

IPv-6

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

DNS resolving nameservers

  • Nameserver 1: 8.8.8.8
  • Nameserver 2: 8.8.4.4
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

DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=20:89:84:c8:12:8a
TYPE=Ethernet
BOOTPROTO=static
NAME="System eth0"
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
IPADDR= 2001:db8::c0ca:1eaf
NETMASK=255.255.255.0

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


[...]
NETWORKING_IPV6=yes

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

[...]
#IPv-6
IPV6INIT=yes
IPV6ADDR=2001:db8::c0ca:1eaf
IPV6_DEFAULTGW=2001:db8::1ead:ed:beef

3 DNS configuration

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

vi /etc/resolv.conf

[...]
nameserver	8.8.8.8
nameserver	8.8.4.4


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 server1.example.com to set the hostname, I will edit the /etc/hosts file:

vi /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.0.100   server1.example.com     server1

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

and the resolv.conf file:

vi /etc/resolv.conf


NETWORKING=yes
HOSTNAME=server1.example.com
GATEWAY=192.168.0.1
[...]

and reboot the server.

reboot

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

hostname

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=192.168.0.108 NETMASK=255.255.255.0 GATEWAY=192.168.0.1 DNS1=8.8.8.8 DNS1=8.8.4.4

Here I have done the IP aliasing for the IP 192.168.0.108, 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:

ifconfig

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

[email protected]:~# ifconfig
eth0      Link encap:Ethernet  HWaddr 20:89:84:c8:12:8a 
          inet addr:192.168.0.100  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: 2001:db8::c0ca:1eaf/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          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:192.168.0.108  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          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:

Suggested articles

8 Comment(s)

Add comment

Comments

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

By: 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 !

By: Der PCFreak

In CentOS 7 there is a tool called "nmtui" it is installed by default, even in the minimum installation. It is a text-based "graphical" tool where setting up networking is very easy and straightforward. I would only use the manual way if I want to script or automate networking setup and use "nmtui" wherever possible.

By: david

I'd suggest that you rename your copied file differently. The init scripts look for anything with "ifcfg-" and process it as a valid interface, so if you name the file OLD.ifcfg-eth0 instead, this will work cleaner.

By: Fabio

Are you sure that:"NETWORKING=yes HOSTNAME=server1.example.com GATEWAY=192.168.0.1"

has to be add in "/etc/resolv.conf" and not in "/etc/sysconfig/network"?

By: mohammad shamim khan

Hi team,

 

[[email protected] // /]# service network restartfgrep: ifcfg-ifcfg-: No such file or directoryfgrep: ifcfg-ifcfg-: No such file or directory/etc/init.d/network: line 207: ./ifcfg-ifcfg-: No such file or directoryShutting down loopback interface:                          [  OK  ]Bringing up loopback interface:                            [  OK  ]Bringing up interface eth0:  Device eth0 does not seem to be present, delaying initialization.                                                           [FAILED]fgrep: ifcfg-ifcfg-: No such file or directoryfgrep: ifcfg-ifcfg-: No such file or directoryfgrep: ifcfg-ifcfg-: No such file or directoryegrep: ifcfg-ifcfg-: No such file or directoryBringing up interface ifcfg-:  Device does not seem to be present, delaying initialization.                                                           [FAILED][[email protected] // /]#

please help for the same

By: Hitman47s1

Hello , i follow this steps and i create an other ip adress but when run with it a game server , it run normaly but can't connet to the server so i understand that this new ip not connected to enternet not like the defolt ip !

By: ITnavigator

This is not /etc/resolc.conf file, this it /etc/sysconfig/network:

NETWORKING=yes HOSTNAME=server1.example.com GATEWAY=192.168.0.1 [...]