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!
Frequently Asked Questions
How to configure a static IP address for CentOS servers?
To configure a static IP address for CentOS servers, you need to edit the network interface configuration file and set the IP address, netmask, and gateway. This ensures the server has a fixed IP address.
What is the importance of setting up static IP addresses in networked environments?
In networked environments, setting up static IP addresses ensures consistent and reliable connectivity as servers do not change IP addresses. This is crucial for services that require a fixed IP.
What is the process of setting up static IP 4 in CentOS?
To set up static IP 4 in CentOS, you need to backup the default interface configuration, edit the configuration file to change BOOTPROTO to 'static', and then add the IP address, netmask, and gateway for the server.
How can you create a backup of the default interface configuration in CentOS?
To create a backup of the default interface configuration in CentOS, you can use the command 'mv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.orig'. This ensures you have a copy before making changes.
What is the default configuration of the network interface file in CentOS for DHCP setup?
In CentOS, the default configuration of the network interface file for DHCP setup includes parameters like DEVICE, BOOTPROTO, HWADDR, NM_CONTROLLED, ONBOOT, TYPE, and UUID.
Why is it recommended to create a backup before editing the interface configuration file in CentOS?
Creating a backup before editing the interface configuration file in CentOS is recommended to ensure that you can revert back to the original settings in case of any issues or misconfigurations.
What are some common virtualization software used for running lab environments?
Common virtualization software used for running lab environments include VMware Workstation, VirtualBox, Hyper-V, and others. These tools allow users to create and test different setups.
How can you change a DHCP setup to a static IP address configuration in CentOS?
To change a DHCP setup to a static IP address configuration in CentOS, you need to edit the network interface file and replace 'BOOTPROTO="dhcp"' with 'BOOTPROTO="static"'. Then add the static IP, netmask, and gateway.