linux 命令route add default dev eth0和route add default gw eth0的区别?[通俗易懂]

2022-09-22 18:48:54 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。 本机以太网卡eth0的IP地址为手动配置: 网关IP地址为192.168.1.1/24 #ifconfig eth0 192.168.1.100 netmask 255.255.255.0 #route add default dev eth0 //默认路由,将去往未知网络的数据包全部从接口eth0发出去 测试结果为ping外网失败; [root@localhost ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 eth0 [root@localhost ~]# ping www.baidu.com -c 5 PING www.a.shifen.com (61.135.169.105) 56(84) bytes of data. From localhost (192.168.1.110) icmp_seq=2 Destination Host Unreachable From localhost (192.168.1.110) icmp_seq=3 Destination Host Unreachable From localhost (192.168.1.110) icmp_seq=4 Destination Host Unreachable From localhost (192.168.1.110) icmp_seq=5 Destination Host Unreachable — www.a.shifen.com ping statistics — 5 packets transmitted, 0 received, 4 errors, 100% packet loss, time 4001ms pipe 3 但是,如果我不写默认路由出接口,而是直接下一跳,却可以ping通外网! 如下: #route del default dev eth0 //删掉刚才配的静态路由 #route add default gw 192.168.1.1 //写默认路由的下一跳地址 现在ping外网却通了! [root@localhost ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 [root@localhost ~]# ping www.baidu.com -c 5 PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data. 64 bytes from 61.135.169.125: icmp_req=1 ttl=51 time=305 ms 64 bytes from 61.135.169.125: icmp_req=2 ttl=51 time=145 ms 64 bytes from 61.135.169.125: icmp_req=3 ttl=51 time=98.3 ms 64 bytes from 61.135.169.125: icmp_req=4 ttl=51 time=75.5 ms 64 bytes from 61.135.169.125: icmp_req=5 ttl=51 time=342 ms — www.a.shifen.com ping statistics — 5 packets transmitted, 5 received, 0% packet loss, time 4005ms rtt min/avg/max/mdev = 75.570/193.664/342.982/109.636 ms ======================================================== 那么为什么 route …dev eth0不行?而route …gw eth0却可以呢? 原理上这两条命令应该是等价的阿!

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/169850.html原文链接:https://javaforall.cn

0 人点赞