Sub topic 17
Inspecting addresses in practice
This chapter assumes you know the address types covered earlier: global unicast (2000::/3), link-local (fe80::/10), unique local (fc00::/7), loopback, unspecified, and multicast (ff00::/8). It also assumes you can read compressed notation and prefix lengths. The goal is to look at real addresses on a real host and say what each one is, where it came from, and whether it works.
What you can learn from an address
An address's text form tells you most of what you need to know.
- The leading bits give its type and scope.
- The prefix length tells you which part names the network.
- The last 64 bits usually form the interface identifier. Its shape hints at how it was generated.
You can check these facts by hand, with operating system tools, or with a script. Using all three lets you catch mistakes in any one of them.
Normalizing text with Python
Compressed text is hard to compare by eye. Two strings can name the same address, such as 2001:db8::1 and 2001:0DB8:0:0:0:0:0:1. The ipaddress module parses either form and prints a canonical one.
import ipaddress
addr = ipaddress.ip_address("2001:0db8:0000:0000:0000:ff00:0042:8329")
print(addr.compressed) # 2001:db8::ff00:42:8329
print(addr.exploded) # 2001:0db8:0000:0000:0000:ff00:0042:8329Use compressed when you store or compare addresses as text. It gives the shortest canonical form, in lowercase. Use exploded when you want to line up groups in a log or check a nibble boundary.
Classifying addresses
The is_* properties classify an address by its prefix.
import ipaddress
for text in ["::1", "fe80::1", "fd12:3456:789a::1", "ff02::1"]:
a = ipaddress.ip_address(text)
print(text, a.is_loopback, a.is_link_local, a.is_private, a.is_multicast)
# ::1 True False True False
# fe80::1 False True True False
# fd12:3456:789a::1 False False True False
# ff02::1 False False False TrueThe output shows a gotcha. is_private is True for loopback, link-local, and unique local addresses alike, so it cannot tell them apart. It is also True for the documentation range 2001:db8::/32. Use is_private as a rough "not globally routable" test, not as a type label.
For a precise label, test membership in the prefixes you learned earlier. Check the narrow cases first:
import ipaddress
PREFIXES = [
("unique local", ipaddress.ip_network("fc00::/7")),
("global unicast", ipaddress.ip_network("2000::/3")),
]
def classify(text):
a = ipaddress.ip_address(text)
if a.version != 6:
raise ValueError("not an IPv6 address")
if a == ipaddress.IPv6Address("::"):
return "unspecified"
if a.is_loopback:
return "loopback"
if a.is_multicast:
return "multicast"
if a.is_link_local:
return "link-local"
for label, net in PREFIXES:
if a in net:
return label
return "other"
for t in ["::", "::1", "fe80::1", "fd00::5", "ff02::1", "2001:db8::1"]:
print(t, "->", classify(t))Note that 2001:db8::1 is classified as global unicast. It sits inside 2000::/3 even though it is reserved for documentation. A prefix test tells you the format, not whether the address is actually reachable on the Internet.
Zone identifiers
A link-local address is only meaningful on one link. A host with two interfaces can have fe80::1 reachable on both, so tools need a zone identifier, written after a percent sign: fe80::1%eth0. On Windows the zone is usually a number, as in fe80::1%12. Python 3.9 and later parse the zone and keep it in scope_id. Earlier versions raise ValueError.
import ipaddress
a = ipaddress.ip_address("fe80::1%eth0")
print(a.is_link_local) # True
print(a.scope_id) # eth0Recognizing how an interface identifier was made
Many interface identifiers built from a MAC address show an ff:fe pattern in the middle. This is the modified EUI-64 method. It splits the MAC, inserts ff:fe, and flips the universal/local bit (0x02) in the first byte.
import ipaddress
def eui64_link_local(mac):
b = bytearray(int(x, 16) for x in mac.split(":"))
b[0] ^= 0x02 # flip the universal/local bit
iid = bytes(b[:3]) + b"\xff\xfe" + bytes(b[3:])
return ipaddress.IPv6Address(b"\xfe\x80" + bytes(6) + iid)
print(eui64_link_local("00:1a:2b:3c:4d:5e")) # fe80::21a:2bff:fe3c:4d5eYou can use this to check a suspected match. If an address ends in ...:xxff:fexx:xxxx and the MAC derived from it matches a device on your network, the address is EUI-64 based. Most modern systems use random or opaque identifiers for global addresses instead. A tail with no ff:fe therefore does not mean the address is hand-assigned.
Inspecting a Linux host
Three commands cover the basics.
ip -6 addr show
ip -6 route show
ping -6 -c 3 ::1Typical output from ip -6 addr show looks like this. The addresses are sample values.
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 2001:db8:abcd:1:8c3a:5e1f:2b7d:90a4/64 scope global temporary dynamic
valid_lft 86212sec preferred_lft 14212sec
inet6 2001:db8:abcd:1:21a:2bff:fe3c:4d5e/64 scope global dynamic mngtmpaddr
valid_lft 86212sec preferred_lft 14212sec
inet6 fe80::21a:2bff:fe3c:4d5e/64 scope link
valid_lft forever preferred_lft foreverRead each line in this order:
- The prefix length after the slash shows the on-link prefix. It is normally /64.
scope globalorscope linkmatches the type: global unicast or link-local.temporarymarks a privacy address used for outgoing connections.mngtmpaddrmarks the stable address from which temporary ones are generated.dynamicmeans the address came from SLAAC or DHCPv6. The lifetimes count down from the values in the Router Advertisement. An address past itspreferred_lftis deprecated: existing connections keep using it, but new ones prefer another address.- A
tentativeflag means duplicate address detection has not finished. The address cannot be used yet.
ip -6 route show should include a connected route for the /64, a fe80::/64 route, and a default route through a router's link-local address, for example default via fe80::1 dev eth0. A missing default route is the usual reason a host with a valid global address cannot reach the Internet.
The ping -6 -c 3 ::1 command tests only the local IPv6 stack. To test the link, ping the all-nodes multicast address with a zone: ping -6 -c 3 ff02::1%eth0. Replace eth0 with your interface name. Every IPv6 host on the link should answer.
A diagnostic workflow
The flowchart below turns the checks into a repeatable order. Start from the symptom "this host has no IPv6 connectivity".
Pinging a literal address first, such as 2001:4860:4860::8888, separates routing problems from DNS problems. If that works but a hostname fails, the fault is in name resolution, not addressing.
Common errors
| Symptom | Likely cause |
|---|---|
| `Invalid argument` pinging `fe80::1` | Missing zone identifier. Add `%eth0`. |
| Address stays `tentative` | Duplicate address detection is running or has failed. |
| Global address but no Internet | No default route, or no upstream IPv6 routing. |
| Two global addresses on one interface | Normal. One is stable and one is a temporary privacy address. |
| Only a link-local address | No Router Advertisement received, or the interface ignores them. |
| `ValueError` parsing in Python | More than one `::`, a group longer than four digits, or a zone on Python before 3.9. |
Exercise
A host shows inet6 fd12:3456:789a:1:5c2e:91ff:fe0a:77b3/64 scope global dynamic. Using the classify function from this chapter, what type does it report? What does the ff:fe pattern suggest about the interface identifier? Can this address be reached from the public Internet?
Show answer
classify reports "unique local", because fd12:... falls inside fc00::/7 and none of the earlier checks match. The ff:fe in the middle of the identifier suggests it was built with modified EUI-64 from a MAC address, which would be 5e:2e:91:0a:77:b3 after flipping the universal/local bit back. A unique local address is not routed on the public Internet, so it is reachable only within the organization's own networks. The scope global label in ip output refers to the address scope, not to Internet reachability.