0Pricing
Linux Networking & TCP/IP for Developers · 课时

深入理解子网划分与 CIDR

掌握可变长度子网掩码和 CIDR 表示法,以规划地址空间、计算范围并高效聚合路由。

深入理解子网划分与 CIDR 是 CoddyKit 上的免费 Linux Networking & TCP/IP for Developers 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Networking & TCP/IP for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Subnetting Matters

Subnetting splits a large network into smaller, manageable segments. It improves security, performance, and address efficiency.

CIDR (Classless Inter-Domain Routing) replaced rigid class A/B/C boundaries with flexible prefix lengths.

CIDR Notation

A CIDR block is written as address/prefix. The prefix counts the number of leading network bits.

  • 192.168.1.0/24 — 24 network bits, 8 host bits
  • 10.0.0.0/8 — 8 network bits, 24 host bits

The Subnet Mask

A prefix maps directly to a dotted-decimal mask.

  • /24 = 255.255.255.0
  • /26 = 255.255.255.192
  • /30 = 255.255.255.252

Each set bit in the mask is a network bit.

Counting Hosts

Host count = 2^(host bits) minus 2 (network and broadcast addresses).

  • /24 -> 2^8 - 2 = 254 hosts
  • /26 -> 2^6 - 2 = 62 hosts
  • /30 -> 2^2 - 2 = 2 hosts (perfect for point-to-point links)

Finding the Network Address

Bitwise-AND the IP with the mask to get the network address. The example below computes it in Python.

import ipaddress
net = ipaddress.ip_network('192.168.5.130/26', strict=False)
print('network:', net.network_address)
print('broadcast:', net.broadcast_address)

Determining the Range

For 192.168.5.130/26: block size is 64, so subnets start at .0, .64, .128, .192.

The .130 address falls in the 192.168.5.128/26 subnet (usable .129 to .190, broadcast .191).

Variable-Length Subnet Masking (VLSM)

VLSM lets different subnets use different prefix lengths from one block, matching size to need.

Allocate largest subnets first to avoid fragmentation and wasted address space.

Route Aggregation (Supernetting)

Adjacent subnets can be summarized into one larger prefix, shrinking routing tables.

  • 192.168.0.0/24 + 192.168.1.0/24 -> 192.168.0.0/23

Aggregation only works when blocks are contiguous and aligned.

Subnetting with Python

The ipaddress module can split a network into smaller subnets automatically.

import ipaddress
big = ipaddress.ip_network('10.0.0.0/24')
for sub in big.subnets(new_prefix=26):
    print(sub)

Checking Membership

Verify whether an address belongs to a network — essential for ACLs and routing decisions.

import ipaddress
addr = ipaddress.ip_address('10.0.0.45')
net = ipaddress.ip_network('10.0.0.0/26')
print(addr in net)

Common Pitfalls

Watch out for:

  • Forgetting to subtract network and broadcast addresses
  • Misaligned aggregation that overlaps other blocks
  • Using /31 links (valid per RFC 3021 for point-to-point) without device support

Quick Check

Test your subnetting math.

Recap

You can now plan address space confidently:

  • Read and convert CIDR prefixes and masks
  • Calculate network, broadcast, and host ranges
  • Apply VLSM to size subnets to need
  • Aggregate contiguous blocks to shrink routing tables
  • Use Python's ipaddress module for verification

This underpins the IPv6, routing, and NAT topics in this course.

常见问题解答

「深入理解子网划分与 CIDR」课时是免费的吗?

是的 — 「深入理解子网划分与 CIDR」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Networking & TCP/IP for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

「深入理解子网划分与 CIDR」这节课中我会学到什么?

掌握可变长度子网掩码和 CIDR 表示法,以规划地址空间、计算范围并高效聚合路由。 你通过在浏览器中直接运行的动手代码来练习 Linux Networking & TCP/IP for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Networking & TCP/IP for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Networking & TCP/IP for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「深入理解子网划分与 CIDR」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Linux Networking & TCP/IP for Developers 课中编写并运行代码吗?

能。每节 Linux Networking & TCP/IP for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. IPv6 编址与概念
  2. 高级路由协议(RIP/OSPF)
  3. 网络地址转换(NAT)
  4. 深入理解子网划分与 CIDR
← 返回 Linux Networking & TCP/IP for Developers