How do I set an extra IP for an existing interface?

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?


Solution 1:

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.

Solution 2:

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