Examples : Binding Multiple IP to One Interface In Linux

May 14, 2011 | By
| Reply More

Linux can be deployed as a very powerful web server. Sometimes, you may require to host multiple web sites 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 article, we have used Red Hat Enterprise Linux Server 5.6 for the purpose of demonstration.

Network Configuration

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

[root@redhat-server ~]# 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.

[root@redhat-server ~]# 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
lrwxrwxrwx 1 root root 11 Jul 3 03:51 ifdown-isdn -> ifdown-ippp
-rwxr-xr-x 1 root root 1481 Oct 13 2010 ifdown-post
-rwxr-xr-x 1 root root 1084 Oct 13 2010 ifdown-ppp
-rwxr-xr-x 1 root root 637 Oct 13 2010 ifdown-routes
-rwxr-xr-x 1 root root 1593 Oct 13 2010 ifdown-sit
-rwxr-xr-x 1 root root 913 Oct 13 2010 ifdown-sl
-rwxr-xr-x 1 root root 1074 Oct 13 2010 ifdown-tunnel
lrwxrwxrwx 1 root root 18 Jul 3 03:51 ifup -> ../../../sbin/ifup
-rwxr-xr-x 1 root root 13427 Oct 13 2010 ifup-aliases
-rwxr-xr-x 1 root root 859 Oct 13 2010 ifup-bnep
-rwxr-xr-x 1 root root 10798 Oct 13 2010 ifup-eth
-rwxr-xr-x 1 root root 12033 Oct 13 2010 ifup-ippp
-rwxr-xr-x 1 root root 8078 Oct 13 2010 ifup-ipsec
-rwxr-xr-x 1 root root 10853 Oct 13 2010 ifup-ipv6
-rwxr-xr-x 1 root root 823 Oct 13 2010 ifup-ipx
lrwxrwxrwx 1 root root 9 Jul 3 03:51 ifup-isdn -> ifup-ippp
-rwxr-xr-x 1 root root 727 Oct 13 2010 ifup-plip
-rwxr-xr-x 1 root root 954 Oct 13 2010 ifup-plusb
-rwxr-xr-x 1 root root 2369 Oct 13 2010 ifup-post
-rwxr-xr-x 1 root root 4298 Oct 13 2010 ifup-ppp
-rwxr-xr-x 1 root root 1707 Oct 13 2010 ifup-routes
-rwxr-xr-x 1 root root 3662 Oct 13 2010 ifup-sit
-rwxr-xr-x 1 root root 1675 Oct 13 2010 ifup-sl
-rwxr-xr-x 1 root root 2078 Oct 13 2010 ifup-tunnel
-rwxr-xr-x 1 root root 3325 Oct 13 2010 ifup-wireless
-rwxr-xr-x 1 root root 5433 Oct 13 2010 init.ipv6-global
-rwxr-xr-x 1 root root 1154 Oct 13 2010 net.hotplug
-rw-r--r-- 1 root root 10363 Oct 13 2010 network-functions
-rw-r--r-- 1 root root 39833 Oct 13 2010 network-functions-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:

[root@redhat-server ~]# 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:

• The device name goes under DEVICE.
• 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:

[root@redhat-server ~]# 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:

[root@redhat-server ~]# service network restart
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: [ OK ]
[root@redhat-server ~]# 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.

Filed Under : LINUX HOWTO, NETWORK

Tagged With :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.