CIDR 和 IP 地址数

CIDR and number of ip-addresses

我读到 10.240.0.0/24 最多可以托管 254 个 IP 地址。怎么样?
我如何直观地理解 /24 在这里做什么来帮助提供 254 个唯一的 ip 地址?

TL;博士;

计算主机数量的一种简便方法是

2 ^ ( 32 - 24 ) - 2 = 256

因为:

  1. 我们正在做位操作(01,两个可能的值)
  2. IP 是一组 4 个八位字节,当一个八位字节为 8 位时 (4 * 8 = 32)
  3. 24 是您的 CIDR
  4. 每个子网上都有两个保留IP,广播地址和子网0,它们不应该用于主机

CIDR 是在按位运算的帮助下计算的。

一个 IP 是一组 4 个八位字节,每个字节用一个点分隔。

255.255.255.255
=
11111111.11111111.11111111.11111111

当您指定 /24 的 CIDR 时,您正在为您的 IP 请求一个子网,其中包含一个掩码,该掩码将填充 24 位设置为 1

11111111.11111111.11111111.00000000
=
255.255.255.0

您的IP是

10.240.0.0
=
00001010.11110000.00000000.00000000

现在我们可以在您的 IP 和您的子网之间应用按位与运算

11111111.11111111.11111111.00000000
&
00001010.11110000.00000000.00000000
=
00001010.11110000.00000000.00000000

所以你最终得到 10.240.0.0 作为你的 IP 前缀。
相同的子网可以应用于后续 IP

10.240.0.1

11111111.11111111.11111111.00000000
&
00001010.11110000.00000000.00000001
=
00001010.11110000.00000000.00000000

提供相同的 10.240.0.0 IP 前缀

10.240.0.2

11111111.11111111.11111111.00000000
&
00001010.11110000.00000000.00000010
=
00001010.11110000.00000000.00000000

提供相同的 10.240.0.0 IP 前缀
等等等等

总而言之,按位运算非常简单:

  • 每次你有一个 0 & x 它将等于 0
  • 每次你有一个 1 & x 它将等于 x

所以这意味着使用 10.240.0.0/24,您有一个 255.255.255.0 的子网,因此 IP 范围从 10.240.0.010.240.0.255

这仍然为您提供了 256 个可能的地址?

嗯,是的,但你必须记住,在 IPv4 中,你有两个不可用的地址:

  • 子网零(您范围的第一个地址)
  • 和广播地址(您范围内的最后一个地址)

Special Addresses:

From the Assigned Numbers memo [Reynolds, J., and J. Postel, "Assigned Numbers", RFC-943, USC/Information Sciences Institute, April 1985.]:

"In certain contexts, it is useful to have fixed addresses with functional significance rather than as identifiers of specific hosts. When such usage is called for, the address zero is to be interpreted as meaning "this", as in "this network". The address of all ones are to be interpreted as meaning "all", as in "all hosts". For example, the address 128.9.255.255 could be interpreted as meaning all hosts on the network 128.9. Or, the address 0.0.0.37 could be interpreted as meaning host 37 on this network."

It is useful to preserve and extend the interpretation of these special addresses in subnetted networks. This means the values of all zeros and all ones in the subnet field should not be assigned to actual (physical) subnets.

来源: https://www.ietf.org/rfc/rfc950.txt

所以现在,如果您 256 - 2,您就有 254 个可用的主机。

总结一下:

  • CIDR:10.240.0.0/24
  • 子网掩码:255.255.255.0(当 IP 显示为八位字节组时 1 的 24 次)
  • IP 范围:10.240.0.0 - 10.240.0.255
  • 子网零:10.240.0.0
  • 广播地址:10.240.0.255
  • 主机 IP 范围:10.240.0.1 - 10.240.0.254