Here’s a brief tutorial that shows you how to configure a static IP address for CentOS servers. In an environment where there’s no DHCP server to administer and distribute dynamic IP addresses, static IP addresses need to be setup for all your systems.

In most networked environments, servers do not get dynamic IP addresses. They are assigned static IPs and static DNS addresses.

If you’re running a CentOS 6/7 server, you may have to configure static IP address for it. This brief tutorial is going to show you how to do it in CentOS.

For this tutorial, we’re going to assign IP address 192.168.0.2 with NetMask 255.255.255.0 and gateway 192.168.0.1. Our server name will be srvr1.

This tutorial will also come in handy for those who are running lab environments using VMware or other virtualization software to test different setups.

I, for one, run many test environments using VMware workstation. I run Ubuntu, Windows, CentOS 7 and other operating systems. I also setup different IP environments.

One environment is running DHCP and others are static.

 

  • Setting Up Static IP 4 in CentOS

To setup static IP addresses in CentOS, first create backup of the default interface configuration. It’s always a good idea to create backup of important files before making changes to them. To do that, run the commands below.

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

Than run the commands below to begin editing the configuration file.

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

By default, the configuration file will look like the block below in a DHCPed environment.

 

DEVICE="eth0"
BOOTPROTO="dhcp"
HWADDR="08:00:27:2B:AD:65"
NM_CONTROLLED="yes"
ONBOOT=yes
TYPE=Ethernet
UUID="4fb06bd0-0bb2-7ffb-47f1-d6edd65f3e03"

 

To change the above setup to a static IP address configuration, replace the following line above as shown below.

BOOTPROTO="static"

Then add the IP address, network mask and gateway for the server at the end and save the file.

IPADDR="192.168.0.2"
NETMASK="255.255.255.0"
GATEWAY="192.168.0.1"

 

  • Setting Up Static IP 6 in CentOS

For IPv6 setup, open the configuration file again (ifcfg-eth0) and add the following lines. You may want to remove the IPv4 IP addresses.

IPV6INIT="yes"
IPV6_AUTOCONF="no"
IPV6ADDR=fe80:2001:eedc::0"
IPV6_DEFAULTGW="2001:c4b8::3ebd:3d:bcdf"

 

Save the file and go to the next configuration file. Next, open the file below

vi /etc/sysconfig/network

Then add or change the line to the line shown, only for IPv6 configuration below and save.

NETWORKING_IPV6=yes

 

  • Configuring DNS Addresses

Finally, go and configure the static DNS addresses for the server. To do that, open the resolv.conf below

vi /etc/resolv.conf

Then add/or change the nameserver you want to use. Here are Google DNS server as an example.

nameserver        8.8.8.8
nameserver         8.8.4.4

 

Next, go to the host file and device the server name and IP address.

vi /etc/hosts

Add the line as shown below to define the server static address. Replace the IP and server name to reflect your environment. Save the file and you’re done.
192.168.0.2    srvr1.example.com     srvr1

When you’re done, save the file and reboot the server. Do a hostname command and the new IP configuration shoud display.

Enjoy!