Did You Know?
Donate to TecMint

We are pleased to announce our new TecMint Q/A section to submit your Linux questions

Create Multiple IP Addresses to One Single Network Interface

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators

The concept of creating or configuring multiple IP addresses on a single network interface is called IP aliasing. IP aliasing is very useful for setting up multiple virtual sites on Apache using one single network interface with different IP addresses on a single subnet network.

The main advantage of using this IP aliasing is, you don’t need to have a physical adapter attached to each IP, but instead you can create multiple or many virtual interfaces (aliases) to a single physical card.

Linux IP Aliasing

Create Multiple IP Addresses in One NiC

The instructions given here are applies to all major Linux distributions like Red Hat, Fedora, and CentOS. Creating multiple interfaces and assign IP address to it manually is a daunting task. Here we’ll see how we can assign IP address to it defining a set of IP range. Also understand how we are going to create a virtual interface and assign different range of IP Address to an interface in one go. In this article we used LAN IPs, so replace those with ones you will be using.

Creating Virtual Interface and Assign Multiple IP Addresses

Here I have an interface called “ifcfg-eth0“, the default interface for the Ethernet device. If you’ve attached second Ethernet device, then there would be an “ifcfg-eth1” device and so on for each device you’ve attached. These device network files are located in “/etc/sysconfig/network-scripts/” directory. Navigate to the directory and do “ls -l” to list all devices.

# cd /etc/sysconfig/network-scripts/
# ls -l
Sample Output
ifcfg-eth0   ifdown-isdn    ifup-aliases  ifup-plusb     init.ipv6-global
ifcfg-lo     ifdown-post    ifup-bnep     ifup-post      net.hotplug
ifdown       ifdown-ppp     ifup-eth      ifup-ppp       network-functions
ifdown-bnep  ifdown-routes  ifup-ippp     ifup-routes    network-functions-ipv6
ifdown-eth   ifdown-sit     ifup-ipv6     ifup-sit
ifdown-ippp  ifdown-tunnel  ifup-isdn     ifup-tunnel
ifdown-ipv6  ifup           ifup-plip     ifup-wireless

Let’s assume that we want to create three additional virtual interfaces to bind three IP addresses (172.16.16.126, 172.16.16.127, and 172.16.16.128) to the NIC. So, we need to create three additional alias files, while “ifcfg-eth0” keeps the same primary IP address. This is how we moving forward to setup three aliases to bind the following IP addresses.

Adapter            IP Address                Type
-------------------------------------------------
eth0              172.16.16.25            Primary
eth0:0            172.16.16.26            Alias 1
eth0:1            172.16.16.27            Alias 2
eth0:2            172.16.16.28            Alias 3

Where “:X” is the device (interface) number to create the aliases for interface eth0. For each alias you must assign a number sequentially. For example, we copying existing parameters of interface “ifcfg-eth0” in virtual interfaces called ifcfg-eth0:0, ifcfg-eth0:1 and ifcfg-eth0:2. Go into the network directory and create the files as shown below.

# cd /etc/sysconfig/network-scripts/
# cp ifcfg-eth0 ifcfg-eth0:0
# cp ifcfg-eth0 ifcfg-eth0:1
# cp ifcfg-eth0 ifcfg-eth0:2

Open a file “ifcfg-eth0” and view the contents.

[root@tecmint network-scripts]# vi ifcfg-eth0

DEVICE="eth0"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=172.16.16.125
NETMASK=255.255.255.224
GATEWAY=172.16.16.100
HWADDR=00:0C:29:28:FD:4C

Here we only need two parameters (DEVICE and IPADDR). So, open each file with VI editor and rename the DEVICE name to its corresponding alias and change the IPADDR address. For example, open files “ifcfg-eth0:0“, “ifcfg-eth0:1” and “ifcfg-eth0:2” using VI editor and change both the parameters. Finally it will look similar to below.

ifcfg-eth0:0
DEVICE="eth0:0"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=172.16.16.126
NETMASK=255.255.255.224
GATEWAY=172.16.16.100
HWADDR=00:0C:29:28:FD:4C
ifcfg-eth0:1
DEVICE="eth0:1"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=172.16.16.127
NETMASK=255.255.255.224
GATEWAY=172.16.16.100
HWADDR=00:0C:29:28:FD:4C
ifcfg-eth0:2
DEVICE="eth0:2"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=172.16.16.128
NETMASK=255.255.255.224
GATEWAY=172.16.16.100
HWADDR=00:0C:29:28:FD:4C

Once, you’ve made all changes, save all your changes and restart/start the network service for the changes to reflect.

[root@tecmint network-scripts]# /etc/init.d/network restart

To verify all the aliases (virtual interface) are up and running, you can use “ifconfig” or “ip” command.

[root@tecmint network-scripts]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:172.16.16.125  Bcast:172.16.16.100  Mask:255.255.255.224
          inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:237 errors:0 dropped:0 overruns:0 frame:0
          TX packets:198 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:25429 (24.8 KiB)  TX bytes:26910 (26.2 KiB)
          Interrupt:18 Base address:0x2000

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:172.16.16.126  Bcast:172.16.16.100  Mask:255.255.255.224
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Base address:0x2000

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:172.16.16.127  Bcast:172.16.16.100  Mask:255.255.255.224
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Base address:0x2000

eth0:2    Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:172.16.16.128  Bcast:172.16.16.100  Mask:255.255.255.224
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Base address:0x2000

Ping each of them from different machine. If everything setup correctly, you will get a ping response from each of them.

ping 172.16.16.126
ping 172.16.16.127
ping 172.16.16.128
Sample Output
[root@tecmint ~]# ping 172.16.16.126
PING 172.16.16.126 (172.16.16.126) 56(84) bytes of data.
64 bytes from 172.16.16.126: icmp_seq=1 ttl=64 time=1.33 ms
64 bytes from 172.16.16.126: icmp_seq=2 ttl=64 time=0.165 ms
64 bytes from 172.16.16.126: icmp_seq=3 ttl=64 time=0.159 ms

--- 172.16.16.126 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.159/0.552/1.332/0.551 ms

[root@tecmint ~]# ping 172.16.16.127
PING 172.16.16.127 (172.16.16.127) 56(84) bytes of data.
64 bytes from 172.16.16.127: icmp_seq=1 ttl=64 time=1.33 ms
64 bytes from 172.16.16.127: icmp_seq=2 ttl=64 time=0.165 ms
64 bytes from 172.16.16.127: icmp_seq=3 ttl=64 time=0.159 ms

--- 172.16.16.127 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.159/0.552/1.332/0.551 ms

[root@tecmint ~]# ping 172.16.16.128
PING 172.16.16.128 (172.16.16.128) 56(84) bytes of data.
64 bytes from 172.16.16.128: icmp_seq=1 ttl=64 time=1.33 ms
64 bytes from 172.16.16.128: icmp_seq=2 ttl=64 time=0.165 ms
64 bytes from 172.16.16.128: icmp_seq=3 ttl=64 time=0.159 ms

--- 172.16.16.128 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.159/0.552/1.332/0.551 ms

Seems everything working smoothly, With these new IPs’ you can setup virtual sites in Apache, FTP accounts and many other things.

Assign Multiple IP Address Range

If you would like to create a range of Multiple IP Addresses to a particular interface called “ifcfg-eth0“, we use “ifcfg-eth0-range0” and copy the contains of ifcfg-eth0 on it as shown below.

[root@tecmint network-scripts]# cd /etc/sysconfig/network-scripts/
[root@tecmint network-scripts]# cp -p ifcfg-eth0 ifcfg-eth0-range0

Now open “ifcfg-eth0-range0” file and add “IPADDR_START” and “IPADDR_END” IP address range as shown below.

[root@tecmint network-scripts]# vi ifcfg-eth0-range0

#DEVICE="eth0"
#BOOTPROTO=none
#NM_CONTROLLED="yes"
#ONBOOT=yes
TYPE="Ethernet"
IPADDR_START=172.16.16.126
IPADDR_END=172.16.16.130
IPV6INIT=no
#GATEWAY=172.16.16.100

Save it and restart/start network service

[root@tecmint network-scripts]# /etc/init.d/network restart

Verify that virtual interfaces are created with IP Address.

[root@tecmint network-scripts]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:172.16.16.125  Bcast:172.16.16.100  Mask:255.255.255.224
          inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1385 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1249 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:127317 (124.3 KiB)  TX bytes:200787 (196.0 KiB)
          Interrupt:18 Base address:0x2000

eth0:0     Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:172.16.16.126  Bcast:172.16.16.100  Mask:255.255.255.224
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Base address:0x2000

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:172.16.16.127  Bcast:172.16.16.100  Mask:255.255.255.224
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Base address:0x2000

eth0:2    Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:172.16.16.128  Bcast:172.16.16.100  Mask:255.255.255.224
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Base address:0x2000

eth0:3    Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:172.16.16.129  Bcast:172.16.16.100  Mask:255.255.255.224
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Base address:0x2000

eth0:4    Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:172.16.16.130  Bcast:172.16.16.100  Mask:255.255.255.224
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Base address:0x2000

If you having any trouble in setting up, please do post your queries in the comment section.

He has over 10 years of rich IT experience which includes various Linux Distros, FOSS and Networking. Narad always believes sharing IT knowledge with others and adopts new technology with ease.

Linux Services & Free WordPress Setup

Our post is simply ‘DIY’ aka ‘Do It Yourself, still you may find difficulties and want us to help you out. We offer wide range of Linux and Web Hosting Solutions at fair minimum rates. Please submit your orders by Clicking Here.

29 Responses

  1. Chaitanya says:

    can i have alias address to be in different subnet to that of the primary IP address?

    • David Brown says:

      Yes, you can have your aliases on different subnets. I use that as a “poor man’s VLAN” – you can have different networks on the same physical network without having to use managed switches. Machines on the different networks will generally have very little influence on each other, and unless the user knows what they are doing (such as by reading this article…), they can’t access other systems.

      I also find it very useful for testing and configuring network equipment and other devices. I have a spare ethernet port that is configured with 192.168.1.2, 192.168.2.2, 192.168.99.2, and similar addresses, for convenience when working with routers or other devices with fixed IP addresses.

    • @Chaitanya Yes, create interface for different Subnet as “ifcfg-eth0-range1” and give parameter range as shown above.

  2. John Sanabria says:

    Hi, and thank you for sharing this article. One question, make sense to assign IP addresses for different networks in the same network interface, e.g. 192.168.21.3, 10.0.0.4, 172.16.16.220?

    • Jay says:

      generally, all Alias IP should be from same VLAN which means you can’t. I hope, still network admin dont have any way to allign different VLAN on same interface

  3. BasketCase says:

    Both the ifconfig command and the ethx:y syntax have been obsolete for more than a decade (since the 2.4 kernel came out).

    The correct way to manually add an IP address to an interface is:
    ip addr add 172.16.16.126/27 dev eth0

    The correct way to set this up on a RedHat system is in the ifcfg-eth0 file add additional lines such as:
    IPADDR2=172.16.16.127
    NETMASK2=255.255.255.224

    The correct way to list the IPs on a system is ‘ip addr ls’. The ifconfig command is not compatible with the modern networking features of the Linux kernel.

    • Allen says:

      Thanks I was going to say the same thing. Please don’t use aliases. They aren’t supported any more and you’ll even run into issues where the interface aliases don’t always come up on a reboot.

      Use ‘ip addr show’ rather than ifconfig to see interfaces.

  4. Bruce Hsieh says:

    How about IPv6?

    It seems a totally different story. The difference on Multiple IPs between IPv4 and IPv6 will confuse many linuxers.

  5. David Dreggors says:

    Also, as a quick (temporary) solution, you can use either the ifconfig or ip commands:

    Example:
    ip address add 10.10.20.1/24 dev eth0:0

    or

    ifconfig eth0:0 10.10.20.1/24 up

    These will not persist after a reboot, they are temporary and may come in handy for things like connecting to a new router to configure it, or quickly connect to a misconfigured PC on your network.

    Besides rebooting, you can bring these down manually like this:

    ip address delete 10.10.20.1/24 dev wlp1s0:0

    or

    ifconfig wlp1s0:0 down

    Cheers!

    • David Dreggors says:

      oops… I was testing these commands on my laptop so the commands to bring the temporary aliased IPs should actually be:

      ip address delete 10.10.20.1/24 dev eth0:0
      or
      ifconfig eth0:0 down

      Note that the device should match one of your devices (with :0 after it) as seen by:

      ifconfig

      or

      ip address show

      Each new alias you add (if more than 1) should go up in number as seen in the authors example.

  6. Jay says:

    Simple but handy article. (y)

  7. Martin says:

    Can I have both ipv4 and ipv6 address on one interface?

    I am just trying to setup ipv6 on my ddwrt router.

    • Allen says:

      Yes. It should be doing that by default unless you disabled IPV6. Notice the inet6 section of ip addr show.

      # ip addr show eth1
      3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
      link/ether 00:25:b5:01:10:0e brd ff:ff:ff:ff:ff:ff
      inet 192.168.40.44/16 brd 192.168.255.255 scope global eth1
      inet6 fe80::225:b5ff:fe01:100e/64 scope link
      valid_lft forever preferred_lft forever

  8. Arfat says:

    Can this setting of IP Aliasing will be survive after reboot. After Reboot all IP wil be working.

  9. Arfat says:

    Sir if i do an IP Aliasing. Now how can i open eth0:1 in IPTables Firewall. Pls reply me.
    Thanks in advance.

    • Ravi Saive says:

      You can open any IP Aliasing device in iptables, by below rule. Just replace “eth0:1″ with your IP Aliasing device name.

      # iptables -A INPUT -m state --state NEW -p tcp -i eth0:1 -j ACCEPT
      
  10. aFriend says:

    . I am very happy to see here about assigning Multiple IPs for a single interface.

    . I want to monitor interval timings between the multiple IPs(eth0 and aliases) rotation.

    . how can i check it ?

    Adv..Thanks

    ………………………………
    aFriend

    • Allen says:

      Rotation? Are you referring to dhcp renewals or bonding load balancing via round-robin? If it’s bonding, having both on the same physical interface is worthless. If it’s dhcp renewals, just scan /var/log/messages or your dhcp server logs filtering out just your MAC address.

  11. aFriend says:

    i am using Static IP, that is eth0= x.x.x.1

    using aliases as mentioned below like wise

    eth0:0=x.x.x.2
    eth0:1=x.x.x.3
    eth0:2=x.x.x.4
    eth0:3=x.x.x.5

    i want to monitor the rotation of the IPS (what is the time intervals between the IP rotation?) how can i check it ?

  12. Dwight K says:

    Hi narad,

    I am configuring multiple addresses in my “rc.local” file using:

    ifconfig eth0:1 199.199.199.199 netmask 255.255.255.199
    route add -host 199.199.199.199 dev eth0

    ifconfig eth0:2 199.199.199.200 netmask 255.255.255.199
    route add -host 199.199.199.200 dev eth0

    ———-

    When I used to have only 2 or 3 of these, on boot the interfaces would get setup.

    But now my rc.local file has 10 interfaces and a long firewall startup script.

    I am finding that if my ethernet adapter or upstream router is unplugged temporarily, I lose all the network settings and have to reissue above command.

    Also, on startup, it doesn’t execute my long firewall script, I have to drop all rules, reissues the “ifconfig” command and then rerun the rules in the shell.

    Will your solution solve this problem?

  13. Dwight K says:

    Hi used exactly your commands to try and create the virtual interface, but it didn’t work for me.

    I am using Fedora 13. The syntax at the shell is: service network restart. Then I ran ifconfig and it showed eth0, eth1, and lo – no eth0:1.

    This is what my ifcfg-eth0 file looks like:

    DEVICE=eth0
    ONBOOT=yes
    TYPE=Ethernet
    BOOTPROTO=none
    DNS1=xxx.xxx.xxx.4
    DEFROUTE=yes
    IPV4_FAILURE_FATAL=yes
    IPV6INIT=no
    NAME=”System eth0″
    UUID=xxxxx
    DNS2=xxx.xxx.xxx.6
    HWADDR=xx
    IPADDR0=xxx.xxx.xx.242
    PREFIX0=28
    GATEWAY0=xxx.xxx.xx.241

    This is what I used for my ifcfg-eth0:1

    DEVICE=eth0:1
    ONBOOT=yes
    TYPE=Ethernet
    BOOTPROTO=none
    NAME=”System eth0:1″
    HWADDR=xx
    IPADDR0=xxx.xxx.xx.243
    PREFIX0=28
    GATEWAY0=xxx.xxx.xx.241

    I don’t know what the “IPADDR0″ – the zero is for.

    I read the note from your other user who says to add

    IPADD1
    IPADD2

    directly into the ifcfg-eth0, file.

    When I did that, the network interface is still eth0 – no eth0:1, and it had the last IP address mentioned in the list – so it didn’t create multiple virtual interfaces.

  14. Julius says:

    Hi,
    My Linux servers has 6 ipaddress(3 eth ) . if I execute hostname -I (capital ‘eye’) I get 6 ipaddress (eth0, etho:0,eth1, eth1:0,eth2, eth2:0) and when I execute hostname -i( smal ‘eye’) I get the ipaddres of eth0 in which I am unable to ping because I dont have access but I do have access to eth2 and eth2:0 .
    How do I change in such a way that hostname -i displays the ipaddress of eth2 rather than eth0

  15. kamiquiroz says:

    Hi

    I have configured virtual interfaces when I run ifconfig they appears, but in the network manager just permit one connection, in the end one network exist for the clients is like 8021q is not working any idea_?

  16. BB says:

    IP alias with NetworkManager on FC17 does not work as given here!
    Nor does the method where all IL alais are given as:

    IPADDR0
    IPADDR!
    etc.

    BB

  17. Barbapapa says:

    Hi,
    I have assigned an ip alias on one Ethernet card
    the ifconfig output shows both of them correctly but when I
    unplug cable and plug it, just second Ip Address is
    active and can be ping.
    Any solution will be appreciated.

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.