How to Set the Static IP Address Using CLI in Fedora/CentOS Linux

How to set the static IP address for CentOS 7/Fedora 22+ is introduced in this post. Here, we assume the Linux is using NetworkManager to manage the network. All steps are done as root.

Find the interface you want to set the address

Command

ifconfig -a

lists all interfaces.

Find the interface name to set, such as enp2s0.

Edit the interface’s configuration file

For interface NAME, the configuration file is stored in /etc/sysconfig/network-scripts/ifcfg-NAME. For the above example of enp2s0, the configuration file is /etc/sysconfig/network-scripts/ifcfg-enp2s0.

As an example, to set its IP to 1.2.3.4 with netmask 255.255.255.0, gateway 4.3.2.1, DNS 114.114.114.114, the content will be like

NM_CONTROLLED="yes"
DEVICE="enp2s0"
BOOTPROTO=static
ONBOOT="yes"
IPADDR=1.2.3.4
NETMASK=255.255.255.0
GATEWAY=4.3.2.1
DNS1=114.114.114.114

For more details of the configuration and other options, you may check the the Interface Configuration Options from this QA.

Restart network/NetworkManager services

You will need to restart the network/Networkmanager services to make the new configuration take effect

# systemctl restart network.service
# systemctl restart NeworkManager.service

Then you can use ifconfig -a again to verify whether the IP is set successfully.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

One comment:

Leave a Reply

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