Linux › Section I · Your network and its configuration › Topic I.4

IPv6 addresses and networks

Most Linux machines today run IPv4 and IPv6 side by side, often without their users noticing. This topic shows how to see a host's IPv6 addresses and routes, why one interface carries several of them, and how to test each protocol separately.

IPv6 is already switched on

The Linux kernel has supported IPv6 for decades, and current distributions enable it by default. If your network's router offers IPv6, a Linux host configures it automatically alongside IPv4, a setup called dual stack. The protocol itself, with its 128-bit addresses, fixed header and lack of broadcast, is taught in the Networking topic IPv6; the same definitions apply here. This topic stays with what Linux shows you and how to read it. The examples use 2001:db8::/32, the prefix reserved for documentation, so they can never clash with a real network.

How Linux prints addresses

An IPv6 address is eight groups of four hexadecimal digits separated by colons. Leading zeros in a group may be dropped, and one run of all-zero groups may be replaced by a double colon. Linux tools print the shortest standard form, in lower case, so 2001:0db8:0000:0042:0000:0000:0000:0001 appears as 2001:db8:0:42::1. Subnets use the same slash notation as IPv4, and almost every LAN is a /64: the first 64 bits identify the network and the last 64 bits, the interface identifier, identify the host. A /64 is so large that address exhaustion is no concern on a single link.

Viewing IPv6 configuration

ip -6 address shows only IPv6 addresses; plain ip address shows them too, on lines starting inet6. An illustrative wired interface might list three. inet6 2001:db8:42:7:9d2e:61b0:4c3a:e18f/64 scope global temporary dynamic. Then inet6 2001:db8:42:7:3c1a:9bff:fe20:51d4/64 scope global dynamic mngtmpaddr noprefixroute. Last, inet6 fe80::3c1a:9bff:fe20:51d4/64 scope link. Each global address has valid_lft and preferred_lft counters in seconds. The loopback interface lo has ::1/128 with scope host, the IPv6 counterpart of 127.0.0.1.

Why one interface has several addresses

Every IPv6 interface has a link-local address from fe80::/10, shown in the fe80::/64 subnet with scope link. It works only on its own link and exists even when there is no router, so neighbours can always talk. A global address, scope global, is reachable across the internet; global unicast addresses currently start with 2 or 3. Many hosts add temporary addresses, marked temporary, which change regularly and are used for outgoing connections so that websites cannot track a device by a fixed address; mngtmpaddr marks the stable address they are generated from. Networks may also use unique local addresses from fc00::/7, in practice starting fd.

Worked example: reading ip -6 address

Use the three illustrative addresses from the earlier card. 1. fe80::3c1a:9bff:fe20:51d4/64 has scope link and the fe80 prefix: it is the link-local address. 2. 2001:db8:42:7:3c1a:9bff:fe20:51d4/64 has scope global and mngtmpaddr: it is the stable global address. 3. 2001:db8:42:7:9d2e:61b0:4c3a:e18f/64 is marked temporary: it is a privacy address for outgoing connections. 4. The global addresses share their first 64 bits, so both are in subnet 2001:db8:42:7::/64. Check: the stable and link-local addresses end in the same 64-bit interface identifier, 3c1a:9bff:fe20:51d4; the temporary one does not.

IPv6 routes

ip -6 route shows the IPv6 routing table. An illustrative host prints 2001:db8:42:7::/64 dev enp3s0 proto ra metric 100, then fe80::/64 dev enp3s0 proto kernel metric 1024, then default via fe80::1 dev enp3s0 proto ra metric 100. The default route, also written ::/0, follows the same longest-prefix rule as IPv4. proto ra means the routes came from a router advertisement, the mechanism described in DHCP and automatic IPv6 configuration. Notice that the gateway is a link-local address. A router's link-local address does not change if the ISP renumbers the global prefix, so routes built on it stay valid.

Link-local addresses need an interface

Because every interface has its own fe80::/64 subnet, a link-local address on its own is ambiguous on a machine with two or more interfaces: the kernel cannot tell which link to use. You must add a zone index, the interface name after a percent sign. ping fe80::1%enp3s0 asks the router on the wired link to reply; plain ping fe80::1 may fail with an error on a multi-interface host. The ip -6 route output above did not need this, because the route already names dev enp3s0. Global addresses never need a zone index, since the routing table picks the interface.

Dual stack and testing each protocol

On a dual-stack host, each application chooses IPv4 or IPv6 when it connects. Programs ask the C library for a name's addresses, and by default the library puts IPv6 addresses first when the host has IPv6 configured, following the address-selection rules of RFC 6724 (which revised RFC 3484); on glibc systems /etc/gai.conf can change that order. Browsers add a race between the two, called Happy Eyeballs, so a broken IPv6 path does not stall a page. To test each protocol on its own, many tools take -4 and -6 options: ping -6 sends ICMPv6 echo requests, and curl -6 fetches a page over IPv6 only. ping6 is the legacy name.

Worked example: checking addresses with Python

Python's ipaddress module, present on most Linux systems, handles IPv6 exactly as it handles IPv4. 1. python3 -c "import ipaddress as i; print(i.ip_address('2001:0db8:0000:0042:0000:0000:0000:0001'))" prints 2001:db8:0:42::1, the same short form ip shows. 2. Replacing print(i.ip_address(...)) with print(i.ip_address(...).exploded) prints all 32 hexadecimal digits again. 3. python3 -c "import ipaddress as i; print(i.ip_interface('2001:db8:42:7:3c1a:9bff:fe20:51d4/64').network)" prints 2001:db8:42:7::/64. 4. Testing membership with in shows fe80::5c3e:11ff:fe4a:2b07 is inside fe80::/10. Check: step 3 agrees with the worked example that grouped the global addresses.

Common mistakes to avoid

First, expecting one IPv6 address per interface; a link-local, a stable global and temporary addresses are normal. Second, treating a link-local default gateway as a misconfiguration; it is standard. Third, pinging a link-local address without a zone index such as %enp3s0. Fourth, assuming an IPv4-only test proves the network works; check IPv6 with ping -6 and curl -6 too. Fifth, using :: twice in one address, or dropping trailing zeros from a group. Sixth, switching IPv6 off to fix a problem instead of diagnosing it, which hides the fault and breaks IPv6-only services.

Key takeaways

Linux runs IPv4 and IPv6 together by default. ip -6 address lists IPv6 addresses on inet6 lines: a link-local address in fe80::/64 with scope link, a global address with scope global, often temporary privacy addresses, and ::1 on lo. LAN subnets are /64. ip -6 route shows routes, usually learned from router advertisements (proto ra), with a link-local default gateway. Link-local destinations need a zone index such as %enp3s0. Applications choose the protocol, generally preferring IPv6; ping -6, ping -4, curl -6 and curl -4 test each one. Documentation examples use 2001:db8::/32.

Flashcards

Flashcard

Dual stack

IPv4 and IPv6 running side by side on one host

The default on current distributions.

Test your understanding

Question 1 of 5

ip -6 address shows fe80::3c1a:9bff:fe20:51d4/64 scope link. What kind of address is it?

Question 2 of 5

ip -6 route shows default via fe80::1 dev enp3s0 proto ra. What does this tell you?

Question 3 of 5

A laptop has wired and Wi-Fi interfaces. Which command reliably pings the router's link-local address on the wired link?

Question 4 of 5

How does Linux print the address 2001:0db8:0000:0042:0000:0000:0000:0001?

Question 5 of 5

Why does an interface often show an address marked temporary as well as a stable global address?

← Previous topicNext topic →