How to: Setup a Static IP Address on CentOS

This guide will show you how you can setup a static IP address on CentOS. To do this, you will need to have a basic understanding of networking.

Prerequisites

In order to follow this tutorial, you will need the following

Sudo Privileges

Basic understanding of Networking

Disabling Network Managers

The first thing we are going to be doing is to stop the Network manager from trying to automatically handle the network for us. This will stop all of our settings been overridden at a later date. To do this, we can type

sudo service NetworkManager stop

After this, we will permanently turn this off and stop it been enabled again on startup.

sudo chkconfig NetworkManager off

Viewing Current Network Info

As your adapter may be named something differently and you may also want to check on your current network setup, you should use this command.

ip addr

Here you can see the adapter name and the current IP.

Editing Config File

Before we edit the config file, you should make sure that you are editing the correct one. You can view the available files to edit using this command

ls /etc/sysconfig/network-scripts/

You should look for a file which is ifcfg- followed by the adapter name seen when you did the ip addr command.

Once we have done this, we can go through and edit the configuration file for the network adapter so that we can tell it which address to use. Make sure to use the correct file which was identified in the previous step. If you get an error about nano not been installed, follow this guide

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

This file should look a little something like this:

You will need to alter the line highlighted to read

BOOTPROTO="static"

Once we have done this, we can set up our new IP at the bottom of the file. To do this, you will need to add the following three lines but make sure to put in the information which is appropriate to your network configuration.

IPADDR=10.0.0.81 #The new ip you would like to use
GATEWAY=10.0.0.1 #The ip of your router
NETMASK=255.255.255.0 #The subnet mask which is normally 255.255.255.0

When updated, this should look like this:

You can now save (Ctrl + O and then Enter) and exit (Ctrl + X)

Updating Gateway and NameServers

We are now just going to add the gateway to another network config file just to make sure the system knows where to send the data when connecting to the web.

sudo nano /etc/sysconfig/network

In this, we just need to add the following:

GATEWAY=10.0.0.1 #Your gateway

Once you have done this, saved and closed the file, we can go to add the name servers we would like to use to resolve dns. The file we will need to edit is

You can add as many nameservers as you would like. Again, save and close this file.

Once we have done all this, everything should be done

Restarting Network Adapter and Testing

We can now restart our network interface to make sure our changes have been taken into account.

sudo systemctl restart network

We can then also ping google to make sure we have network connectivity and that our namesevers are working.

ping google.com

If you go ahead and restart your server, you should see that everything is still working and using the details you used. If this is not the case, please make sure you follow the first few steps to disable the NetworkManager.