Static IP on CentOS 7 +

This guide is created for people who want to set a static IP address on their CentOS computer or virtual machine.

Introduction

Setting a static IP address is a very important thing to do if you want to set-up a webserver or web-application server. If you use a dynamic IP your IP address might change on every reboot or when the lease expires. Resulting in other application not being able to reach the server.

Preparation

To follow the steps of this guide please make sure you have the following:

  • An operating system running CentOS, Fedora or openSUSE
  • A text editor to edit configuration files
  • Access to the terminal of your OS, either using it locally or through SSH
  • About 5-10 minutes of your time

If you don’t have a text editor installed, you can easily do so by running ‘sudo yum install nano‘, please make sure you have access to the internet so you can download the packages.

Listing the Network Adapters

First we need to check which network adapter you’re currently using. You can do so by using the following command:

ip link show

This will show a list of the current active adapters. The first one is often the loopback adapter (lo). In the picture below it shows that number 2 (eth0) is my active adapter. Remember the name of the adapter.

Static IP Adapters

Getting a Static IP

Now we have the name of the active adapter, we can change the configuration file of the named adapter. Change your directory to:

cd /etc/sysconfig/network-scripts/

You’re now in the directory where the config file is stored. Use this command to show a list of all the files in your directory:

ls

As you can see there is a file called ‘ifcfg-<your_adapter_name>‘. We need to open this file with a text editor:

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

Do not forget to change the ‘eth0’ to your own adapter name.

List Config Files

You will see a list of options to edit.

TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
NAME="eth0"
UUID="5e03cd12-fa1a-448b-bb01-5a7e00ea1652"
DEVICE="eth0"
ONBOOT="yes"
IPADDR="10.40.14.200"
PREFIX="24"
GATEWAY="10.40.14.1"
DNS1="10.40.14.1"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_PRIVACY="no"

Edit the options to your desire. To change the IP Address, change the line of IPADDR=”10.40.14.200″ to your own. Remember to change the DNS server and gateway accordingly.

Press CTRL-O to save your changes, or CTRL-X to close. To apply your changes, restart your networking service by using:

service network restart

You now have a Static IP address!