How to configure a static IP address on CentOS 7

Question: On CentOS 7, I want to switch from DHCP to static IP address configuration with one of my network interfaces. What is a proper way to assign a static IP address to a network interface permanently on CentOS or RHEL 7?

If you want to set up a static IP address on a network interface in CentOS 7, there are several different ways to do it, varying depending on whether or not you want to use Network Manager for that.

Network Manager is a dynamic network control and configuration system that attempts to keep network devices and connections up and active when they are available). CentOS/RHEL 7 comes with Network Manager service installed and enabled by default.

To verify the status of Network Manager service:

$ systemctl status NetworkManager.service

To check which network interface is managed by Network Manager, run:

$ nmcli dev status

If the output of nmcli shows "connected" for a particular interface (e.g., enp0s3 in the example), it means that the interface is managed by Network Manager. You can easily disable Network Manager for a particular interface, so that you can configure it on your own for a static IP address.

Here are two different ways to assign a static IP address to a network interface on CentOS 7. We will be configuring a network interface named enp0s3.

Configure a Static IP Address without Network Manager

Go to the /etc/sysconfig/network-scripts directory, and locate its configuration file (ifcfg-enp0s3). Create it if not found.

Open the configuration file and edit the following variables:

In the above, "NM_CONTROLLED=no" indicates that this interface will be set up using this configuration file, instead of being managed by Network Manager service. "ONBOOT=yes" tells the system to bring up the interface during boot.

Save changes and restart the network service using the following command:

# systemctl restart network.service

Now verify that the interface has been properly configured:

# ip add

Configure a Static IP Address with Network Manager

If you want to use Network Manager to manage the interface, you can use nmtui (Network Manager Text User Interface) which provides a way to configure Network Manager in a terminal environment.

Before using nmtui, first set "NM_CONTROLLED=yes" in /etc/sysconfig/network-scripts/ifcfg-enp0s3.

Now let's install nmtui as follows.

# yum install NetworkManager-tui

Then go ahead and edit the Network Manager configuration of enp0s3 interface:

# nmtui edit enp0s3

The following screen will allow us to manually enter the same information that is contained in /etc/sysconfig/network-scripts/ifcfg-enp0s3.

Use the arrow keys to navigate this screen, press Enter to select from a list of values (or fill in the desired values), and finally click OK at the bottom right:

Finally, restart the network service.

# systemctl restart network.service

and you're ready to go.

Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

12 thoughts on “How to configure a static IP address on CentOS 7

  1. After I did this, I was no longer able to reach the internet. The IP changed. And I still have my local network, but I can no longer reach the internet.

    • Add
      as the default gateway, remember to change x.x.x.x as your default gateway, you may find it when you set BOOTPROTO=dhcp (reboot, login again) and in the cosole window type ip route check the line "default via x.x.x.x..." this will be your default gateway.
      Also check the file "/etc/resolv.conf", it should have a line nameserver with a valid DNS server, for example "nameserver"

      • As Juan mentions, you must have GATEWAY=X.X.X.X in your /etc/sysconfig/network-scripts/ifcfs- file, not GATEWAY0=X.X.X.X

        That GATEWAY0 must be a Network Manager convention.
        You may also need to change DNS1 to DNS.

  2. In my case, swapping from DHCP to static (removing the line that says ="static") a reboot was needed. systemctl restart network.service did not do the job, it just kept it's DHCP assigned address until reboot.

  3. On a base centOS 7 install I added the package and changed IP address from DHCP to static using its latest network module, but networking was messed up. The command "nmcli dev status" showed the connection "ens192" in STATE connected, but:

    Problem: "nmtui edit ens192" failed with message: nmtui-edit: no such connection 'ens192'

    The NAME entry in my /etc/sysconfig/network-scripts/ifcfg-ens192 file was empty (""), so I:

    1. set NAME="ens192" to match the file/dev
    2. did a "systemctl restart network.service"

    After this, "nmtui edit ens192" worked fine.

    Note: I also opted to add a missing "NM_CONTROLLED=yes", to make things clear, even though I found it worked without it (I presume "yes" is now the default). However, removing "NAME" completely did not work, so it seems the redundancy with the file/dev name is required.

  4. Thanks for this article. I was a bit stuck. I did what you described above for my situation and it worked perfectly. I did the same modifications to the ifcfg-eth0 file on a kvm vm, but I left Network Manager still running. The internal network worked fine, but the gateway did not work and I could not see anything outside the local kvm network. Once I indicated Network Manager should not run as you demonstrated above, everything worked perfectly. You did what I did on Red Hat 6. Somehow I thought Red Hat 7 would behave a bit differently. I am just wondering what the network manager tui (which I did not try) does that makes the gateway work... Any ideas?

  5. I could not get the non-network manager procedure to work unless I disabled network manager

    systemctl mask NetworkManager
    systemctl stop NetworkManager
    systemctl restart network

  6. you should specify at least one nameserver in the /etc/resolv.conf file, by adding a line in /etc/resov.conf
    "nameserver or your dns server address"

  7. systemctl status NetworkManager.service
    Loaded: not-found (Reason: No such file or directory)
    Active: inactive (dead)

    I got this message. I would like to set up a newly bought IP address to my VPS. How should I do in the next step?

  8. thank you! it was very interesting but can you tell me what we can do if we have two network interfaces?

    • Very easy. For two interfaces eth0 and eth1:
      If you don't use NetworkManager, create /etc/sysconfig/network-scripts/ifcfg-eth0 and /etc/sysconfig/network-scripts/ifcfg-eth1, and populate them as instructed in the tutorial.
      If you use NetworkManager, configure eth0 and eth1 using nmtui separately as follows.
      # nmtui edit eth0
      # nmtui edit eth1

Leave a comment

Your email address will not be published. Required fields are marked *