32

I want to add two more loopback interfaces.

I used the following command:

ifconfig lo: 127.0.0.2 netmask 255.0.0.0 up

and type in ifconfig, i can see the loopback address was added.

But if i want to add one more interface... (for example 127.0.0.3) the previous interface (127.0.0.2) was overwritten.

Also when i look in /etc/network/interfaces i see no entry.

How can i add multiple loopback interfaces permanently?

5 Answers 5

38

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

7
  • 1
    Works. Thanks! But what is the difference between lo and lo: and how to remove an interface from lo:?
    – Leviathan
    Apr 6, 2014 at 20:01
  • 1
    never saw lo: before. Remove with ifconfig lo: down
    – user224465
    Apr 6, 2014 at 20:46
  • What is the difference between ifconfig lo: and route add -host ? Are they doing the same thing?
    – hengxin
    Jul 30, 2014 at 8:00
  • 1
    lo:<foo> is a new interface that use lo as a base, its called a alias and you can operate it as a normal interface: set it up, down, etc. It creates a separation between just adding more ips to a interface or creating a new interface. It is useful when you dont want actions applied on one ip to be done also to the rest of the ips in one interface Jun 3, 2015 at 14:09
  • 1
    Used route add -host [dest] dev lo, and it caused some really unexpected behavior. It doesn't work on its own, without creating the interface alias route add doesn't work - not sure what it does. Does lo then forward the packages to the default gateway, or what happens? Get ping timeouts with route add. Seems like it might be dangerous.
    – nichtNox
    May 22, 2017 at 14:50
7

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...
6
  • What are the ips for the new created lo2 and lo3? By the way, what do the numbers (interfaces(5), ifup(8), and ifdown(8)) mean? Thanks.
    – hengxin
    Jul 30, 2014 at 8:05
  • Those numbers are references to the man pages, but don't know exactly how they work. The lines are comented anyway. About the IP, you'll need to set them with ifconfig or with address 127.0.0.2 etc under each interface. Oct 7, 2014 at 17:25
  • If you're curious what the numbers mean, run man man; the numbers indicate what section of the manual each manpage is in. Section 5 is for file formats and conventions, for example, and section 8 is for system administration commands (that usually are only expected to be run by root). Jul 6, 2015 at 23:36
  • 5
    I don't think that this actually works. You can only have on "lo" device per namespace according to driver/net/loopback.c. The "dummy" device may do everything you need, "sudo ip link add name loop1 type dummy"
    – mcr
    Sep 25, 2015 at 13:42
  • 2
    This doesn't work: Cannot find device "lo1" Jan 4, 2018 at 15:14
5

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
1
  • this adds a dummy interface which works similar but lacks flags the lo interface has
    – sjas
    Jul 7, 2018 at 18:27
3

When using the newer iproute2 tool you can also do what user224465 suggests:

ip addr add 127.0.0.2 dev lo
route add -host 127.0.0.2 dev lo

I've used this to create a fake ip address for aws on docker, nl. 169.254.169.254 Combine this with ectou_metadata and you have aws from the cloud on your docker image.

3
  • This is the correct answer for modern Ubuntu hosts
    – Rino Bino
    Feb 1, 2023 at 19:01
  • Is this permanent or transient? i.e. will these settings persist after a reboot?
    – Coder Guy
    Mar 8, 2023 at 19:46
  • This is transient. Mar 9, 2023 at 20:38
0

I've notice that I'm not the only one looking for a solution for multiple loopback interfaces. In the last period I was playing a lot with FRRouting and route-leak between VRF and I notice a strange problem with dummy interfaces: are not responding to ping if the dummy interface is under a VRF (more explantions in README.md on the github). For this reason I was writing a new driver based on dummy, VRF interface and loopback and I was able to make it to responds to ping.

Maybe there is somebody else intereseted to test it and see if it's suitable as a multi loopback interface.

Here are the sources: https://github.com/EasyNetDev/linux-multi-loopback

Also I opened a ticket to Linux Kernel mentioning this misbehavior of the dummy driver and about this new driver: https://bugzilla.kernel.org/show_bug.cgi?id=215675

Hope it helps.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .