Skip to content

Beginner · Subnetting practice and IPv6 · topic 19 of 57 · 30 min

IPv6 addresses: format and shortening rules

Explanation

IPv6 was created because IPv4 ran out of addresses. An IPv6 address is 128 bits, written as eight groups of four hexadecimal digits called hextets. You shorten it by dropping leading zeros in each hextet and by replacing the longest run of all-zero hextets with :: once. Most LANs use a /64 subnet, and a site often gets a /48 with 16 bits for subnet IDs.

Why IPv6?

IPv4 has about 4.3 billion addresses, and the free pool ran out years ago. Private addresses and NAT have stretched it, but NAT adds complexity and breaks some applications. IPv6 (Internet Protocol version 6) solves the shortage with 128-bit addresses: 2^128, about 3.4 × 10^38 addresses, enough to give every device its own global address for a very long time.

India is one of the big IPv6 users in the world. Large mobile operators, such as Reliance Jio, built their networks with IPv6 at the core, so a large share of mobile traffic in India runs over IPv6, and India appears near the top of country rankings in IPv6 adoption statistics such as Google's. If you work in an Indian ISP, telecom company or data centre, you will meet IPv6 every day.

Hexadecimal in two minutes

Writing 128 bits in binary or decimal would be very long, so IPv6 uses hexadecimal (hex), base 16. Hex uses the digits 0 to 9 and the letters a to f for 10 to 15. One hex digit is exactly 4 bits, so it is easy to convert.

Hex01289abcdef
Decimal01289101112131415
Binary00000001001010001001101010111100110111101111

The full format

An IPv6 address is eight groups of 16 bits. Each group is called a hextet and is written as four hex digits. The groups are separated by colons. For example: 2001:0db8:0000:0000:0000:ff00:0042:8329. The block 2001:db8::/32 is reserved for documentation, so you will see it in every book and in this course.

Shortening rules (RFC 5952)

  1. Drop the leading zeros in each hextet: 0db8 becomes db8, 0042 becomes 42, and 0000 becomes 0. Never drop trailing zeros: db80 stays db80.
  2. Replace the longest run of two or more all-zero hextets with a double colon, ::. You can do this only once in an address.
  3. If two runs of zeros are equally long, shorten the leftmost one.
  4. Do not use :: for a single zero hextet; write 0 instead.
  5. Write the letters in lowercase.

Worked examples

Full addressShortest correct form
2001:0db8:0000:0000:0000:ff00:0042:83292001:db8::ff00:42:8329
2001:0db8:0000:0001:0000:0000:0000:00012001:db8:0:1::1 (the run of three wins over the single 0)
fe80:0000:0000:0000:0202:b3ff:fe1e:8329fe80::202:b3ff:fe1e:8329
2001:0db8:0000:0000:0001:0000:0000:00012001:db8::1:0:0:1 (a tie, so the left run is shortened)
0000:0000:0000:0000:0000:0000:0000:0001::1 (the loopback address)

Expanding a short address

To expand, count the hextets you can see and fill the :: with enough zero hextets to make eight. 2001:db8::1 shows three hextets, so :: stands for 8 − 3 = 5 zero hextets: 2001:0db8:0000:0000:0000:0000:0000:0001. This is why :: may appear only once: with two of them, 2001::1::2, nobody could tell how many zeros each one stands for.

Prefixes, /64 subnets and /48 sites

IPv6 uses prefix lengths just like CIDR: 2001:db8:acad:1::/64 means the first 64 bits are the network. Almost every LAN in IPv6 is a /64, so the last 64 bits, the interface ID, identify the device. An organisation commonly receives a /48 from its ISP (home connections often get a /56 or a /64; it varies by ISP).

Bits481664
PartGlobal routing prefix (from the ISP)Subnet ID (yours to number)Interface ID (the device)

With a /48 you have 16 bits of subnet ID, so 2^16 = 65,536 /64 subnets. If your site prefix is 2001:db8:acad::/48, your subnets are 2001:db8:acad:0::/64, 2001:db8:acad:1::/64, 2001:db8:acad:2::/64 and so on. Subnetting in IPv6 is usually just counting in the fourth hextet.

python
import ipaddress

a = ipaddress.ip_address("2001:0db8:0000:0000:0000:ff00:0042:8329")
print(a.compressed)   # 2001:db8::ff00:42:8329
print(ipaddress.ip_address("2001:db8::1").exploded)
site = ipaddress.ip_network("2001:db8:acad::/48")
print(next(site.subnets(new_prefix=64)))   # 2001:db8:acad::/64
compressed gives the short form, exploded the full form.

Common mistakes

Careful: Using :: twice. 2001:db8::1::5 is invalid, because the reader cannot know how many zeros each :: hides. Use :: once and write the other zeros as 0.
  • Dropping trailing zeros: 2001:db80::1 and 2001:db8::1 are different addresses.
  • Treating hex letters as mistakes: "abcd" is a normal hextet.
  • Giving a LAN a prefix longer than /64. SLAAC (next topic) needs a /64 to work.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

IPv6 addresses: format and shortening rules · 1/8

IPv6: 128 bits

IPv4 ran out of addresses. IPv6 fixes this with 128-bit addresses, more than enough for every device on Earth.

Prefer a video by a teacher? Find free videos on “ipv6 address format shortening rules explained ccna” on YouTube ↗

In real life: IPv6 on a mobile network

Open the network details on a phone using a large Indian mobile operator and you will usually see a global IPv6 address starting with 240, from the 2400::/12 block that APNIC hands out in the Asia-Pacific. Many large websites and apps, such as video streaming and social media, are reachable over IPv6, so much of that traffic never needs IPv4 NAT. A NOC engineer at such an operator reads and writes IPv6 addresses all day, and the shortening rules are second nature.

python
import ipaddress

for text in ["2001:0db8:0000:0001:0000:0000:0000:0001", "fe80:0000:0000:0000:0202:b3ff:fe1e:8329"]:
    print(ipaddress.ip_address(text).compressed)
Shows 2001:db8:0:1::1 and fe80::202:b3ff:fe1e:8329.

Practice (0/4 done)

Task 1 · Code · Python

Write drop_leading_zeros(hextet) that removes the leading zeros of one hextet, keeping a single "0" if it was all zeros. Example: "0db8" gives "db8", "0000" gives "0", "db80" stays "db80".

Task 2 · Code · Python

Write expand(addr) WITHOUT the ipaddress module. It takes a valid IPv6 address (maybe with :: and short hextets) and returns the full form: eight hextets of four lowercase hex digits. Example: expand("2001:db8::1") returns "2001:0db8:0000:0000:0000:0000:0000:0001".

Task 3 · Code · Python

Write compress(addr) WITHOUT the ipaddress module. The input is always a full address with eight hextets. Apply the RFC 5952 rules: drop leading zeros, replace the longest run of two or more zero hextets with :: (the leftmost if there is a tie), and use lowercase. The tests compare your result with Python's ipaddress module.

Task 4 · Question

Which is a correct shortening of 2001:0db8:0000:0000:0000:0000:0001:0000?

Quick quiz

Question 1

1.How many bits are in an IPv6 address?

Question 2

2.Why is 2001:db8::5::1 invalid?

Question 3

3.What is the full form of fd00::1?

Question 4

4.A site has a /48. How many /64 subnets can it make?

Question 5

5.How many bits does one hex digit stand for?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

How many bits are in an IPv6 address?

Answer: 128

IPv6 addresses are 128 bits: eight hextets of 16 bits.

Why is 2001:db8::5::1 invalid?

Answer: It uses :: twice, so the number of hidden zeros is unclear

:: may appear only once in an address.

What is the full form of fd00::1?

Answer: fd00:0000:0000:0000:0000:0000:0000:0001

Two hextets are shown, so :: stands for six zero hextets.

A site has a /48. How many /64 subnets can it make?

Answer: 65,536

64 − 48 = 16 bits of subnet ID, and 2^16 = 65,536.

How many bits does one hex digit stand for?

Answer: 4

One hex digit is 4 bits, so a hextet of four hex digits is 16 bits.

Which address is private?Related

Answer: 172.20.1.5

172.16.0.0/12 covers 172.16.x.x to 172.31.x.x, so 172.20.1.5 is private; 172.32.1.5 is not.

From IP addresses, subnets and CIDR

Each subnet must hold 25 hosts. Which prefix wastes the fewest addresses?Related

Answer: /27

/27 gives 30 usable hosts. /28 gives only 14, and /26 wastes more.

From Subnetting a network into equal subnets

Why is 10 written as 00001010 in an IP address and not as 1010?Related

Answer: Because every octet is always exactly 8 bits

An octet always has 8 bit positions, so the leading zeros keep every bit in its right place.

From Binary and decimal for networking

Why do private addresses and NAT exist?Related

Answer: Because there are not enough public IPv4 addresses for every device

IPv4 has about 4.3 billion addresses, fewer than the number of devices. Private ranges are reused everywhere and NAT shares a few public addresses.

From IPv4 address structure and special ranges

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 1 more topic in “Subnetting practice and IPv6” before its exam: IPv6 address types, EUI-64, SLAAC and NDP.
  2. Module 4 exam: Subnetting practice and IPv6 · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Switching and VLANs. 57 topics left in the course before the final exam.

← Previous: Subnetting drills for exam speed