How to configure a static IP address on CentOS 7(CentOS7静态IP地址设置)

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

How to configure a static IP address on CentOS 7(CentOS7静态IP地址设置)

<img src="//bbsmax.ikafan.com/static/L3Byb3h5L2h0dHBzL2Zhcm00LnN0YXRpY2ZsaWNrci5jb20vMzg2MS8xNTI5NTgwMjcxMV9hMTAyYTM1NzRkX3ouanBn.jpg">

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.

How to configure a static IP address on CentOS 7(CentOS7静态IP地址设置)

<img src="//bbsmax.ikafan.com/static/L3Byb3h5L2h0dHBzL2Zhcm00LnN0YXRpY2ZsaWNrci5jb20vMzkxMS8xNTExMjM5OTk3N19kM2RmOGUxNWY1X3ouanBn.jpg">

Open the configuration file and edit the following variables:

How to configure a static IP address on CentOS 7(CentOS7静态IP地址设置)

<img src="//bbsmax.ikafan.com/static/L3Byb3h5L2h0dHBzL2Zhcm00LnN0YXRpY2ZsaWNrci5jb20vMzg4MC8xNTExMjE4NDE5OV9mNGNiZjI2OWE2LmpwZw==.jpg">

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

How to configure a static IP address on CentOS 7(CentOS7静态IP地址设置)

<img src="//bbsmax.ikafan.com/static/L3Byb3h5L2h0dHBzL2Zhcm02LnN0YXRpY2ZsaWNrci5jb20vNTU5My8xNTExMjM5Nzk0N19hYzY5YTMzZmI0X3ouanBn.jpg">

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:

How to configure a static IP address on CentOS 7(CentOS7静态IP地址设置)

<img src="//bbsmax.ikafan.com/static/L3Byb3h5L2h0dHBzL2Zhcm00LnN0YXRpY2ZsaWNrci5jb20vMzg3OC8xNTI5NTgwNDUyMV80MTY1Yzk3ODI4X3ouanBn.jpg">

Finally, restart the network service.

# systemctl restart network.service

and you're ready to go.

上一篇:【转】Cocos2d-x纹理优化的一些方案——2013-08-26 22


下一篇:ORACLE透明网关访问SQL Server配置总结