Configuring a static IP on Centos7

Setting a static IP address on CentOS7

When CentOS is installed; it will try to obtain an IP address automatically via DHCP; therfore it is not unusual to need to assign it a static IP address (especially if it will be used as a server). this short article will show you how to configure static IP address in CentOS system.

1. Locate the Ethernet adapter name
Navigate to the “/etc/sysconfig/network-scripts” directory and view the contents

cd /etc/sysconfig/network-scripts
ls

In our example, our network card is called ‘ifcfg-ens160’

2. Edit the network interface
Using a text editor (such as “vi”) we can edit the Ethernet adapter settings

sudo vi /etc/sysconfig/network-scripts/ifcfg-ens160

3. Ethernet adapter parameters
Make the following changes to the Ethernet adapter information

BOOTPROTO=dhcp  <<– Change the “dhcp” to “static”
IPADDR=INSERT IP HERE  <<– Add this line
NETMASK=INSERT NETMASK HERE <<– Add this line
GATEWAY=INSERT DEFAULT GATEWAY HERE <<– Add this line
ONBOOT=yes <<– Change this line from “no” to “yes”

4. Save and Quit
Next we have to save our changes and quit the text editor.

:wq

5. Specify the DNS resolver address
Using a text editor (such as “vi”) we can edit the resolver configuration file.

vi /etc/resolv.conf

6. Specify the DNS resolver(s)
Within the “resolv.conf” configuration file we can list the DNS servers that we would like our installation of CentOS to use.

nameserver {INSERT #1 RESOLVER IP HERE}
nameserver {INSERT #1 RESOLVER IP HERE}

7. Save and Quit
Next we have to save our changes and quit the text editor.

:wq

8. Restart the network settings
In order for our network changes to take effect we have to restart the network service.

service network restart