What are the rules for shortening an IPv6 address?
Show the answer
Say this first
Two rules. First, drop leading zeros inside each hextet (0db8 becomes db8, 0000 becomes 0),
never trailing zeros. Second, replace one run of consecutive all-zero hextets with ::, and only once
per address. The canonical form (RFC 5952) uses :: on the longest run, the leftmost run if two are
equal, never on a single zero hextet, and writes hex in lowercase.
Go deeper
Worked example
2001:0db8:0000:0000:0000:ff00:0042:8329
2001:db8:0:0:0:ff00:42:8329 leading zeros removed
2001:db8::ff00:42:8329 longest zero run replaced with ::Why :: only once
The reader rebuilds the address by counting the hextets that are written and filling the gap with zeros
until there are eight. With two :: there is no way to know how many zeros go in each gap.
Expanding an address
Count the written hextets, then insert zeros for the rest.
2001:db8:0:a::b:0 6 hextets written, so :: stands for 2
2001:0db8:0000:000a:0000:0000:000b:0000RFC 5952 canonical form
Any valid form is accepted as input. RFC 5952 defines the one form tools should print, so logs, configs and search results match:
- lowercase hex
- leading zeros removed
::on the longest run of two or more zero hextets, the first one on a tie- no
::for a single0hextet:2001:db8:0:1:1:1:1:1stays as is
They will ask next
Is 2001:db8:100:: the same as 2001:db8:1::?
No. The third hextet is
0100in the first and0001in the second. You may only drop leading zeros, so0100shortens to100, never to1.Does it matter which form you type into a router?
Not to the router, it parses all valid forms. It matters for people: compare and grep addresses in the canonical form, or you will miss matches in logs and IPAM.
Traps
- Removing trailing zeros.
0100is100, not1. - Using
::twice in one address. - Using
::to replace a single zero hextet. It is valid input but not canonical.