Tuesday, April 13, 2010

Set multiple IPs in single nic.

Hello guys...
Ever wondered how to set multiple IPs in a single network card? Here is the solution.

Multiple IP binded in a single network card allows you run different services in different IPs, for example you can run HTTP on one IP and SMTP on another IP or a private LAN using a local IP and the alias holding your Public IP. The major benifit here is that you do not need an additional physical adaptor, you can bind many virtual IPs to a single network card. Here I have explained the procedure for creating multiple IPs for RedHat based and Debian bases systems

Redhat based systems.

Let me assume that your NIC is bound with a static IP address. Go to the folder /etc/sysconfig/network-scripts/, there you will find your network configuration files.

# cd /etc/sysconfig/network-scripts/

Let me also guess that your machine has only one network card (can be onboard too!!!), then you will find a file ifcfg-eth0 in the folder. This file holds the IP information for the first nic. If your machine has more nics you will have ifcfg-eth1, ifcfg-eth2 and so on... Now open the ifcfg-eth0 file, you can view the network configuration as below:

# cat ./ifcfg-eth0# File: ifcfg-eth0


DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.120
NETMASK=255.255.255.0
BROADCAST=192.168.1.255
NETWORK=192.168.1.0
HWADDR=xx:xx:xx:xx:xx:xx


Now to bind another IP address to the same network card, you just have to copy the ifcfg-eth0 file to ifcfg-eth0:1.
 
# cp ./ifcfg-etho ./ifcfg-eth0:1


Now open the file in your favourite editor and change the values of 'DEVICE' and IP ADDR' parameters.
 
# vi ./ifcfg-eth0:1
 
DEVICE=eth0:1
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.130
NETMASK=255.255.255.0
BROADCAST=192.168.1.255
NETWORK=192.168.1.0
GATEWAY=192.168.1.100
HWADDR=xx:xx:xx:xx:xx:xx


Now you can restart your network.
 
# service network restart
 
yes you are done. Now you have two IPs in a single network card. You can check it using:
 
# ifconfig
 
Or else try pinging to the new IP from any other machins in your network.. Voila,,,, isn't it????
You can add as many number of virtual IPs as you want by the same way, just go on as ifcfg-eth0:1, ifcfg_etho:2, and so on...
 
Debian based systems
 
In debian based systems the IP information is store in /etc/network/interfaces file. Open it.
 
# cat /etc/network/interfaces
 
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.1.140
netmask 255.255.255.0
gateway 192.168.1.100
broadcast 192.168.1.255
network 192.168.1.0
                 
Now add these lines to the file: (Remember to make changes to the IP as per your need!!!)

auto eth0:1
iface eth0:1 inet static
address 192.168.1.150
netmask 255.255.255.0
gateway 192.168.1.100
broadcast 192.168.1.255
network 192.168.1.0

You can add as many virtual IPs as needed.
After entering the required details you should restart the network using:

# /etc/init.d/networking restart

Voila you are done. You got multiple IPs in debian based systems too!!!!!

With warm regards,
Jayakrishnan

No comments:

Post a Comment