Add additional IPs to one NIC in Linux February 10, 2011Posted by Tournas Dimitrios in Linux.
This article will outline how to bind multiple IP addresses to a single NIC . By using multiple IP’s you can run a service under a specific IP while having another service under a different one ( have a web server on one and FTP on another ) . One of the major benefits is that you don’t need a physical adapter for each IP but instead can create many virtual ones tied to a single physical card . This article is targeted to RedHat based distributions (Fedora , CetnOs ) .
Linux allows you to add additional network addresses using alias feature . But all additional network IP address must be in the same subnet . For example if your eth0 is using 192.168.1.5 IP address then the alias must be setup using 192.168.1.0/24
The quick way :
Two commands to remember :
$ /sbin/ifconfig eth0:0 192.168.0.5
$ /sbin/route add -host 192.168.0.5 dev eth0:0
This change will no longer be in effect after rebooting. Therefor you must add the two commands to the bottom of your /etc/rc.local file. Your NIC should now be listening and responding on both this and the original IP address found in /etc/sysconfig/network-scripts/ifcfg-eth0
If you want to add add more than one additional IP address, continue like this:
$ /sbin/ifconfig eth0:0 192.168.0.4
$ /sbin/ifconfig eth0:1 192.168.0.5
$ /sbin/ifconfig eth0:2 192.168.0.6
$ /sbin/route add -host 192.168.0.4 dev eth0:0
$ /sbin/route add -host 192.168.0.5 dev eth0:1
$ /sbin/route add -host 192.168.0.6 dev eth0:2
An alternative way :
Duplicating the network script (ifcfg-eth0) in /etc/sysconfig/network-scripts/ directory .
Each adapter that is installed on a computer has it’s unique network script , so the second adaptor will have ifcfg-eth1 and so on for each adaptor . Let’s assume our computer has only one network card , and we want to bind three additional IP’s ( 192.168.1.150 , 192.168.1.160 and 192.168.1.170 ) to this interface . We need to create three alias files while ifcfg-eth0 maintains the primary IP address . This is how we set up the aliases to bind the addresses :
The :X (where X is the interface number ) is appended to the interface file name to create the alias . For each alias we create , an incresing number will be assigned . For this example we will create aliases for eth0 . Three aliases needs 3 copies of the network script :
- cd /etc/sysconfig/network-scripts
- cp ifcfg-eth0 ifcfg-eth0:0
- cp ifcfg-eth0 ifcfg-eth0:1
- cp ifcfg-eth0 ifcfg-eth0:2
Let’s see the example in practice :
Editing each alias network script , we are only interested for two lines (DEVICE and IPADDR) . We’ll rename the device and IP in each file to it’s new values .
After restarting the network interface with : service network restart , we can confirming the changes with ifconfig
Now let’s ping all our IP’s from a remote computer . Of course for convenience we are using fping