9

I have an interface eth0, and I wish to give it an extra virtual IP. I achieve this by the following:

ifconfig eth0:0 ip.address.goes.here netmask subnet.address.goes.here

This works fine, however, when I reboot, this is lost.

I have tried editing /etc/network/interfaces to add the following:

auto eth0:0 iface eth0:0 inet static
    address ip.address.goes.here
    netmask subnet.address.goes.here

However, upon rebooting, the static ip for eth0 is loaded fine, but, the eth0:0 virtual IP is not loaded at all.

So, how can I permanently add the eth0:0 virtual IP?

wilhil
  • 1,785
  • Possible duplicate: http://askubuntu.com/questions/45086/how-to-add-an-ip-alias-on-a-bridged-interface .. this is for a bridge, but change br0 to eth0 and the method is the same. – Caesium Nov 27 '11 at 13:54

2 Answers2

13

Instead of that eth0:0 business, you should do this:

  • Configure your (one) static IP address in /etc/network/interfaces as you normally would:

    # The primary network interface
    auto eth0
    iface eth0 inet static
    address 192.168.0.201
    network 192.168.0.0
    netmask 255.255.255.0
    broadcast 192.168.0.255
    gateway 192.168.0.1
    
  • Add another IP to this interface by adding this right after the above:

    up /sbin/ip addr add 192.168.0.203/24 dev eth0
    down /sbin/ip addr del 192.168.0.203/24 dev eth0
    
  • The complete file should look like this

Now, if you check what IP addresses are configured by running ip addr show, both will show up:

2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:1d:fa:0b brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.201/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.203/24 scope global secondary eth0

My thanks to Lekensteyn for pointing me in the right direction. Every site on the internet just talks about eth0:0 for a secondary IP address. This seems like the proper way to do it.

  • 2
    Yes, ifconfig has been deprecated in favour of the iproute2 suite (the ip command). ifconfig will never leave my muscle memory though. Also important of note, ifconfig cannot see address aliases added by iproute2! (this doesn't make them work any less well). – Caesium Nov 27 '11 at 14:21
  • Great! @Caesium that's along the lines of what I suspected. Good to know I wasn't completely misremembering. :) – Stefano Palazzo Nov 27 '11 at 14:23
  • 1
    @Caesium I am a little confused, I tried manually typing "ip addr add 192.168.0.40/24 dev eth0 label eth0:51" which is garbage information - and, it showed up in ifconfig just fine? ... Don't suppose you fancy coming in to chat to talk a little more!? – wilhil Nov 27 '11 at 14:25
  • 2
    @wilhil, ooh! you taught me something, if you add a label it does show up in ifconfig! If you don't use a label it doesn't. Thanks :) – Caesium Nov 27 '11 at 14:32
  • 6
    The pastebin entry posted is no longer available: The Paste you are looking for does not currently exist. – saji89 Mar 20 '14 at 04:43
1

If you want to do things the "traditional" way, the relevant part of /etc/network/interfaces should look like:

auto eth0:0
iface eth0:0 inet static
    address ip.address.goes.here
    netmask subnet.address.goes.here

instead of this, where you made a mistake:

auto eth0:0 iface eth0:0 inet static
    address ip.address.goes.here
    netmask subnet.address.goes.here
  • After adding, execute: ifup eth0:0 to start it manually. You can check if its working by executing: netstat -rn – lepe Aug 17 '15 at 04:16