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.
By default, the configuration file will look like the block below in a DHCPed environment.
To change the above setup to a static IP address configuration, replace the following line above as shown below.
Then add the IP address, network mask and gateway for the server at the end and save the file.
- 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.
Save the file and go to the next configuration file. Next, open the file below
Then add or change the line to the line shown, only for IPv6 configuration below and save.
- Configuring DNS Addresses
Finally, go and configure the static DNS addresses for the server. To do that, open the resolv.conf below
Then add/or change the nameserver you want to use. Here are Google DNS server as an example.
Next, go to the host file and device the server name and IP address.
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.