Wednesday, August 26, 2009

Centos Ubuntu FreeBSD Multiple IP Addresses on One Interface

SkyHi @ Wednesday, August 26, 2009

FreeBSD IP Alias: Setup 2 or More IP address on One NIC


IP aliasing is the process of assigning more than one IP address to a network interface. This is useful for Apache web server virtual hosting or other network servers such as ftp server.

This tutorial explains how to assign one or more IP address to a single network interface under FreeBSD operating system.

It is possible to create network alias or assign 2 ip address to a single NIC under FreeBSD operating system.
Our Sample Setup

* Interface name: lnc0 - IP : 192.168.1.1/255.255.255.0
* Interface name: lnc0 alias - IP : 192.168.1.5/255.255.255.255

Netmask must be different otherwise you will get an error ifconfig: ioctl (SIOCAIFADDR): File exists.
Set ip alias using ifconfig command line utility

Login as the root user.

Now, use ifconfig command as follows to set alias:
# ifconfig lnc0 192.168.1.5 netmask 255.255.255.255 alias
Persistent IP Alias Configuration

Edit file /etc/rc.conf to setup persistent ip aliasing option by appending following configuration, so that next time FreeBSD comes up (after restart/shutdown) it will create alias for you:
ifconfig_lnc0_alias0="192.168.1.5 netmask 255.255.255.255"

Finally, Restart FreeBSD network service using network restart script:
# /etc/rc.d/netif restart && /etc/rc.d/routing restart
Display alias and real ip using ifconfig lnc0 command:
# ifconfig lnc0
Sample output:

lnc0: flags=8843 mtu 1500
inet6 fe80::20c:29ff:fe01:ddbd%lnc0 prefixlen 64 scopeid 0x1
inet 192.168.1.2 netmask 0xffffff00 broadcast 192.168.1.255
inet 192.168.1.5 netmask 0xffff

Sample /etc/rc.conf file with IP aliasing

Here is my /etc/rc.conf file with em0 and em1 interface with 5 aliases:

hostname="sun.nixcraft.in"
# default vsnl router interface
defaultrouter="202.54.1.200"
# Force 10Mbps for both public and private LAN interace
ifconfig_em0="inet 10.20.110.2 netmask 255.255.255.192 media 10baseT/UTP mediaopt full-duplex"
ifconfig_em1="inet 202.52.1.2 netmask 255.255.255.248 media 10baseT/UTP mediaopt full-duplex"
ifconfig_em0_alias0="inet 10.20.1.226 netmask 255.255.255.0"
ifconfig_em0_alias1="inet 10.20.1.227 netmask 255.255.255.0"
ifconfig_em0_alias2="inet 10.20.1.228 netmask 255.255.255.0"
ifconfig_em0_alias3="inet 10.20.1.229 netmask 255.255.255.0"
ifconfig_em0_alias4="inet 10.20.1.230 netmask 255.255.255.0"
# static routing configuration
static_routes="lan0"
route_lan0="-net 10.0.0.0/8 10.20.110.1"






Linux Creating or Adding New Network Alias To a Network Card (NIC)

Q. I would like to create alias for my network card (NIC). How do I setup 2 IP address on One NIC? How do I add alias under Centos / Fedora / Debian / Ubuntu Linux?

A. Linux allows you to add additional network address using alias feature. Please note that all additional network IP address must be in same subnet. For example if your eth0 using 192.168.1.5 IP address then alias must be setup using 192.168.1.0/24 subnet.
ifconfig command line

You can use ifconfig command to configure a network interface and alias. For example:

* eth0 NIC IP 192.168.1.5
* eth0:0 first NIC alias: 192.168.1.6

To setup eth0:0 alias type the following command as the root user:
# ifconfig eth0:0 192.168.1.6 up
Verify alias is up and running using following command:
# ifconfig -a
# ping 192.168.1.6
However, if you reboot the system you will lost all your alias. To make it permanent you need to add it network configuration file.
Debian / Ubuntu Linux Instructions

You can configure the additional IP addresses automatically at boot with another iface statement in /etc/network/interfaces:
# vi /etc/network/interfaces
Append text as follows:

auto eth0:1
iface eth0:1 inet static
name Ethernet alias LAN card
address 192.168.1.7
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0

Save and close the file. Restart the network:
# /etc/init.d/networking restart

Red Hat / RHEL / CentOS / Fedora Linux Instructions

Copy etc/sysconfig/network-scripts/ifcfg-eth0 file as /etc/sysconfig/network-scripts/ifcfg-eth0:0
# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0
Open file /etc/sysconfig/network-scripts/ifcfg-eth0:0 using vi text editor:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0:0
Find entry that read as follows:

DEVICE=eth0

Replace with:

DEVICE=eth0:0

Find entry that read as follows:

IPADDR=xxx.xxx.xxx.xxx

Replace it with your actual IP address:

IPADDR=192.168.1.7

At the end your file should like as follows:

DEVICE=eth0:0
IPADDR=192.168.1.7
NETMASK=255.255.255.0
NETWORK=192.168.1.0
ONBOOT=yes
NAME=eth0:0


DEVICE=eth0:190
BOOTPROTO=static
IPADDR=192.168.1.7
BROADCAST=192.168.1.255
NETMASK=255.255.255.0
NETWORK=192.168.1.0
#HWADDR=00:0D:33:38:C3:28
ONBOOT=yes



Open file /etc/sysconfig/network-scripts/ifcfg-eth0 and make sure file does not have a GATEWAY= entry:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
Find the entry that read as follows:

GATEWAY=your-ip

Remove or comment it out by prefixing # (hash) :

# GATEWAY=192.168.1.254

Save the file. Add the GATEWAY= to your /etc/sysconfig/network:

# vi /etc/sysconfig/network

Append or modify GATEWAY entry:

GATEWAY=192.168.1.254

Save the file. Reboot the system or run the following command:

# ifup eth0:0

OR

# service network restart

Red Hat / CentOS / Fedora Multiple IP address range

You can assign multiple ip address range as follows to eth0:
vi /etc/sysconfig/network-scripts/ifcfg-eth0-range0
Append following code from 202.54.112.120 to 202.54.112.140:

IPADDR_START=202.54.112.120
IPADDR_END=202.54.112.140
CLONENUM_START=0
NETMASK=255.255.255.0

Save and close the file.


Reference: http://www.cyberciti.biz/faq/multiple-ip-addresses-on-one-interface/