You get a bonus - 1 coin for daily activity. Now you have 1 coin

CentOS/RedHat: configuring a network card (how to set an IP address, DNS servers, and gateway)

Practice



Network interfaces

CentOS/RedHat (hereafter RHEL) names network cards using the name eth followed by a number. Virtual interfaces (loopback, VPN, etc.) are named separately by RHEL, also with a name (for example, tun, tap) and a number after it.

For example, if you have 2 network cards in your computer, they will be named eth0 and eth1 by default.

Note that this operating system permanently binds the MAC address of the physical network interface (network card) to the network card's settings. So, if you had a network card in your computer with MAC address 11:22:33:44:55:66 and static address 192.168.0.1, and you replaced it with another one - with MAC 22:33:44:55:66:77 - then on first boot you'll find that all your settings are gone. The new card will still be named the same though - for example, eth0.

If the settings get lost (and this will definitely happen if you replace the network card) - we use this method (don't worry - all the settings are preserved):


IP addresses

Persistent settings are stored in the files /etc/sysconfig/network-scripts/ifcfg-???

For example, the settings for eth0 are stored in the file:
/etc/sysconfig/network-scripts/ifcfg-eth0

and the settings for ppp0:
/etc/sysconfig/network-scripts/ifcfg-ppp0


Here's an example of the ifcfg-eth0 file:
# Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C+
DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.0.255
HWADDR=FE:16:3E:68:FC:FB
IPADDR=192.168.0.1
NETMASK=255.255.255.0
NETWORK=192.168.0.0
ONBOOT=yes

This file defines the IPv4 address 192.168.0.1 on network card eth0 (via the IPADDR directive).


If you need the network card to obtain its address via DHCP (automatically) - the file should look like this:
# Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C+
DEVICE=eth0
BOOTPROTO=dhcp
HWADDR=FE:16:3E:68:FC:FB
ONBOOT=yes


In the case of several network cards (2 or more) - they are named eth1, eth2, and so on. Accordingly, we create the files ifcfg-eth1, ifcfg-eth2, etc. and write the necessary parameters into them.


Setting the gateway

To do this, you need to edit the file /etc/sysconfig/network and set the required gateway IP address in the "GATEWAY" line:

/etc/sysconfig/network
...
GATEWAY=192.168.0.250
...

In this example we specified a gateway with IP 192.168.0.250.


Setting DNS servers

As in any Unix system, DNS servers are specified in the file /etc/resolv.conf

Here's an example of such a file:
nameserver 192.168.1.1
nameserver 192.168.2.1
nameserver 11.22.33.44

Unix in general, and RHEL in particular, allows specifying no more than 3 DNS servers.


Applying

After making all the changes, you can apply the new values without rebooting the whole server - run the network script with the restart command:

$ sudo /etc/init.d/network restart

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Computer networks"

Terms: Computer networks