IPv6 全方位指南——從核心概念到網路實踐
Final Version: A Comprehensive Guide to IPv6—From Core Concepts to Network Implementation
第一章:IPv6 簡介
Chapter 1: Introduction to IPv6
1.1 為何需要 IPv6:IPv4 的侷限
網際網路的基石是網際網路協定(IP),它使得全球的電腦能夠相互定位和通訊。長久以來,我們使用的是第四版協定(IPv4)。然而,IPv4 在設計之初並未預見到網際網路今日的爆炸性成長。它使用32位元位址,理論上限值約為42.9億個位址。隨著個人電腦、智慧型手機、物聯網(IoT)設備等連網裝置的幾何級數增長,IPv4 位址已於2019年底全球範圍內宣告耗盡,這成為了制約網際網路持續發展的關鍵瓶頸。
1.1 Why We Need IPv6: The Limitations of IPv4
The foundation of the internet is the Internet Protocol (IP), which enables computers worldwide to locate and communicate with one another. For decades, we have used its fourth version (IPv4). However, IPv4 was not designed to anticipate the explosive growth the internet sees today. It uses 32-bit addresses, offering a theoretical maximum of about 4.29 billion unique addresses. With the geometric increase of connected devices like personal computers, smartphones, and the Internet of Things (IoT), the global pool of IPv4 addresses was declared exhausted by the end of 2019, becoming a critical bottleneck for the internet’s continued expansion.
1.2 IPv6 的核心優勢
為了解決此問題,網際網路工程任務組(IETF)設計了第六版網際網路協定(IPv6)。IPv6 並非 IPv4 的簡單升級,而是一次全面的革新,帶來了多項核心優勢:
- 龐大的位址空間: IPv6 使用128位元位址,提供了約 3.4×10³⁸ 個位址,數量之巨足以為地球上的每一粒沙子分配一個IP位址。這徹底解決了位址耗盡的問題,並使網路位址轉換(NAT)技術不再是必要之惡。
- 簡化的標頭格式: IPv6 的標頭結構比 IPv4 更簡潔,移除了不必要的欄位,這使得路由器的處理效率更高,有助於提升網路的整體效能。
- 內建的安全性: 網際網路安全協定(IPsec)是 IPv6 設計中的強制性組成部分,為網路通訊提供了端到端的加密和身份驗證,增強了安全性。
- 更佳的行動性與自動組態: IPv6 的設計原生支援行動 IP,並且擁有強大的無狀態位址自動配置(SLAAC)能力,讓設備能「即插即用」,大幅簡化了網路管理。
- 提升的網路效率: IPv6 廢除了廣播(Broadcast),改用更高效的多點傳送(Multicast)進行一對多通訊,減少了不必要的網路流量。
1.2 Core Advantages of IPv6
To solve this problem, the Internet Engineering Task Force (IETF) designed the Internet Protocol version 6 (IPv6). IPv6 is not a simple upgrade from IPv4 but a comprehensive overhaul, bringing several core advantages:
- Vast Address Space: IPv6 uses 128-bit addresses, providing approximately 3.4×10³⁸ addresses—a number so large it’s enough to assign an IP address to every grain of sand on Earth. This completely solves the address exhaustion problem and makes technologies like Network Address Translation (NAT) no longer a necessary evil.
- Simplified Header Format: The IPv6 header structure is simpler than IPv4’s, removing unnecessary fields. This allows routers to process packets more efficiently, helping to improve overall network performance.
- Built-in Security: Internet Protocol Security (IPsec) is a mandatory component of the IPv6 design, providing end-to-end encryption and authentication for network communications and enhancing security.
- Better Mobility and Autoconfiguration: IPv6 has native support for Mobile IP and features a powerful Stateless Address Autoconfiguration (SLAAC) capability, allowing devices to be “plug-and-play” and significantly simplifying network management.
- Improved Network Efficiency: IPv6 eliminates broadcasts and replaces them with more efficient multicasts for one-to-many communication, reducing unnecessary network traffic.
1.3 IPv6 位址結構與表示法
一個 IPv6 位址由128位元(16個位元組)組成,通常以八組四位數的十六進位數字表示,組與組之間以冒號分隔。
- 標準表示法: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
- 壓縮表示法: 為了方便書寫,可以套用兩個規則:
- 省略前導零: 每組數字中開頭的零可以省略。例如 0db8 可以寫成 db8。
- 壓縮連續的零: 連續出現的一串值為零的組,可以用雙冒號 :: 來取代。這個規則在一個位址中只能使用一次。
- 套用規則後,上述位址可簡寫為:2001:db8:85a3::8a2e:370:7334
1.3 IPv6 Address Structure and Representation
An IPv6 address consists of 128 bits (16 bytes), typically represented as eight groups of four hexadecimal digits, separated by colons.
- Standard Notation: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
- Compressed Notation: To simplify writing, two rules can be applied:
- Omit Leading Zeros: Leading zeros in any group can be omitted. For example, 0db8 can be written as db8.
- Compress Consecutive Zeros: A single, contiguous series of all-zero groups can be replaced by a double colon ::. This rule can only be used once per address.
- After applying these rules, the address above can be shortened to: 2001:db8:85a3::8a2e:370:7334
第二章:IPv6 位址分配的核心概念
Chapter 2: Core Concepts of IPv6 Address Allocation
2.1 網路前綴 (Network Prefix) 的角色
在 IPv6 中,類似於 IPv4 的子網路遮罩(Subnet Mask)的概念由「網路前綴」及其長度來表示。前綴長度以斜線 / 加上一個數字(0-128)來註明,表示該 IPv6 位址的前多少位元是用於識別網路的「網路部分」。
- 例如:
2001:db8:1234:5678::/64- 這表示前64位元 (
2001:db8:1234:5678) 是網路前綴,用於識別該位址所在的子網路。 - 後64位元是用於識別該子網路內特定設備的介面識別碼。
- 這表示前64位元 (
- 在區域網路(LAN)中,/64 是最常見也是官方推薦的前綴長度,它為單一子網路提供了廣闊的位址空間(2⁶⁴個位址),並確保 SLAAC 等自動化功能正常運作。
2.1 The Role of the Network Prefix
In IPv6, the concept analogous to IPv4’s subnet mask is represented by the “network prefix” and its length. The prefix length is denoted by a forward slash / followed by a number (0-128), indicating how many of the leading bits of the address constitute the “network portion.”
- For example:
2001:db8:1234:5678::/64- This signifies that the first 64 bits (
2001:db8:1234:5678) are the network prefix, used to identify the subnet the address belongs to. - The remaining 64 bits are the Interface Identifier, used to identify a specific device within that subnet.
- This signifies that the first 64 bits (
- On a Local Area Network (LAN), /64 is the most common and officially recommended prefix length. It provides an immense address space (2⁶⁴ addresses) for a single subnet and ensures that autoconfiguration features like SLAAC function correctly.
2.2 介面識別碼 (Interface Identifier, IID)
介面識別碼(IID)是 IPv6 位址的後64位元,用於在一個子網路內唯一標識一台設備的網路介面。其產生方式主要有兩種:
- EUI-64 格式: 根據網路介面的48位元 MAC 位址自動生成。這使得 IID 全球唯一,但也可能洩漏設備資訊,引發隱私擔憂。
- 隱私擴展 (Privacy Extensions): 作業系統會為對外連線產生一個隨機、臨時的 IID。這個位址會定期變更,從而防止外部追蹤者根據固定的 IP 位址來追蹤用戶活動。
2.2 Interface Identifier (IID)
The Interface Identifier (IID) constitutes the latter 64 bits of an IPv6 address and uniquely identifies a device’s network interface within a subnet. There are two primary methods for its generation:
- EUI-64 Format: Automatically generated from the network interface’s 48-bit MAC address. This makes the IID globally unique but can also leak device information, raising privacy concerns.
- Privacy Extensions: The operating system generates a random, temporary IID for outbound connections. This address changes periodically to prevent external parties from tracking user activity based on a fixed IP address.
第三章:IPv6 位址分配方式詳解
Chapter 3: Detailed IPv6 Address Allocation Methods
IPv6 address allocation is highly flexible, covering a range of modes from fully manual to highly automated.
3.1 靜態分配 (Static Allocation)
網路管理員為裝置手動設定固定的 IPv6 位址、網路前綴長度和預設閘道。
- 特點: 位址固定不變、可預測性高、易於管理和追蹤。
- 適用場景: 網路中的關鍵基礎設施,如伺服器(Web、DNS 伺服器)、路由器、交換機等。
3.1 Static Allocation
The network administrator manually configures a fixed IPv6 address, network prefix length, and default gateway for a device.
- Characteristics: The address is fixed, predictable, and easy to manage and track.
- Use Case: Critical network infrastructure like servers (Web, DNS), routers, and switches.
3.2 無狀態位址自動配置 (SLAAC)
裝置透過鄰居發現協定(NDP)中的「路由器公告(RA)」訊息從路由器取得網路前綴,然後結合自行產生的介面識別碼(IID)來組成位址。
- 特點: 高度自動化、去中心化、即插即用。傳統 SLAAC 無法提供 DNS 伺服器等額外設定資訊。
- 適用場景: 絕大多數用戶端網路,如家庭網路、公共 Wi-Fi、物聯網(IoT)。
3.2 Stateless Address Autoconfiguration (SLAAC)
A device obtains the network prefix from a router via a “Router Advertisement (RA)” message from the Neighbor Discovery Protocol (NDP), then combines it with a self-generated Interface Identifier (IID) to form an address.
- Characteristics: Highly automated, decentralized, and plug-and-play. Traditional SLAAC cannot provide additional configuration information like DNS servers.
- Use Case: The vast majority of client networks, such as home networks, public Wi-Fi, and IoT.
3.3 有狀態位址自動配置 (DHCPv6)
此方式由 DHCPv6 伺服器集中管理和分配 IPv6 位址及所有網路設定。路由器的 RA 訊息中包含一個 M 旗標 (Managed Flag)。若 M=1,則表示裝置必須透過 DHCPv6 來取得位址。
- 特點: 集中式管理,可精確控制位址的分配。伺服器會記錄每個位址的分配狀態(「有狀態」)。
- 適用場景: 需要嚴格控管 IP 位址分配的企業網路。
3.3 Stateful Autoconfiguration (DHCPv6)
In this method, a DHCPv6 server centrally manages and assigns IPv6 addresses and all network settings. The router’s RA message contains an M (Managed) flag. If M=1, it instructs devices that they must obtain an address via DHCPv6.
- Characteristics: Centralized management with precise control over address allocation. The server maintains the state (“stateful”) of each assigned address.
- Use Case: Enterprise networks that require strict control over IP address assignment.
3.4 混合模式:無狀態 DHCPv6
此模式結合了 SLAAC 和 DHCPv6 的優點。裝置使用 SLAAC 自行產生位址,同時向 DHCPv6 伺服器請求 DNS 等額外資訊。
- 運作原理: 路由器的 RA 訊息會設定 M 旗標 為 0(不由 DHCPv6 管理位址),同時設定 O 旗標 (Other Configuration Flag) 為 1(透過 DHCPv6 取得其他設定)。
- 實踐優勢: DHCPv6 伺服器無需追蹤 IP 位址的狀態,故稱「無狀態 DHCPv6」。此模式是功能與效率的最佳平衡點。
3.4 Hybrid Mode: Stateless DHCPv6
This mode combines the benefits of SLAAC and DHCPv6. A device generates its own address using SLAAC while requesting additional information, like DNS servers, from a DHCPv6 server.
- How it works: The key is that the router’s RA message sets the M flag to 0 (address is not managed by DHCPv6) and the O (Other Configuration) flag to 1 (get other settings from DHCPv6).
- Practical Advantage: The DHCPv6 server does not need to track the state of IP addresses, hence the name “Stateless DHCPv6.” This mode offers the best balance of functionality and efficiency.
第四章:實踐案例:在 Ubuntu 上配置混合 IPv6 網路
Chapter 4: Practical Example: Configuring a Hybrid IPv6 Network on Ubuntu
4.1 場景設定與子網路劃分
- 給定位址區塊: 假設您從 ISP 獲得位址 2404:c804:2325:9a10::/60。
- 子網路劃分: /60 的前綴長度表示您擁有 64 – 60 = 4 位元可用於劃分子網路,即 2⁴ = 16 個 /64 子網路。
- 本案例選用子網路: 我們選用第一個子網路 2404:c804:2325:9a10::/64 用於區域網路。
- 伺服器角色: 一台 Ubuntu 伺服器同時扮演路由器和無狀態 DHCPv6 伺服器。LAN 介面為 ens192。
4.1 Scenario Setup and Subnetting
- Given Address Block: Assume you have been allocated the address block
2404:c804:2325:9a10::/60from your ISP. - Subnetting: A /60 prefix length gives you 64 – 60 = 4 bits for subnetting, which allows for 2⁴ = 16 separate /64 subnets.
- Subnet for this Example: We will use the first subnet,
2404:c804:2325:9a10::/64, for our LAN. - Server Role: An Ubuntu server will act as both a router and a Stateless DHCPv6 server. Its LAN interface is ens192.
4.2 步驟一:配置 Ubuntu 伺服器靜態 IP
編輯 Netplan 設定檔(/etc/netplan/ 目錄下的 YAML 檔案)為 LAN 介面指派靜態 IP。codeYaml
network:
version: 2
ethernets:
ens192:
accept-ra: no
addresses:
- 2404:c804:2325:9a10::1/64
執行 sudo netplan apply 使設定生效。
4.2 Step 1: Configure a Static IP on the Ubuntu Server
Edit the Netplan configuration file (the YAML file in /etc/netplan/) to assign a static IP to the LAN interface.codeYaml
network:
version: 2
ethernets:
ens192:
accept-ra: no
addresses:
- 2404:c804:2325:9a10::1/64
Run sudo netplan apply to apply the changes.
4.3 步驟二:配置路由器 (radvd)
安裝並設定 radvd (Router Advertisement Daemon) 來發送 RA 訊息。codeBash
sudo apt update
sudo apt install radvd
編輯設定檔 /etc/radvd.conf:codeConf
interface ens192
{
AdvSendAdvert on;
// 設定 M 旗標為 0 (SLAAC)
AdvManagedFlag off;
// 設定 O 旗標為 1 (請求額外設定)
AdvOtherConfigFlag on;
// 宣告網路前綴
prefix 2404:c804:2325:9a10::/64
{
AdvOnLink on;
AdvAutonomous on;
AdvRouterAddress on;
};
};
啟動並啟用 radvd 服務:sudo systemctl enable –now radvd。
4.3 Step 2: Configure the Router (radvd)
Install and configure radvd (Router Advertisement Daemon) to send RA messages.codeBash
sudo apt update
sudo apt install radvd
Edit the configuration file /etc/radvd.conf:codeConf
interface ens192
{
AdvSendAdvert on;
// Set M flag to 0 (for SLAAC)
AdvManagedFlag off;
// Set O flag to 1 (request other info)
AdvOtherConfigFlag on;
// Announce the network prefix
prefix 2404:c804:2325:9a10::/64
{
AdvOnLink on;
AdvAutonomous on;
AdvRouterAddress on;
};
};
Start and enable the radvd service: sudo systemctl enable –now radvd.
4.4 步驟三:配置無狀態 DHCPv6 伺服器
安裝 isc-dhcp-server。codeBash
sudo apt install isc-dhcp-server
編輯 DHCPv6 設定檔 /etc/dhcp/dhcpd6.conf 來提供 DNS 資訊:codeDhcpd
authoritative;
# 提供 Google 的公共 DNS
option dhcp6.name-servers 2001:4860:4860::8888, 2001:4860:4860::8844;
# 定義子網路,但內部無需 range6
subnet6 2404:c804:2325:9a10::/64 {
}
編輯 /etc/default/isc-dhcp-server,指定監聽的介面: INTERFACESv6=”ens192″
啟動並啟用服務:sudo systemctl enable –now isc-dhcp-server。
4.4 Step 3: Configure the Stateless DHCPv6 Server
Install isc-dhcp-server.codeBash
sudo apt install isc-dhcp-server
Edit the DHCPv6 configuration file /etc/dhcp/dhcpd6.conf to provide DNS information:codeDhcpd
authoritative;
# Provide Google's Public DNS servers
option dhcp6.name-servers 2001:4860:4860::8888, 2001:4860:4860::8844;
# Define the subnet, but no range6 is needed for stateless mode
subnet6 2404:c804:2325:9a10::/64 {
}
Edit /etc/default/isc-dhcp-server to specify the listening interface: INTERFACESv6=”ens192″
Start and enable the service: sudo systemctl enable –now isc-dhcp-server.
4.5 步驟四:用戶端驗證
在接入此網路的用戶端電腦上,它應該會透過 SLAAC 自動設定一個 2404:c804:2325:9a10:…. 的 IP 位址,並透過無狀態 DHCPv6 取得 DNS 設定。
- 查看 IP 位址: ip -6 address
- 查看 DNS 設定: cat /etc/resolv.conf
4.5 Step 4: Client Verification
On a client machine connected to this network, it should automatically configure an IP address starting with 2404:c804:2325:9a10:.... via SLAAC and receive the DNS settings via Stateless DHCPv6.
- Check IP address:
ip -6 address - Check DNS settings:
cat /etc/resolv.conf