如何理解route的输出

如何理解route的输出

说我正在配置Ubuntu系统的网络。所以我可以编辑/etc/network/interfaces如下:

...
address x.x.x.x
netmask x.x.x.x
gateway 192.168.1.1

然后我执行命令route,可以得到这样的输出:

default       gateway    genmask
192.168.1.0   0.0.0.0    255.255.255.0

我不知道我该如何理解192.168.1.00.0.0.0并且255.255.255.0

答案1

一个简单的例子可能会有所帮助。假设此路由表条目(来自 Linux 下的“route -an”命令输出,并非显示所有列,仅显示我们需要的列 - 这是“获胜”条目,其他条目未显示):

Destination   Gateway  Genmask          Iface
192.168.69.0  0.0.0.0  255.255.255.0    eth7
.... other entries not shown ....

现在假设我们正在路由一个目标 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).

匹配条目的网关字段是0.0.0.0,意思是“没有网关,不需要”。这意味着与此条目匹配的 IP 可直接用于该主机,并且可以使用指定的接口“eth7”进行访问。将 192.168.69.25 的 ARP 发送到 eth7 连接的网络上;应该会收到包含该主机 MAC 的回复。将数据包发送到该 MAC 地址。

如果网关不是 0.0.0.0,则它应该是在 eth7 网络上找到的设备的 IP。从 eth7 发送网关的 ARP 并获取回复。将数据包发送到该 ARP。这通常是一个路由器,它将接收数据包并将其转发到适当的下一跳网络。

这是一个简短而有趣的示例 - 您可以通过一些搜索找到更多详细信息......

答案2

路线手册页对此进行了很好的解释:

OUTPUT
   The output of the kernel routing table is organized in the following columns

   Destination
          The destination network or destination host.

   Gateway
          The gateway address or '*' if none set.

   Genmask
          The netmask for the destination net; '255.255.255.255' for a host destination and '0.0.0.0' for the default route.

因此,192.168.1.0 是您的目标网络,0.0.0.0 是您的网关,255.255.255.0 是您的网络掩码。
如果您不确定为什么您的网关是 0.0.0.0这个答案很好地解释了这一点

相关内容