CentOS 7 Tutorial: static IP Address CentOS 7
Enjoying this content? Subscribe to the Channel!
The Ultimate Guide to Setting a Static IP Address in CentOS 7 (No More DHCP Headaches!)
Introduction: Taking Control of Your Network Configuration
Welcome back to Darren’s Tech Tutorials! If you’ve ever run a server, hosted a virtual machine, or managed a crucial piece of network infrastructure, you know the frustration of an IP address that constantly changes. This is the default behavior of Dynamic Host Configuration Protocol (DHCP), which is great for laptops but terrible for stable server environments.
When you rely on DHCP, every time you reboot your CentOS 7 machine, there’s a chance your IP address will shift. This breaks application configurations, stops network services from resolving correctly, and makes remote access a nightmare.
In this comprehensive guide, we are going to dive deep into the configuration files and show you the precise, step-by-step process for assigning a permanent, static IP address on CentOS 7. Once we’re done, your server will maintain the same address even after multiple reboots, guaranteeing stability and predictability!
Why Static IP is Essential for CentOS 7 Servers
Before we start editing files, let’s quickly confirm the “why.” By setting a static IP, you achieve:
- Reliability: The IP address is always the same, making firewall rules and port forwarding work consistently.
- Remote Access: SSH and other remote management tools always connect to the correct location.
- DNS Integrity: Internal DNS entries won’t break because the associated IP address vanished.
Let’s get started!
Prerequisites
To follow this guide successfully, you will need:
- Root or Sudo access to your CentOS 7 machine.
- The preferred network details: the desired Static IP Address, Subnet Mask, Default Gateway, and DNS Server addresses.
Step 1: Identifying Your Network Interface
CentOS 7 configurations rely on editing specific files corresponding to your Network Interface Card (NIC). First, we need to find the name of the interface we are configuring (e.g., eth0, enp0s3, or ens33).
Use the ip a command or the nmcli tool:
nmcli dev status
Look for the interface that shows a state of connected. For the purpose of this tutorial, we will assume your active interface configuration file is named ifcfg-eth0.
Step 2: Navigating to the Configuration Directory
All essential network configuration scripts are stored in the same location. Navigate to this directory using the cd command:
cd /etc/sysconfig/network-scripts/
Now, we will open the configuration file for your specific interface (replace eth0 with your actual interface name if different) using a text editor like vi or nano:
sudo vi ifcfg-eth0
Step 3: Modifying the Network Configuration File
When you open the file, you will see several lines of configuration. We need to change or add the following key parameters to switch from DHCP to a static configuration.
The initial file might look similar to this (showing DHCP settings):
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
NAME=eth0
ONBOOT=yes
We are going to modify the settings to look exactly like the example below. Make sure to replace the bracketed placeholder values with the actual network details for your specific environment.
| Parameter | Required Value | Description |
|---|---|---|
BOOTPROTO |
static |
Switches from dynamic IP assignment to static. |
ONBOOT |
yes |
Ensures the interface starts automatically upon boot. |
IPADDR |
[YOUR_STATIC_IP] |
The permanent IP address you are assigning. |
NETMASK |
[YOUR_NETMASK] |
The subnet mask for your network (e.g., 255.255.255.0). |
GATEWAY |
[YOUR_DEFAULT_GATEWAY] |
The IP address of your router. |
DNS1 |
[YOUR_PRIMARY_DNS] |
Primary DNS server (e.g., 8.8.8.8). |
Example Static Configuration File:
Edit your file (ifcfg-eth0) to match this structure, substituting your details:
TYPE=Ethernet
BOOTPROTO=static # CHANGE THIS LINE
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
NAME=eth0
UUID=f9b1c7c9-f1c9-4b1d-b8d9-29c36868d402
ONBOOT=yes # Ensure this is set to 'yes'
IPADDR=192.168.1.150 # YOUR STATIC IP ADDRESS
NETMASK=255.255.255.0 # YOUR NETMASK
GATEWAY=192.168.1.1 # YOUR DEFAULT GATEWAY
DNS1=8.8.8.8 # PRIMARY DNS SERVER
DNS2=8.8.4.4 # SECONDARY DNS SERVER
Once you have made these changes, save the file and exit the text editor.
Step 4: Applying the Changes and Verification
The final step is to restart the network service so that the system reads your new static configuration.
Run the following command:
sudo systemctl restart network
If the command executes successfully, your new static IP address should be active!
Verifying the New Static IP
To confirm that the change worked, use the ip a command and look for your interface:
ip a show eth0
The output should clearly show the new, manually assigned inet address matching the IPADDR you defined in Step 3.
Congratulations! You have successfully assigned a static IP address to your CentOS 7 machine. This address will remain persistent across all future reboots, giving you rock-solid stability.
Conclusion
That’s all there is to it! By taking a few minutes to configure your NIC files, you have eliminated the common instability issues caused by dynamic IP allocation on a system that needs permanent addressing.
This stability is vital whether you’re hosting web services, databases, or just need reliable SSH access to your virtual machine environment. Now you can reboot your server with peace of mind, knowing exactly where it lives on your network.
If this tutorial saved you a networking headache, please give this post a thumbs up, subscribe to Darren’s Tech Tutorials for more clear and accessible guides, and let me know in the comments below what static address you chose for your server! Happy networking!