Wednesday 24 September 2014

Virtual IP Configuration

Configuring multiple IP addresses on the single interface is known as virtual IP configuration. Some time also known as the IP alias. IP aliasing is very useful in case of creating different websites on same server using virtual hosting (IP based virtual hosting) using a single network interface card.
The main advantage of using Virtual IP is that we can create as many virtual IPs as required without the need of additional interface cards.

Here we will discuss how to setup virtual IP configuration on Linux:

If we want to temporarily setup the virtual IP we can use the below command:

# ifconfig eth0:0 10.14.153.23 netmask 255.255.255.0

Now you can check the interface using ifconfig command.

If we want to permanently setup the virtual IP, We need to setup the configuration in a file.

CentOS, RedHat, Fedora.

Suppose we have eth0 physical interface available and we want to create 3 new virtual interface based on this interface. We will follow the below steps:

1. Move to the network files directory:

# cd /etc/sysconfig/network-scripts/

# ls -l

2. Open the contents of the ifcfg-eth0 file using cat command.

DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
IPADDR=10.14.153.31
NETMASK=255.255.255.0
GATEWAY=10.14.153.254

BOOTPROTO=static

3. We can see the configuration file of eth0 interface as ifcfg-eth0. Copy this file to below names in the same directory.

# cp ifcfg-eth0 ifcfg-eth0:0 

# cp ifcfg-eth0 ifcfg-eth0:1

# cp ifcfg-eth0 ifcfg-eth0:2

4. Now edit the individual file and set the IP address and Device name as shown below.

# vim ifcfg-eth0:0

DEVICE=eth0:0
TYPE=Ethernet
ONBOOT=yes
IPADDR=10.14.153.23
NETMASK=255.255.255.0
GATEWAY=10.14.153.254

BOOTPROTO=static

# vim ifcfg-eth0:1

DEVICE=eth0:1
TYPE=Ethernet
ONBOOT=yes
IPADDR=10.14.153.24
NETMASK=255.255.255.0
GATEWAY=10.14.153.254

BOOTPROTO=static

# vim ifcfg-eth0:2

DEVICE=eth0:2
TYPE=Ethernet
ONBOOT=yes
IPADDR=10.14.153.25
NETMASK=255.255.255.0
GATEWAY=10.14.153.254

BOOTPROTO=static

5. After saving the files, restart the network service.

# /etc/init.d/network restart

6.  We can also specify the range of virtual IP addresses. create a file and specify the range.

# vim /etc/sysconfig/network-scripts/ifcfg-eth0-range0

TYPE=Ethernet
IPADDR_START=10.14.153.23
IPADDR_END=10.14.153.26


7. Save the file and restart the service.

# /etc/init.d/network restart

4 comments:

  1. Wow!!!......Helps great ..But I wana know that is there any other type of IP aliasing is there.If exists then please tell us..

    ReplyDelete
  2. No Sandeep, There is no other aliasing available. Either I will share aliasing in debian systems.

    ReplyDelete
  3. Thanks for helps sahil.............I have a question that if we can enable virtual IP, then why we need multiple NIC interfaces ???????

    ReplyDelete
  4. Need of multiple interfaces and Virtual IP are totally different things. NIC is the thing that happens at physical layer, But IP configuration is on the network layer. IP is the sort of another name that we gave to our NIC. If we see logically, we are dividing a NIC into parts by giving the virtual IP. On the other hand, using different NIC helps in better redundancy and provides fail-over support.

    ReplyDelete