27

I have a development environment set up where I have a separate loopback address for multiple websites.

For example, I have the following:

127.0.0.1 www.example.com
127.0.0.2 foo.example.com
127.0.0.3 bar.example.com
127.0.0.4 waffles.example.com

I'd like an equivalent solution for IPv6.

I already know that you can use ::1 as a loopback address, but ::2, ::3, etc don't seem to work.

Are there any other loopback addresses in IPv6? Is there a way to have multiple unique loopback addresses?

kasperd
  • 29,894
  • 16
  • 72
  • 122
Doctor Jones
  • 370
  • 1
  • 3
  • 7
  • 1
    I'd recommend ::ffff:7f00:1, ::ffff:7f00:2, and so on, but no doubt someone would throw rotten fruit at me. – womble Jul 27 '11 at 10:21
  • You could try something in the IPv6 interface-local multicast scope. That will loop back into your host, and it is a huge range. Whether or not it works for you is going to depend on what it is you are trying to do. – Ron Maupin Jan 12 '19 at 16:59

6 Answers6

15

Technically ::2, ::3 etc. are part of ::0.0.0.0/96, the "ipv4 compatible ipv6 address" range. It's deprecated, but you probably don't want to use that range.

For a lab environment, use unique local addressing. Go to https://www.ultratools.com/tools/rangeGenerator and generate yourself a prefix. Then you can pick whatever addresses you want out of that prefix, and assign them to the loopback interface.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
Ben Jencks
  • 1,351
  • 8
  • 13
  • 1
    Why not ::127.0.0.0/96 ? – Kind Contributor Dec 02 '19 at 03:48
  • Quick note: https://www.ultratools.com/tools/rangeGenerator seems to be not available any more. Instead, one can use services like https://dnschecker.org/ipv6-address-generator.php or https://simpledns.plus/private-ipv6 – msiemens Feb 17 '22 at 12:25
10

I recommend using RFC 4193 addresses for this. RFC 4193 allows you to construct your own /48 for local use by taking the byte value fd followed by 5 random bytes. You are allowed to put anything you see fit after the first 48 bits, so if you wanted a /64 you can take fd followed by 7 random bytes as in this example for Linux systems:

ip -6 route add to local fd66:29e9:f422:8dfe::/64 dev lo

Using RFC 4193 has the advantage compared to earlier answers that you are not violating any RFCs in doing so and the addresses can be used without an interface identifier.

Each of the ranges mentioned in earlier answers are either using ranges reserved for different purposes or link-local addresses which require an interface identifier whenever used.

There exists an expired draft suggesting that the range 1::/32 gets allocated for additional loopback addresses like you are asking for. However since that draft expired years ago and no such allocation was ever made you cannot use 1::/32 for this.

kasperd
  • 29,894
  • 16
  • 72
  • 122
  • I have now written a small command line tool to generate RFC 4193 compliant prefixes: https://v6tools.kasperd.dk/rfc4193/ – kasperd Jan 04 '19 at 12:30
9

Your loopback adress is ::1/128. Note the width of the subnet which restricts it to just one host. change the subnetmask to something wider and check your routing table. Or use link-local adresses.

Fladi
  • 850
  • 4
  • 7
  • 3
    This answer would be better, if you added, on either or both, of windows or linux, how to bind additional IPv6 addresses to the loopback interface. In IPv4, you get 127.0.0.1/8, and in IPv6 you get ::1/128, so is there any way to add some more for IPv6 ? – William Feb 16 '16 at 21:15
  • Its a general answer, without any link and sample. – Mohammad Nikravan Oct 11 '21 at 12:15
4

There is a RFC proposal called A Larger Loopback Prefix for IPv6, which proposes using 1::/32 as loopback addresses. However, the proposal was not approved and expired already in 2013.

Ben Z.
  • 103
  • 4
F.Raab
  • 141
  • 2
  • This was already mentioned in the answer I wrote last year on this very question. – kasperd Jan 13 '19 at 16:34
  • @kaspered Oh yea you are right, sorry did not notice since the answer includes in fact two answers and begins with the other one. – F.Raab Jan 16 '19 at 13:05
  • I did not really consider the expired draft to be an answer because it never became a standard. I noted it under my answer because it's relevant background information and provides some context on what attempt has been on standardizing on a range for that particular purpose. – kasperd Jan 16 '19 at 13:20
2
jcomeau@aspire:~/rentacoder/jcomeau/kybyz$ sudo ip route add local ::/104 dev lo 
jcomeau@aspire:~/rentacoder/jcomeau/kybyz$ ping6 ::2
PING ::2(::2) 56 data bytes
64 bytes from ::2: icmp_seq=1 ttl=64 time=0.083 ms
64 bytes from ::2: icmp_seq=2 ttl=64 time=0.128 ms
^C
--- ::2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.083/0.105/0.128/0.024 ms
jcomeau@aspire:~/rentacoder/jcomeau/kybyz$ ping6 ::3
PING ::3(::3) 56 data bytes
64 bytes from ::3: icmp_seq=1 ttl=64 time=0.148 ms
64 bytes from ::3: icmp_seq=2 ttl=64 time=0.141 ms
64 bytes from ::3: icmp_seq=3 ttl=64 time=0.142 ms

from Can I bind a (large) block of addresses to an interface?

it's the only thing that worked for me. linux-specific, of course.

nota bene: because this may conflict with 0.0.0.0/8 addresses at some time in the future, ::127.0.0.0/104 would probably be a better choice.

jcomeau_ictx
  • 252
  • 4
  • 13
  • This is effectively widening the routing table into the range reserved for ipv4 compatibility as described by Ben Jencks. – korkman Feb 05 '16 at 14:27
  • that concerns me none whatsoever. it's for testing purposes; it's on my local machine; it affects nobody but myself. – jcomeau_ictx Feb 05 '16 at 16:10
  • 1
    @jcomeau_ictx Whether or not it concerns you, it might concern anyone who will use your answer, because it will then effect *them*. – mtraceur Oct 11 '18 at 23:26
  • it might, but arguably not until the ipv4 range 0/8 is assigned. anyway, I'll fix my answer. – jcomeau_ictx Oct 12 '18 at 00:14
  • it would have been bad had I used /96, but I used /104. anyway, I edited the answer to use ::127.0.0.0/104, the loopback range. – jcomeau_ictx Oct 12 '18 at 00:27
0

Using link-local addressing seems to be the only valid option. What most suggestions here ignore is the fact that the IPv6 prefix declares scope, and I would not want my presumably private addressing to be in global scope.

So for Linux:

$ sudo ip -6 address add fe80::1/64 dev lo
$ sudo ip -6 address add fe80::2/64 dev lo
$ sudo ip -6 address add fe80::3/64 dev lo

And specify interface when referring to such addresses. Testing with netcat:

$ nc -l -p 10001 fe80::1%lo
$ nc fe80::1%lo 10001

Verifying correct scope:

$ ip -6 a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 
    inet6 fe80::1/64 scope link 
       valid_lft forever preferred_lft forever

Still it seems not very elegant having to add every single IP to be able to bind to it. With 127.0.0.0/8, you can bind to any address without prior assigning them.

korkman
  • 1,647
  • 2
  • 13
  • 26
  • No, ULA is a valid way to do site local addressing. It won't route by default on the Internet. You can route it locally however. Every subnet at a site can have a reasonably globally unique private *prefix*, independent of ISP. – John Mahowald Feb 06 '16 at 15:02
  • 1
    And site local is NOT host local. That's my point :-) – korkman Feb 09 '16 at 13:40
  • Is there an advantage of adding a link-local IPv6 address instead of simply ::2, with the command `sudo ip -6 address add ::2 dev lo`? – baptx Apr 12 '20 at 14:35
  • Just adding ::2 shows up as "scope global" in "ip -6 a", which is different than ::1. It was worrying me, but whether this has actual side effects is currently unknown to me. – korkman Apr 18 '20 at 19:39