Configuring a Static IP Address in CentOS

centos static ip

If you are using CentOS, you may need to configure a static IP address for your machine. A static IP address is a fixed IP address that does not change, unlike a dynamic IP address, which can change every time you connect to the Internet.

In this article, we will guide you through the process of configuring a static IP address in CentOS. We will provide a detailed description, illustrate its usage with code examples, and explain any related concepts or methods that may help to clarify the topic.

Prerequisites

Before we begin, make sure you have the following:

  • A CentOS machine
  • Root access
  • A basic understanding of the Linux command line

Steps to Configure a Static IP Address in CentOS

Step 1: Identify the Network Interface

The first step in configuring a static IP address is to identify the network interface that you want to configure. To do this, run the following command:

ip addr

This will display a list of all the network interfaces on your machine. Look for the interface that you want to configure, which will typically be named eth0 or enp0s3.

Step 2: Configure the IP Address

Once you have identified the network interface, you can configure the IP address. To do this, open the network configuration file for the interface using a text editor. In this example, we will use the nano editor:

sudo nano /etc/sysconfig/network-scripts/ifcfg-enp0s3

This will open the network configuration file for the enp0s3 interface. Look for the following lines:

BOOTPROTO=dhcp
ONBOOT=yes

Change BOOTPROTO to static and add the following lines:

IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

Replace 192.168.1.100 with the IP address that you want to assign to the interface. Replace 255.255.255.0 with the subnet mask for your network. Replace 192.168.1.1 with the IP address of your default gateway. Replace 8.8.8.8 and 8.8.4.4 with the IP addresses of your DNS servers.

Save the file and exit the text editor.

Step 3: Restart the Network Service

After you have configured the IP address, you need to restart the network service for the changes to take effect. To do this, run the following command:

sudo systemctl restart network

Step 4: Verify the Configuration

To verify that the configuration was successful, run the following command:

ip addr

This will display the network interfaces on your machine, along with their IP addresses. Look for the interface that you configured and verify that the IP address, subnet mask, default gateway, and DNS servers are correct.

Conclusion

Configuring a static IP address in CentOS is a simple process that can be completed in just a few steps. By following the steps outlined in this article, you can easily configure a static IP address for your machine. Remember to save your changes and restart the network service for the changes to take effect.