
Setting a static IP address
This recipe shows you how to configure a static IP address. Unless you configured a static address during installation, CentOS uses the Dynamic Host Configuration Protocol (DHCP) to obtain an IP address to communicate across the network. Using a dynamically assigned address is fine for most desktop and laptop systems, but those that host e-mail servers, file sharing and print services, and web servers should have an address that doesn't change. The static address provides a stable, known location on the network where users can access a system's services.
Getting ready
This recipe requires a CentOS system with a working network connection and administrative privileges provided by logging in with the root
account. It assumes that your primary Ethernet device is named enp0s3
and is currently configured with DHCP. If your device is named differently, substitute its name appropriately in the following commands.
How to do it...
Follow these steps to configure a static IP address:
- Open the Ethernet device's configuration file, found under
/etc/ sysconfig/network-scripts
,with your text editor:vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
- Change the value of
BOOTPROTO
to none:BOOTPROTO="none"
- At the end of the file, add the
IPADDR
,NETMASK
, andBROADCAST
entries to set the desired IP address. Assign them values that properly reflect your network:IPADDR="192.168.56.100" NETMASK="255.255.255.0" BROADCAST="192.168.56.255"
The interface is configured with a static IP address
- Save your changes and close the file.
- Open the
/etc/sysconfig/network
file using your editor:vi /etc/sysconfig/network
- Add a
GATEWAY
entry to identify your network's gateway:GATEWAY="192.168.56.1"
- Save your changes and close the file.
- Restart the networking service for the configuration changes to take effect:
systemctl restart network.service
How it works...
In this recipe, you learned how to assign a static IP address to an Ethernet device. It assumed the name of your primary Ethernet device to be enp0s3
, thus ifcfg-enp0s3
would be the name of the device's configuration file. If your device is named differently (for example, eth0
, eno1677
, and so on) then you need to adjust the recipe's directions accordingly.
First, we changed the value for BOOTPROTO
from dhcp
, the protocol used to obtain an IP address dynamically, to none
since we are setting the address ourselves. Then we added the IPADDR
, NETMASK
, and BROADCAST
entries to provide the details of the static IP address. Next, we specified the network's default gateway using GATEWAY
in /etc/ sysconfig/network
. This allows us to route traffic beyond the local subnetwork.
After you restart the networking service, you can confirm the new address using the ip
command. ip addr show
will display information about the current state of your system's network devices:

ip addr show displays your system's networking information
See also
For more information on configuring network settings in CentOS, refer to the Configure IP Networking chapter in the RHEL 7 Networking Guide (https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Networking_Guide/ch-Configure_IP_Networking.html).