如何理解ip路由和操作linux的路由表

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 同时在freebsd上使用netstat -nr打印路由表,也顺便提示一下。

现在问题在如何操作路由表,因为ip的选路全靠它吃饭了,怎么为一个特定的网络或者主机选择一条特定的路由?如何删除路由?这些问题对于网络维护和系统管理也尤为重要:

还是以上面的路由表为例,建议使用前先man route查看方法,或许各个系统略有差异,下面是以debian为例,由于是测试,所以使用sudo,而不是root权限:

1、为某主机添加路由 $ sudo route add ?host 192.168.10.58 dev eth1 //所有通向192.168.10.58的数据都是用eth1网卡 $ sudo route add ?host 192.168.11.58 gw 192.168.10.1 //通向192.168.11.58的数据使用网关192.168.10.1

不经意间做了一个验证实验:由于eth1是没有启动的,所以添加这个路由后,192.168.10.58不可ping通 初始路由表 sudo route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.100.0 * 255.255.255.0 U 0 0 0 eth1 192.168.10.0 * 255.255.254.0 U 0 0 0 eth0 default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0 —————————————————————————-

可以ping通192.168.10.58 $ ping 192.168.10.58 PING 192.168.10.58 (192.168.10.58) 56(84) bytes of data. 64 bytes from 192.168.10.58: icmp_seq=1 ttl=64 time=0.188 ms —————————————————————————–

添加192.168.10.58的路由 $ sudo route add -host 192.168.10.58 dev eth1 $ sudo route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.10.58 * 255.255.255.255 UH 0 0 0 eth1 192.168.100.0 * 255.255.255.0 U 0 0 0 eth1 192.168.10.0 * 255.255.254.0 U 0 0 0 eth0 default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0 —————————————————————————-

由于eth0无法使用了,所以发给eth1,就等于丢入黑洞之中 $ ping 192.168.10.58 PING 192.168.10.58 (192.168.10.58) 56(84) bytes of data. — 192.168.10.58 ping statistics — 2 packets transmitted, 0 received, 100% packet loss, time 1010ms —————————————————————————-

指定给网关处理 $ sudo route add -host 192.168.10.58 gw 192.168.10.1 $ sudo route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.10.58 192.168.10.1 255.255.255.255 UGH 0 0 0 eth0 192.168.10.58 * 255.255.255.255 UH 0 0 0 eth1 192.168.100.0 * 255.255.255.0 U 0 0 0 eth1 192.168.10.0 * 255.255.254.0 U 0 0 0 eth0 default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0 —————————————————————————-

要经过网关了,验证了路由表的作用 $ ping 192.168.10.58 PING 192.168.10.58 (192.168.10.58) 56(84) bytes of data. From 192.168.10.1: icmp_seq=1 Redirect Network(New nexthop: 192.168.10.58) 64 bytes from 192.168.10.58: icmp_seq=1 ttl=64 time=1.02 ms —————————————————————————-

废话少说了,同理为某网路添加路由和删除路由如下所示:经过测试有效:

[1][2][3][4]

当你能飞的时候就不要放弃飞

如何理解ip路由和操作linux的路由表

相关文章:

你感兴趣的文章:

标签云: