说我正在配置Ubuntu系统的网络。所以我可以编辑/etc/network/interfaces
如下:
...
address x.x.x.x
netmask x.x.x.x
gateway 192.168.1.1
Run Code Online (Sandbox Code Playgroud)
然后我执行命令route
,可以得到这样的输出:
default gateway genmask
192.168.1.0 0.0.0.0 255.255.255.0
Run Code Online (Sandbox Code Playgroud)
我不知道我该如何理解192.168.1.0
,0.0.0.0
并且255.255.255.0
。
小智 9
一个简单的例子可能会有所帮助。假设此路由表条目(来自 Linux 下的“route -an”命令输出,并非显示所有列,仅显示我们需要的列 - 这是“获胜”条目,其他条目未显示):
Destination Gateway Genmask Iface
192.168.69.0 0.0.0.0 255.255.255.0 eth7
.... other entries not shown ....
Run Code Online (Sandbox Code Playgroud)
现在假设我们正在路由一个目标 IP 为 192.168.69.25 的出站数据包。这些是发生的步骤。(理论上)它们会出现在路由表中的每个条目中;我们只显示“获胜”条目。
Extract destination IP: 192.168.69.25
AND it with the Genmask: 192.168.69.25 AND 255.255.255.0 ==> 192.168.69.0
Match the result against the 'Destination' field of the entry.
They match: 192.168.69.0 matches 92.168.69.0
The entry matches (and we presume it's the 'best' match, for simplicity).
Run Code Online (Sandbox Code Playgroud)
匹配条目的网关字段是0.0.0.0,意思是“没有网关,不需要”。这意味着与此条目匹配的 IP 可直接用于该主机,并且可以使用指定的接口“eth7”进行访问。将 192.168.69.25 的 ARP 发送到 eth7 连接的网络上;应该会收到包含该主机 MAC 的回复。将数据包发送到该 MAC 地址。
如果网关不是 0.0.0.0,则它应该是在 eth7 网络上找到的设备的 IP。从 eth7 发送网关的 ARP 并获取回复。将数据包发送到该 ARP。这通常是一个路由器,它将接收数据包并将其转发到适当的下一跳网络。
这是一个简短而有趣的示例 - 您可以通过一些搜索找到更多详细信息......