Tuesday, February 12, 2008

Bind a multiple IP address on a single NIC card How-to

This how-to demonstrates how to bind multiple IP addresses to a single NIC. You can run a service under a specific IP while having another service under a different one (for example, have DNS on one and SMTP on another), or create a private LAN using a local IP and have the alias hold your Internet IP (such as NAT) by using multiple IP's.

The network scripts are located in /etc/sysconfig/network-scripts/. Go into that directory.

#cd /etc/sysconfig/network-scripts/






The file we're interested in is ifcfg-eth0, the interface for the Ethernet device. If you have a second Ethernet device then there would be an ifcfg-eth1 file and so on for each adapter you have installed. We need to create an alias file while ifcfg-eth0 maintains the primary IP address.

This is how we will setup the aliases to bind the IP addresses.

Adapter IP Address Type
-----------------------------------
eth0 192.168.2.1 Primary
eth0:0 192.168.2.2 Alias 1
eth0:1 192.168.2.3 Alias 2

The :X (where X is the interface number) is appended to the interface file name to create the alias. For each alias you create you assign a number sequentially. For this example we will create aliases for eth0. Make a copy of ifcfg-eth0 for the three aliases.

cp ifcfg-eth0 ifcfg-eth0:0
cp ifcfg-eth0 ifcfg-eth0:1


Take a look inside ifcfg-eth0 and review the contents.

#more ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.2.1
NETMASK=255.255.255.0
GATEWAY=192.168.2.254
TYPE=Ethernet
USERCTL=no
PEERDNS=yes
IPV6INIT=no

We're interested in only two lines (DEVICE and IPADDR). We'll rename the device in each file to its corresponding interface alias and change the IP's. We'll start with ifcfg-eth0:0. Open ifcfg-eth0:0 in vi and change the two lines so they have the new interface and IP address.

#vi ifcfg-eth0:0

DEVICE=eth0:0
IPADDR=192.168.2.2

Save ifcfg-eth0:0 and edit the other alias files (ifcfg-eth0:1) so they have the new interfaces and IP addresses set (follow the table from above). Once you save all your changes you can restart the network for the changes to take effect.

#service network restart

To verify all the aliases are up and running you can run ifconfig (depending on how many new IP's you set up, you can use ifconfig more to pause the output).

#ifconfig

You can also test the IP's by pinging them from a different machine. If everything is working then there should be a response back.

c:\>ping 192.168.2.2
c:\>ping 192.168.2.3

0 comments: