How to Have Multiple IP addresses on One NIC in Linux

Sometimes, you may require to host multiple websites on it. Hosting multiple sites on a single server is achieved through virtual hosting. For virtual hosting, multiple instances of the web server are run. The web service needs a specific port (80) to run and if there are multiple instances running, they will require unique IP-port pair.

However, the port cannot be changed, so we need multiple IP addresses. One IP address can be assigned to only one NIC (Network Interface Card). We cannot always install a NIC for each IP address required. Multiple IP Addresses can be bound to a single NIC by creating virtual interfaces. This technique is sometimes called as IP aliasing. We create aliases for the NIC to which additional addresses needs to be bound. In this tutorial, we have used Red Hat Enterprise Linux Server 5.6 for the purpose of bind ip address demonstration.

Network Configuration

The current network configuration can be checked with 'ifconfig' command:

# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:FA:F1:B7
inet addr:192.168.0.10 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:50 errors:0 dropped:0 overruns:0 frame:0
TX packets:76 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6061 (5.9 KiB) TX bytes:10824 (10.5 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:900 (900.0 b) TX bytes:900 (900.0 b)

Here, "eth0" is the first ethernet interface. The machines having more than one ethernet cards have subsequent interfaces, "eth1", "eth2", and so on. "lo" is the local loopback interface. We will create alias of eth0 interface. But first, let us check the configuration files for these interfaces.
The configuration files for these interfaces are located in the "/etc/sysconfig/network-scripts/" directory. "ls -l /etc/sysconfig/network-scripts/" reveals these files.

# ls -l /etc/sysconfig/network-scripts/
total 380
-rw-r--r-- 1 root root 191 Jul 3 19:01 ifcfg-eth0
-rw-r--r-- 1 root root 254 Oct 13 2010 ifcfg-lo
lrwxrwxrwx 1 root root 20 Jul 3 03:51 ifdown -> ../../../sbin/ifdown
-rwxr-xr-x 1 root root 625 Oct 13 2010 ifdown-bnep
-rwxr-xr-x 1 root root 4573 Oct 13 2010 ifdown-eth
-rwxr-xr-x 1 root root 827 Oct 13 2010 ifdown-ippp
-rwxr-xr-x 1 root root 2149 Oct 13 2010 ifdown-ipsec
-rwxr-xr-x 1 root root 4473 Oct 13 2010 ifdown-ipv6
......

The first file in this directory is 'ifcfg-eth0'. This is the configuration file for eth0 interface. Let us check the contents of this file:

# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Intel Corporation 82540EM Gigabit Ethernet Controller
DEVICE=eth0
IPADDR=192.168.0.10
NETMASK=255.255.255.0
BOOTPROTO=static
HWADDR=08:00:27:FA:F1:B7
ONBOOT=yes
DHCP_HOSTNAME=redhat-server

Let’s have a look at these tags:

• DEVICE - The device name goes here
• IPADDR is the value of IP address.
• NETMASK is the subnet mask.
• If BOOTPROTO has value 'static', the interface is statically configured, i.e. IP address/subnet mask are added manually. If the device takes address automatically, from DHCP server, its value should be 'dhcp'. But in that case, above two values, IPADDR and NETMASK will be missing.
• HWADDR is the hardware or MAC address.
• If ONBOOT has value "yes", the interface will be active, up and running on boot. But it will be down if the value is "no".

Rest of the values are not necessary. Any changes to these files will be persistent over boot. If you need your changes to be permanent, make changes in the files of this directory (network-scripts), instead of using ‘ifconfig’ command (to change IP address).

Adding Virtual Interfaces

The virtual interfaces of the interface eth0 will have the names eth0:0, eth0:1, eth0:2 and so on. To add an interface, create a corresponding file in the network-scripts directory. Now, let us create an interface eth0:0. To create this alias, we will create a file named "ifcfg-eth0:0":

I have created this file using redirection, but you can use any editor you like to create this file:

# cat > /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
IPADDR=192.168.0.20
NETMASK=255.255.255.0
BOOTPROTO=static
HWADDR=08:00:27:FA:F1:B7
ONBOOT=yes

Note that the device's name will be changed to the name of the virtual interface (eth0:0 here), and the hardware address will remain the same (because physical device is the same). This is all that is required for creating the alias. But these changes will not be loaded to the running system. Restart the networking and your new alias is ready to be used:

# service network restart
Shutting down interface eth0: [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: [ OK ] # ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:FA:F1:B7
inet addr:192.168.0.10 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:796 errors:0 dropped:0 overruns:0 frame:0
TX packets:480 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:74780 (73.0 KiB) TX bytes:78183 (76.3 KiB)

eth0:0 Link encap:Ethernet HWaddr 08:00:27:FA:F1:B7
inet addr:192.168.0.20 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:900 (900.0 b) TX bytes:900 (900.0 b)

With the ‘ifconfig’ command, the new (aliased) interfaces can be checked as shown in the output above. If you want to bind another address to eth0, create the alias eth0:1 in a similar fashion.

Temporarily adding multiple IP addresses on a NIC

You can use the 'ifconfig' command to add an IP address to a NIC. But, please note that this IP address will not be available after rebooting the machine. So, you need to use the second step for permanently adding the multiple IP addresses. Here, we assume that you already have a NIC with a static IP configured in it. If we need to add two more IP addresses, say 192.168.1.25 and 192.168.1.26 to this interface, we can accomplish this by executing the following commands:

ifconfig eth0:1 192.168.1.25 netmask 255.255.255.0
ifconfig eth0:2 192.168.1.26 netmask 255.255.255.0

Adding an IP address range

In order to add a range of IP addresses to eth0 interface, say from 192.168.1.20 to 192.168.1.30, you need to create (update) the file /etc/sysconfig/network-scripts/ifcfg-eth0-range0 with the following data:

IPADDR_START=192.168.1.20
IPADDR_END=192.168.1.30
CLONENUM_START=0
NETMASK=255.255.255.0

Then restart the network service,

service network restart

Now, you will be able to see the configured IP addresses using 'ifconfig' command.

Read Also:

Bobbin Zachariah 1:29 pm

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.