How to add a loopback interface?

It depends what you want lo or lo: which is an interface alias.

ifconfig lo:0 127.0.0.2 netmask 255.0.0.0 up
ifconfig lo:1 127.0.0.3 netmask 255.0.0.0 up
ifconfig lo:2 127.0.0.4 netmask 255.0.0.0 up

works. If you want to have more IP's on lo use

route add -host 127.0.0.3 dev lo
route add -host 127.0.0.4 dev lo
route add -host 127.0.0.5 dev lo

works too. If you want to remove it, use:

route del -host 127.0.0.3
route del -host 127.0.0.4
route del -host 127.0.0.5

See also IP-Aliasing Linux Networking-HOWTO


If you keep using ifconfig lo... you're not creating a new interface, you're overwriting the previous one. You could try editing your interfaces file:

sudo vim /etc/network/interfaces

Mine looks like this:

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

So, you could try and modifyi it to create new interfaces:

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto lo2
iface lo2 inet loopback
auto lo3

Then, restart the network, or the whole system, and try to interact with the new interfaces with:

sudo ifconfig lo2/lo3 etc...

To add multiple loopback interfaces permanently, must do an additional check for dummy driver.

Dummy driver is used for the making of multiple loopbacks device instead of
creating multiple aliases to one device, with an attachment of different IP.

This lines add another loopback named loop1, loop2, loop3:

sudo ip link add name loop1 type dummy
sudo ip link add name loop2 type dummy
sudo ip link add name loop3 type dummy

Please check that dummy kernel module is loaded before running the above command:

sudo lsmod | grep dummy