Fri Apr 13 18:02:22 EEST 2012

How to Add Virtual IP adddress on GNU / Linux - Set up multiple IP addresses on Linux boot with ifconfig

Periodically, I have to set-up new Linux servers with second, third in other words multiple IP addresses.
Anyone who needs to set up multiple IP addresses to be responding on one machine knows the common reason is a need is multiple domain names with SSL enabled hosted on the same physical server. Configuring different domain names to be resolved to different IPs is rare but for some case scenarios is a must. Some other use case is if running other popular Network services like, Apache VirtualHost,Qmail + (vpopmail POP3, FTP servers, proxies etc.


The universal way which allows to set multiple virtual IP addresses on GNU / Linux gives the admin an option as both ifconfig or ip commands can be used.

I personally always use ifconfig so I always use ifconfig to set new virtual IP addreses and interfaces.

1. Adding virtual ethernet interface and Virtual IP on Slackware and across all Linuxes with ifconfig

Adding a virtual ethernet interfaces with ifconfig command is done using the syntax:

/sbin/ifconifg eth0:0 IP_ADDR0 netmask NETMASK_IP_ADDR up
/sbin/ifconfig eth0:1 IP_ADDR1 netmask NETMASK_IP_ADDR1 up
/sbin/ifconfig eth0:N IP_ADDR1 netmask NETMASK_IPADDRN up
...

Hence to add the 4 IP addresses:

  • 1.2.3.4
  • 1.2.3.5
  • 1.2.3.7
  • 1.2.5.8
use something like:



server:~# /sbin/ifconfig eth0:0 1.2.3.4 netmask 255.255.255.0 up
server:~# /sbin/ifconfig eth0:1 1.2.3.5 netmask 255.255.255.0 up
server:~# /sbin/ifconfig eth0:2 1.2.3.7 netmask 255.255.255.0 up
server:~# /sbin/ifconfig eth0:3 1.2.5.8 netmask 255.255.255.0 up
...
server:~# /sbin/ifconfig ethX:N x.x.x.x netmask 255.255.255.0 up
2. Setting Virtual IP addresses on interfaces on system boot time

To set a number of IP addresses to automatically be bringed up on server boot, simply place in /etc/rc.local before the exit 0 - last file line;

export IP_ADDRESS1='1.2.3.4';
export IP_ADDRESS2='1.2.3.5';
export IP_ADDRESS3='1.2.3.8';
export IP_ADDRESS4='1.2.3.9';
/sbin/ifconfig eth0:0 $IP_ADDRESS1 netmask 255.255.255.0 up
/sbin/ifconfig eth0:1 $IP_ADDRESS2 netmask 255.255.255.0 up
/sbin/ifconfig eth0:2 $IP_ADDRESS3 netmask 255.255.255.0 up
/sbin/ifconfig eth0:3 $IP_ADDRESS3 netmask 255.255.255.0 up


3. Adding new virtual IP addresses to server to /etc/rc.local from command line

To do add a set of virtual IP addresses to /etc/rc.local without using an interactive text editor (useful for scripting purposes) use:

server:~# head -n $(($(echo $(wc -l /etc/rc.local |awk '{ print $1 }')) - 1)) /etc/rc.local >> /etc/rc.local.tmp
server:~# echo "export IP_ADDRESS1='1.2.3.4'" >> /etc/rc.local.tmp;
server:~# echo "export IP_ADDRESS2='1.2.3.5'" >> /etc/rc.local.tmp
server:~# echo "export IP_ADDRESS3='1.2.3.8'" >> /etc/rc.local.tmp
server:~# echo "export IP_ADDRESS4='1.2.3.9'" >> /etc/rc.local.tmp
server:~# echo "/sbin/ifconfig eth0:0 $IP_ADDRESS1 netmask 255.255.255.0 up" >>/etc/rc.local.tmp
server:~# echo "/sbin/ifconfig eth0:1 $IP_ADDRESS2 netmask 255.255.255.0 up" >> /etc/rc.local.tmp
server:~# echo "/sbin/ifconfig eth0:2 $IP_ADDRESS3 netmask 255.255.255.0 up" >> /etc/rc.local.tmp
server:~# echo "/sbin/ifconfig eth0:3 $IP_ADDRESS3 netmask 255.255.255.0 up" >> /etc/rc.local.tmp
server:~# echo "exit 0" >> /etc/rc.local.tmp
server:~# cp -rpf /etc/local /etc/local.bak; cp -rpf /etc/rc.local.tmp /etc/rc.local


4. Setting up Virtual IP addresses on Debian and Ubuntu G*/Linux

In Debian and other (deb) based Linux distributions (Ubuntu, Mint etc.), there is also another approach to "solve" the task through using the default networking config file:

/etc/network/interfaces

Hence if managing any deb one based Linux, its a good practice in terms of clarity to use the "debian way" to add virutal ip addresses.
On these distros to add new virtual ip address edit /etc/network/interfaces with your favourite text editor, for instance:

# vim /etc/network/interfaces


Place inside config like:

auto eth0:1
iface eth0:1 inet static
address 192.168.1.60
netmask 255.255.255.0
network x.x.x.x
broadcast x.x.x.x
gateway x.x.x.x


N.B. Be cautious or you will end up with your server being unreachable, make sure you put some crontab to periodically check and up the main eth0 assigned IP address to prevent server inaccessibility if you don't have a physical server access.
For more Virtual IPs just add more entries like the above ones ...

5. Setting up Virtual IP addresses on Redhat based distros (RHEL, CentOS, Fedora)

In Fedora, CentOS, RHEL each new virtual IP or virtual IP range has to go in a separate file like:

  • /etc/sysconfig/network-scripts/ifcfg-eth0:0
  • /etc/sysconfig/network-scripts/ifcfg-eth0:1
  • /etc/sysconfig/network-scripts/ifcfg-eth0:2
  • ...


Each of the ifcfg-eth0:N files could be a straight copy of /etc/sysconfig/network-scripts/ifcfg-eth0

[root@centos:~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0
[root@centos:~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0


The default content of ifcfg-eth0:0 should be like:

NETMASK=255.255.255.0
DEVICE=eth0
ONBOOT=yes
TYPE=Ethernet
ETHTOOL_OPTS="duplex full speed 100 autoneg off wol g"

IPADDR=10.0.0.01


You will have to change the fields you see in red to be like:

DEVICE=eth0:0
IPADDR=10.0.0.02


Further on for all virtual IPs create files /etc/sysconfig/network-scripts/ifcfg-ethN and change DEVICE and IPADDR value.

Finally to load up the new Virtual IP interfaces to come online restart networking:

/sbin/service network restart
...


Check and re-check that the IP configs placed are correct, otherwise if you do it on a remote server you will loose connection to it as it could happen the primary server IP on eth0 fails to properly set.