How-to Configure Network Interface in CentOS

Screenshot of Linux Network Restart by sumodirjo

Networking is basic needs for every computer that you use today. You need network connection for various needs, from accessing a file server on your LAN for browsing the internet. In this tutorial I will guide you to configure network interface on a CentOS machine from command line. This guide also applies to another distribution in “Red Hat Family” distribution like Red Hat Enterprise Linux, Fedora, or Scientific Linux. In this tutorial I assume that you use root user or already use su to change from your normal user to root user.

CentOS network interface configuration is located in /etc/sysconfig/network-scripts/. Every interface has its own configuration file. If you’re new to Linux you might not be familiar with device naming in Linux. For ethernet interface, Linux uses ethX as device name where X is the device number starting from zero. For example, if you have 5 devices, then your device names will be eth0 to eth4. In CentOS, network interface configuration file name for a device is ifcfg-. So, for eth0, the configuration name will be ifcfg-eth0.

Configure a DHCP client

To configure your interface to request network configuration from a DHCP server, you can use the configuration below. The following sample is for the first network interface on your machine (eth0), the configuration file will be ifcfg-eth0. Open this file with your favorite text editor and add the following lines to that file. If the file already has content, it’s better to remove all the lines or you can comment out the current content. You can add # to the beginning of a line to comment out a line and then add the following lines at the end of the file.


Configure a static IP
If you have no DHCP server on your network, you have to set a static IP on your interface.


To activate a device, you can use :

# ifup

For example, to activate eth0, you can use :

# ifup eth0

To deactivate a device, you can use :

# ifdown

To deactivate eth0, you use:

# ifdown eth0

To restart the whole networking, you can use :

# service network restart


# /etc/init.d/network restart

I hope this tutorial will be useful for you. Have fun and see you on the next tutorial.

Author: sumodirjo

Fulltime husband of @ririsretno, part time technologist (system / network engineer and IT consultant), writer and blogger. Passionate about all things computing and information technology. Using his spare time to maintain kurungsiku and howtodojo