IP Addresses & Routing

How to Read and Shorten an IPv6 Address

Learn the hexadecimal groups, the two shortening rules, the recommended written form and how to expand any IPv6 address by hand.

Key takeaways

  • An IPv6 address is 128 bits, written as eight groups of four hexadecimal digits.
  • You can drop leading zeros in any group, but never trailing zeros.
  • One run of two or more all-zero groups may be replaced by ::, and only once per address.
  • RFC 5952 recommends lowercase letters and compressing the longest zero run, or the first if two tie.
  • Brackets surround an IPv6 address in a URL, and /64 and similar suffixes give the prefix length.

To shorten an IPv6 address, remove the leading zeros from each group, then replace the longest run of all-zero groups with a double colon, which you may use only once. To expand it again, split at the double colon, count the groups you can see, and fill the gap with zero groups until there are eight.

So 2001:0db8:0000:0000:0000:0000:0000:0001 becomes 2001:db8::1. Those two rules cover almost everything. The rest of this guide shows how to apply them without mistakes and how to read what the groups mean.

Hexadecimal in one minute

Hexadecimal is base 16. It uses the digits 0 to 9 and the letters a to f, where a is 10 and f is 15. One hex digit stands for exactly four bits:

Hex Bits Hex Bits
0 0000 8 1000
1 0001 9 1001
2 0010 a 1010
3 0011 b 1011
4 0100 c 1100
5 0101 d 1101
6 0110 e 1110
7 0111 f 1111

Four hex digits make a 16-bit group, and eight groups make 128 bits. People sometimes call a group a "hextet"; you'll also see "group" or "field".

The full form

Written out completely, an address looks like this:

2001:0db8:3c4d:0015:0000:0000:1a2f:00b7

That is 32 hex digits separated by seven colons. The addresses in this article start with 2001:db8, a prefix reserved for documentation, so they belong to no real network.

Reading 2001:db8:3c4d:15::1a2f:b7
2001:db8:3c4d 1 15 2 :: 3 1a2f:b7 4 Routing prefix The first three groups in this example, assigned to a site. 1 Subnet Written in full it is 0015. It selects one LAN. 2 Compressed zeros Stands for the two missing all-zero groups here. 3 End of interface ID The last groups, with a leading zero dropped from 00b7. 4 2001:db8:3c4d 1 15 2 :: 3 1a2f:b7 4 Routing prefix The first three groups in this example, assigned to a site. 1 Subnet Written in full it is 0015. It selects one LAN. 2 Compressed zeros Stands for the two missing all-zero groups here. 3 End of interface ID The last groups, with a leading zero dropped from 00b7. 4

Rule 1: drop leading zeros

Within each group, remove zeros on the left:

  • 0db8 becomes db8
  • 0015 becomes 15
  • 00b7 becomes b7
  • 0000 becomes 0

Zeros in the middle or at the end stay. 1000 cannot become 1, because that would change the value from 4096 to 1. A group is a number, and you only remove the zeros that don't affect it.

After rule 1 the example is:

2001:db8:3c4d:15:0:0:1a2f:b7

Rule 2: compress one run of zero groups

Consecutive groups that are all zero can be replaced by :::

2001:db8:3c4d:15::1a2f:b7

You may do this only once in an address. Consider 2001:db8:0:0:1:0:0:1. If you wrote 2001:db8::1::1, a reader could not know how many zero groups each :: stood for.

RFC 5952 sets out a preferred form so that everyone writes the same address the same way:

  1. Compress the longest run of zero groups.
  2. If two runs are the same length, compress the first one.
  3. Do not use :: for a single zero group; write 0.
  4. Use lowercase letters.

So 2001:db8:0:0:1:0:0:1 is written 2001:db8::1:0:0:1. Software accepts other valid forms as input, but the consistent form makes searching logs and comparing strings far less error-prone.

Shortening an address step by step
1 Start in full 2001:0db8:0000:0000:0 000:ff00:0042:8329 2 Drop leading zeros 2001:db8:0:0:0:ff00:42: 8329 3 Find the longest zero run Three groups of 0 after db8. 4 Replace it with :: 2001:db8::ff00:42:8329 Start in full 2001:0db8:0000:0000:0000:ff00:004 2:8329 1 Drop leading zeros 2001:db8:0:0:0:ff00:42:8329 2 Find the longest zero run Three groups of 0 after db8. 3 Replace it with :: 2001:db8::ff00:42:8329 4

Expanding by hand

Take fe80::2c1:9aff:fe3b:7.

  1. Split at ::. On the left you have fe80. On the right you have 2c1, 9aff, fe3b, 7.
  2. Count visible groups: 1 plus 4 is 5.
  3. Eight minus 5 leaves 3 zero groups.
  4. Pad every group to four digits.
fe80:0000:0000:0000:02c1:9aff:fe3b:0007

If the :: is at the start or end, one side is empty. ::1 is seven zero groups followed by 0001. 2001:db8:: is 2001:0db8 followed by seven minus one, that is six, zero groups. And :: alone is all zeros, the "unspecified" address.

Practice examples

Full form Recommended short form
0000:0000:0000:0000:0000:0000:0000:0001 ::1
fe80:0000:0000:0000:0000:0000:0000:0001 fe80::1
2001:0db8:0000:0000:0000:0000:0002:0000 2001:db8::2:0
2001:0db8:0000:0001:0000:0000:0000:0001 2001:db8:0:1::1
2001:0db8:0000:0001:0001:0001:0001:0001 2001:db8:0:1:1:1:1:1

The fourth row shows the longest-run rule: the single zero after db8 stays as 0, and the run of three is compressed. The last row has only one lone zero group, so there is nothing to compress.

Prefix lengths

A slash and a number say how many leading bits form the prefix:

2001:db8:3c4d:15::/64

Because each hex digit is four bits, prefix lengths divisible by 4 fall neatly between digits, and those divisible by 16 fall between groups:

Prefix Fixed part
/32 First 2 groups
/48 First 3 groups
/56 3 groups plus 2 digits
/60 3 groups plus 3 digits
/64 First 4 groups

One trap: with /56, count digits in the padded form. In 2001:db8:3c4d:15::/56 the fourth group is 0015, so the first two digits, 00, are part of the prefix and 15 is not. The correct network is 2001:db8:3c4d::/56. Expand before you cut. IPv6 subnetting goes further.

Special written forms

In URLs, put the address in square brackets so the port's colon isn't confused with the address:

http://[2001:db8::10]:8080/status

With a zone identifier. Link-local addresses exist on every interface, so commands may need to know which one:

fe80::1%eth0
fe80::1%12

The first is Linux style with an interface name; the second is Windows style with an interface number.

With embedded IPv4. The final 32 bits can be written in dotted decimal, as in the IPv4-mapped address ::ffff:192.0.2.1, which software uses to represent an IPv4 peer inside an IPv6 socket. The IPv4 to IPv6 representations tool shows these forms for any IPv4 address.

Recognizing the type from the first digits

You can often tell what kind of address you have at a glance:

  • 2 or 3 first: global unicast.
  • fe80: link-local.
  • fc or fd: unique local.
  • ff: multicast.
  • ::1: loopback.

IPv6 address types explains them. Watch the padding here too: a group written fd on its own, as in fd::1, is really 00fd and is not a unique local address.

Comparing and storing addresses safely

Because many strings can represent one address, comparing text is unreliable. 2001:DB8:0:0:0:0:0:1 and 2001:db8::1 are equal. In code, parse addresses into their binary value with a proper library, then compare or reformat. On a machine with Python:

python3 -c "import ipaddress; a = ipaddress.ip_address('2001:0DB8:0:0:0:0:0:0001'); print(a.compressed); print(a.exploded)"

On Windows the command may be python. For databases, use a native IP address type or a binary column. If you must store text, normalize first and allow at least 45 characters, which covers the longest embedded-IPv4 form without a zone identifier. Regular expressions for validating IPv6 are notoriously easy to get wrong, so use the parser your language provides.

Typing and reading aloud

Long addresses invite mistakes. A few habits help:

  • Copy and paste rather than retype.
  • Read group by group, and say "double colon" explicitly.
  • Watch look-alike characters, such as b and 8 in poor fonts.
  • Publish names in DNS so people rarely need to see the number. AAAA records do that job.
  • When you assign server addresses by hand, choose simple ones within your prefix, such as ending in ::10 or ::53. Easy addresses are convenient, though they are also easier for a scanner to guess, so keep the firewall tight.

Common mistakes and how to catch them

Using two double colons. 2001:db8::25::1 is invalid. If an address has two separate zero runs, compress one and write the other out as zeros.

Trimming trailing zeros. 2001:db8:a000:: is not the same as 2001:db8:a::. The first has the group a000; the second has 000a.

Miscounting after compression. After expanding, count the groups. Fewer or more than eight means something went wrong. Remember that an embedded IPv4 address in dotted form counts as two groups.

Reading a port as part of the address. Without brackets, 2001:db8::1:443 is a valid address whose last group is 443. It is not address 2001:db8::1 on port 443. Write [2001:db8::1]:443 when you mean a port.

Forgetting the prefix length. An address in a configuration file without /64 may be treated as a single /128 host, and then neighbors on the same subnet appear unreachable. State the length whenever the tool asks for it.

Searching logs for only one spelling. Different programs may print uppercase, full-length or compressed forms. Normalize before searching, or your search can miss entries that are there.

Reverse DNS names

There is one place where you write every digit: reverse DNS. The name used to look up an IPv6 address is built from all 32 hex digits in reverse order, separated by dots, under ip6.arpa. For 2001:db8::1, you expand to the full form, remove the colons, reverse the digits and add the suffix, so the name starts 1.0.0.0 and ends 8.b.d.0.1.0.0.2.ip6.arpa.

You rarely need to do this by hand. On macOS or Linux, dig -x builds the name for you:

dig -x 2001:db8::1

The documentation address will not return a real name, but the question section of the output shows the full reversed form, which makes it a handy way to check an expansion. PTR records explains who controls these zones.

A quick self-test

Try these before reading the answers.

  1. Shorten 2001:0db8:0000:0000:0008:0800:200c:417a.
  2. Expand ff02::1:2.
  3. Is 2001:db8::0:1:0:0:1 in recommended form?

Answers: the first is 2001:db8::8:800:200c:417a. The second is ff02:0000:0000:0000:0000:0000:0001:0002. For the third, no: expanded, it is 2001:db8:0:0:1:0:0:1, and the recommended spelling is 2001:db8::1:0:0:1. The :: should absorb the whole first run, with no extra 0 beside it.

Common questions

Is the letter case significant?

No. ABCD and abcd are the same value. Lowercase is the recommended way to write it.

Can I use :: for just one zero group?

Parsers generally accept it as input, but the recommended form writes a single zero group as 0.

Why does my computer show a percent sign and number at the end?

That is the zone identifier telling the system which interface a link-local address belongs to. It matters only on your machine and isn't sent in packets.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in IP Addresses & Routing →

Latest news on this topic