How to configure networking in CentOS, Fedora and
other Redhat based distros
On Debian Linux I'm used to configure the networking via
/etc/network/interfaces , however on Redhat based
distributions to do a manual configuration of network interfaces is
a bit different.
In order to configure networking in CentOS there is a special file
for each interface and some values one needs to fill in to enable
networking.
These network adapters configuration files for Redhat based
distributions are located in the files:
/etc/sysconfig/network-scripts/ifcfg-* Just to give you and idea
on the content of this network configuration file, here is how it
looks like:
[root@centos:~ ]# cat
/etc/sysconfig/network-scripts/ifcfg-eth0
# Broadcom Corporation NetLink BCM57780 Gigabit Ethernet PCIe
DEVICE=eth0 BOOTPROTO=static DHCPCLASS= HWADDR=00:19:99:9C:08:3A
IPADDR=192.168.0.1 NETMASK=255.255.252.0 ONBOOT=yes
This configuration is of course just for eth0 for other network
card names and devices, one needs to look up for the proper file
name wh ich corresponds to the network interface visible with the
ifconfig command.
For instance to list all network interfaces via ifconfig use:
[root@centos:~ ]# /sbin/ifconfig |grep -i 'Link encap'|awk '{
print $1 }'
eth0
eth1
lo
In this case there are only two network cards on my host.
The configuration files for the ethernet network devices
eth0 and
eth1 from below example are located in files
/etc/sysconfig/network-scripts/ifcfg-eth{1,2}
/etc/sysconfig/network-scripts/ directory contains plenty of
shell scripts related to Fedora networking.
This directory contains actually the networking boot time load up
rules for fedora and CentOS hosts.
The complete list of options available which can be used in
/etc/sysconfig/network-scripts/ifcfg-ethx is located
in:
/usr/share/doc/initscripts-*/sysconfig.txt
, to quickly observe the documentation:
[root@centos:~ ]# less
/usr/share/doc/initscripts-*/sysconfig.txt
One typical example of configuring a
CentOS based host to
possess a static IP address (192.168.1.5) and a gateway
(192.168.1.1), which will be assigned in boot time during the
/etc/init.d/network is loaded is:
[root@centos:~ ]# cat
/etc/sysconfig/network-scripts/ifcfg-eth0
# Broadcom Corporation NetLink BCM57780 Gigabit Ethernet PCIe
IPV6INIT=no
BOOTPROTO=static
ONBOOT=yes
USERCTL=yes
TYPE=Ethernet
DEVICE=eth0
IPADDR=192.168.1.5
NETWORK=192.168.1.0
GATEWAY=192.168.1.1
BROADCAST=192.168.1.255
NETMASK=255.255.255.0
After some changes to the network configuration files are made, to
load up the new rules a
/etc/init.d/network script restart
is necessery with the command:
[root@centos:~ ]# /etc/init.d/network
restart
Of course one can always use
/etc/rc.local script as
universal way to configure network rules on a Redhat based host,
however using methods like
rc.local to load up,
ifconfig or
route rules in a Fedora would break the
distribution logic and therefore is not recommended.
There is also a serious additional reason against using
/etc/rc.local post init commands load up script.
If one uses
rc.local to load up and configure the networing,
the network will get initialized only after all the other scripts
in
/etc/init.d/ gets started.
Therefore using
/etc/rc.local might also be DANGEROUS!, if
used remotely via (ssh), supposedly it might completely fail to
load the networking, if all bringing the server interfaces relies
on it.
Here is an example, imagine that some of the script set in to load
up during a CentOS boot up hangs and does continue to load forever
(for example after some crucial software upate), as a consequence
the
/etc/rc.local script will never get executed as it only
starts up after all the rest init scripts had succesfully completed
execution.
A network
eth1 interface configuration for a Fedora host
which has to fetch it's network settings automatically via DHCP is
as follows:
[root@fedora:/etc/network:]# cat
/etc/sysconfig/network-scripts/ifcfg-eth1
# Intel Corporation 82557/8/9 [Ethernet Pro 100] DEVICE=eth1
BOOTPROTO=dhcp
HWADDR=00:0A:E4:C9:7B:51
ONBOOT=yes
To sum it up I think Fedora's
/etc/sysconfig/network-scripts
methodology to configure ethernet devices is a way inferior if
compared to Debian.
In GNU/Debian Linux configuration of all networking is (simpler)!,
everything related to networking is in one single file (
/etc/network/interfaces ), moreover getting all the thorough
documentation for the network configurations options for the
interfaces is available as a system wide manual (e.g. man
interfaces).
Partially Debian
interfaces configuration is a bit more
complicated in terms of syntax if matched against Redhat's
network-scripts/ifcfg-*, lest that generally I still find
Debian's manual network configuration interface to be easier to
configure networking manually vicommand line.