Beginner · Subnetting practice and IPv6 · topic 19 of 57 · 30 min
IPv6 addresses: format and shortening rules
Explanation
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.
| Hex | 0 | 1 | 2 | 8 | 9 | a | b | c | d | e | f |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Decimal | 0 | 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| Binary | 0000 | 0001 | 0010 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
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)
- Drop the leading zeros in each hextet: 0db8 becomes db8, 0042 becomes 42, and 0000 becomes 0. Never drop trailing zeros: db80 stays db80.
- Replace the longest run of two or more all-zero hextets with a double colon, ::. You can do this only once in an address.
- If two runs of zeros are equally long, shorten the leftmost one.
- Do not use :: for a single zero hextet; write 0 instead.
- Write the letters in lowercase.
Worked examples
| Full address | Shortest correct form |
|---|---|
| 2001:0db8:0000:0000:0000:ff00:0042:8329 | 2001:db8::ff00:42:8329 |
| 2001:0db8:0000:0001:0000:0000:0000:0001 | 2001:db8:0:1::1 (the run of three wins over the single 0) |
| fe80:0000:0000:0000:0202:b3ff:fe1e:8329 | fe80::202:b3ff:fe1e:8329 |
| 2001:0db8:0000:0000:0001:0000:0000:0001 | 2001: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).
| Bits | 48 | 16 | 64 |
|---|---|---|---|
| Part | Global 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.
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::/64Common mistakes
- 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
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.
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)Practice (0/4 done)
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".
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".
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.
Which is a correct shortening of 2001:0db8:0000:0000:0000:0000:0001:0000?
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- IPv6 address types, EUI-64, SLAAC and NDPSubnetting practice and IPv6 · same module
- VLSM: subnets of different sizesSubnetting practice and IPv6 · same module
- IP addresses, subnets and CIDRCyber Security: from zero to SOC analyst · Networking for security
- IPv4 address structure and special rangesIPv4 addressing
- Validating addresses and checking the same subnetIPv4 addressing
- Subnet masks, prefix length and wildcard masksIPv4 addressing
Questions people ask about this
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.
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.
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.
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.
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 more topic in “Subnetting practice and IPv6” before its exam: IPv6 address types, EUI-64, SLAAC and NDP.
- Module 4 exam: Subnetting practice and IPv6 · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Switching and VLANs. 57 topics left in the course before the final exam.