route命令使用总结
b.网络路由:即去往某一网络或网段的路由 一般多网段之间互相通信,希望建立一条优先路由,而不是通过默认网关时就可以配置网络路由。还是拿房子比喻,你现在不是要出门,而是卧室,卫生间,去卧室就要经过卧室的门,去卫生间也要经过卫生间的门,这里的卧室和卫生间的门就可以认为是去往某一网段的路由,而不是默认路由(即房子的门。) 实际工作中会有需求,两个不同的内部网络之间互访,而不是出网访问,就是上面例子的情况。 本题的答案: route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1 解答实践: [root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1 SIOCADDRT: 网络不可达 #==>当连不通地址192.168.1.1时,无法添加路由。 [root@oldboy ~]# ifconfig eth0:0 192.168.1.1/24 up#==>添加一个IP别名用于临时测试,如果永久生效最好加双网卡或写入到配置文件。 [root@oldboy ~]# ifconfig eth0:0 #==>查看添加的IP别名(网络里把这种多IP的方式称为子接口) eth0:0 Link encap:Ethernet HWaddr 00:0C:29:65:A4:FD inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 再来添加去192.168.1.0的数据包,交给192.168.1.1处理。 [root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1 [root@oldboy ~]# netstat -rn #==>和route -n很像。 Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0 #==>这就是网络路由 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0 拓展:其他写法 [root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0 #==>指定设备而不是地址。 [root@oldboy ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0 [root@oldboy ~]# route del -net 192.168.1.0/24 dev eth0 [root@oldboy ~]# route add -net 192.168.1.0/24 dev eth0 [root@oldboy ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0 总结: route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1 route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0 route add -net 192.168.1.0/24 dev eth0 route del -net 192.168.1.0/24 dev eth0 (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |