To configure a static IP adress on CentOS 7, follow these steps:
Identify the Network Interface
First, determine the name of your active network interface:
# ip addr show
# or
# nmcli connection show
Common interface names include ens33, eth0, etc.
Edit the Network Configuration File
Navigate to the network scripts directory and edit the configuration file for your interface (e.g., ifcfg-ens33):
# cd /etc/sysconfig/network-scripts/
# vi ifcfg-ens33
Example for Bridged Mode
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.0.200
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
DNS1=8.8.8.8
DNS2=8.8.4.4
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens33
UUID=95b614cd-79b0-4755-b08d-99f1cca7271b
DEVICE=ens33
ONBOOT=yes
Example for NAT Mode (Virtual Machine)
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.26.128
NETMASK=255.255.255.0
GATEWAY=192.168.26.1
DNS1=8.8.8.8
DNS2=8.8.4.4
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens33
UUID=a4319fdd-5bf4-4d9d-87a2-7d4f724fa952
DEVICE=ens33
ONBOOT=yes
Note: The example uses
192.168.26.0/24as a typical NAT subnet in virtualization envirnoments like VMware or VirtualBox. Adjust according to your actual network setup.
Restart the Network Service
Apply the changes by restarting the network service:
# systemctl restart NetworkManager
# or
# systemctl restart network
Configure DNS Resolution (Optional)
Edit /etc/resolv.conf to specify DNS servers explicitly (though this is often managed automatically when DNS entries are set in the interface config):
nameserver 8.8.8.8
nameserver 8.8.4.4
The nameserver directive defines DNS servers used for domain resolution. Entries are queried in order.
Set Hostname and Local Host Mapping
To set the system hostname:
# vi /etc/hostname
To map hostnames to IP addresses locally (useful for development or internal services):
# vi /etc/hosts
Add lines in the format: IP_address hostname [alias...]