How to configure a static IP address on CentOS 7 / RHEL 7

I am a new sysadmin and CentOS user. My system is configured to use DHCP. How can I switch from DHCP to static IP address on CentOS 7 desktop system? How do I setup a static TCP/IP address on my CentOS Linux 7 or Red Hat Enterprise Linux 7 server using command line option?

On CentOS 7 or RHEL 7 one need to use the NetworkManager daemon. It attempts to make networking configuration and operation as painless and automatic as possible by managing the primary network connection and other network interfaces, like Ethernet, WiFi, and Mobile Broadband devices. In this quick tutorial you will learn about configuring a network interface with a static IP address using ifcfg files located in /etc/sysconfig/network-scripts/ directory in a CentOS 7 and RHEL 7:

  1. Create a file named /etc/sysconfig/network-scripts/ifcfg-eth0 as follows:
  2. DEVICE=eth0
  3. BOOTPROTO=none
  4. ONBOOT=yes
  5. PREFIX=24
  6. IPADDR=192.168.2.203
  7. Restart network service: systemctl restart network

How do I list network interfaces?

Type the following command:
# ip a

Fig.01: List NICs in a CentOS 7 server using ip command

Fig.01: List NICs in a CentOS 7 server using ip command

Or use the following command:
# nmcli -p dev
Sample outputs:
Fig.02: nmcli command in action

Fig.02: nmcli command in action

Here is a typical DHCP configration for eth0 (stored in /etc/sysconfig/network-scripts/ifcfg-eth0 file):

DEVICE="eth0"
ONBOOT=yes
NETBOOT=yes
UUID="41171a6f-bce1-44de-8a6e-cf5e782f8bd6"
IPV6INIT=yes
BOOTPROTO=dhcp
HWADDR="00:08:a2:0a:ba:b8"
TYPE=Ethernet
NAME="eth0"

How do I configure an eth0 interface with static network IP settings (method # 1)?

To configure an eth0 interface with static network settings using ifcfg files, edit or create a file with name ifcfg-eth0 in the /etc/sysconfig/network-scripts/ directory as follows:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
Update/edit as follows for static IP configuration:

# static IP address on CentOS 7 or RHEL 7#
HWADDR=00:08:A2:0A:BA:B8
TYPE=Ethernet
BOOTPROTO=none
# Server IP #
IPADDR=192.168.2.203
# Subnet #
PREFIX=24
# Set default gateway IP #
GATEWAY=192.168.2.254
# Set dns servers #
DNS1=192.168.2.254
DNS2=8.8.8.8
DNS3=8.8.4.4
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
# Disable ipv6 #
IPV6INIT=no
NAME=eth0
# This is system specific and can be created using 'uuidgen eth0' command #
UUID=41171a6f-bce1-44de-8a6e-cf5e782f8bd6
DEVICE=eth0
ONBOOT=yes

Save and close the file. You do not need to specify the network or broadcast address as this is calculated automatically by the system. To restart networking service, enter:
# systemctl restart network

Verification

Verify new IP settings:
# ip a s eth0
Verify new routing settings:
# ip r
Verify DNS servers settings:
# cat /etc/resolv.conf
Verify the internet connectivity:
# ping -c 3 cyberciti.biz
# ping -c 4 google.com

Sample session:

Fig.03: Testing networking and make sure everything is working

Fig.03: Testing networking and make sure everything is working

How do I configure an eth0 interface with static IP address on CentOS 7 / RHEL 7 using Network Manager (method # 2)?

You need to use the nmtui command. It is a curses?based TUI application for interacting with NetworkManager. To show a connection editor that supports adding, modifying, viewing and deleting connections. To view or setup a static IP using this tool for eth0, enter:
# nmtui edit eth0
Sample outputs:

Fig.04: nmtui is a curses?based TUI application for interacting with NetworkManager.

Fig.04: nmtui is a curses?based TUI application for interacting with NetworkManager.

Conclusion

You learned how to set up static IP address on CentOS 7 or RHEL (Red Hat Enterprise Linux) 7.


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 6 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • Jeremy Canfield Jan 8, 2017 @ 1:04

    Hi Vivek. I have been using your nixCraft site for years. Your site is wonderful. Should PEERDNS=yes and IPV6_PEERDNS=yes be added to ensure /etc/resolv.conf is updated?

  • GSK Dec 4, 2020 @ 14:11

    Thank you and it is work.
    For me DNS not required.

  • Anh Huynh Dec 21, 2020 @ 8:33

    Hi, i did as your guide but when i input “ip a s ens33”, there is no IP address assigned to this network card? Is this guide still works on RHEL 7.6?

    • 🐧 Vivek Gite Dec 21, 2020 @ 10:06

      yes, it works with RHEL 7.6/CentOS 7.6. Please check your config again.

  • kevin ray Feb 19, 2021 @ 14:36

    Thank you so much…!

    It has been so helpful…!!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum