Add a second ip to Ubuntu Linux

Written by
Date: 2010-04-06 10:36:30 00:00


There are situations when you want or need a second IP on your box, for example when you are installing a firewall (In this case is always better to have two NICs but you can do it with only one)

We will assume you already have a system running and already has one IP, if not you may want to read Define a static IP in Linux

Now we will add the second IP use the same command specifying a virtual NIC

sudo ifconfig eth0:0 192.168.1.2 netmask 255.255.255.0 up

That’s all, but to make changes permanent, edit the file /etc/network/interfaces

There you may have already have something like this

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address 10.1.1.2
    network 10.1.1.0
    netmask 255.255.255.0
    broadcast 10.1.1.255
    gateway 10.1.1.1

Change it to this:

auto lo
iface lo inet loopback

auto eth0 eth0:0
iface eth0 inet static
    address 10.1.1.2
    network 10.1.1.0
    netmask 255.255.255.0
    broadcast 10.1.1.255
    gateway 10.1.1.1

iface eth0:0 inet static
    address 192.168.1.2
    network 192.168.1.0
    netmask 255.255.255.0

If you want to specify the gateway also here, proceed this way.

auto lo
iface lo inet loopback

auto eth0 eth0:0
iface eth0 inet static
    address 10.1.1.2
    network 10.1.1.0
    netmask 255.255.255.0
    broadcast 10.1.1.255
    gateway 10.1.1.1
    up route add default gw 10.1.1.1
    down route del default gw 10.1.1.1

#Second IP same NIC

iface eth0:0 inet static
    address 192.168.1.2
    network 192.168.1.0
    netmask 255.255.255.0