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.
DEVICE=eth0 BOOTPROTO=dhcp ONBOOT=yes
Configure a static IP
If you have no DHCP server on your network, you have to set a static IP on your interface.
DEVICE=eth0 BOOTPROTO=static IPADDR=192.168.19.200 NETMASK=255.255.255.0 NETWORK=192.168.19.0 ONBOOT=yes
To activate a device, you can use :
For example, to activate eth0, you can use :
# ifup eth0
To deactivate a device, you can use :
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.