使用 arp 和 traceroute 验证网络配置
在最后一步中,你将学习使用 arp 和 traceroute 来检查你的网络配置。
首先,让我们确保必要的工具已安装。arp 在 net-tools 中,而 traceroute 在其自己的包中。
-
安装 net-tools 和 traceroute:
sudo apt-get update
sudo apt-get install -y net-tools traceroute
使用 arp 查看 ARP 缓存
地址解析协议 (ARP) 在本地网络上将 IP 地址映射到物理 MAC 地址。结果存储在 ARP 缓存中。
-
为了填充你的 ARP 缓存,首先从路由表中找到你的默认网关的 IP。
ip route | grep default
输出将显示你的网关 IP。请注意,IP 地址会有所不同。
default via 172.16.50.253 dev eth0
default via 172.16.50.253 dev eth0 proto dhcp src 172.16.50.11 metric 100
-
现在,ping 该网关 IP(例如,上面示例中的 172.16.50.253)以生成流量。
ping -c 1 <your-gateway-ip>
-
使用 arp -a 显示 ARP 缓存。
arp -a
你将看到网关的 IP 和 MAC 地址。你可能还会看到我们之前尝试 ping 的 192.168.1.1 地址的 <incomplete> 条目,这是正常的。
? (192.168.1.1) at <incomplete> on eth0
_gateway (172.16.50.253) at ee:ff:ff:ff:ff:ff [ether] on eth0
这证实了你的系统已将网关的 IP 解析为 MAC 地址。
使用 traceroute 跟踪网络路径
traceroute 命令显示数据包到达目的地所经过的路由器序列(“跃点”)。
-
让我们跟踪到像 google.com 这样的公共域名的路由。
traceroute google.com
该命令将打印路径。注意: 在许多云环境中,traceroute 显示的第一个跃点可能是内部 IP 地址,而不是路由表中的默认网关。这是正常行为。
traceroute to google.com (142.250.189.174), 30 hops max, 60 byte packets
1 10.220.9.2 (10.220.9.2) 0.345 ms ...
2 11.73.5.1 (11.73.5.1) 1.978 ms ...
3 ...
8 sfo03s24-in-f14.1e100.net (142.250.189.174) 3.001 ms ...
- 第一个跃点是实验提供商网络内的路由器。
- 后续跃点显示了跨越互联网到最终目的地的路径。
- 如果路径上的路由器不响应
traceroute 探测,可能会出现星号(* * *),这可能是由于防火墙或其他网络策略。
恭喜!你现在已经学会了如何在 Linux 系统上检查、静态配置和动态配置 IP 地址,以及使用 ping、arp 和 traceroute 等关键工具来验证和排查你的网络。