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

linux 命令route add default dev eth0和route add default gw eth0的区别?[通俗易懂]本机以太网卡eth0的IP地址为手动配置:网关IP地址为192.168.1.1/24#ifconfigeth0192.168.1.100netmask255.255.255.0  #routeadddefaultdeveth0           //默认路由,将去往未知网络的数据包全部从接口eth0发出去测试结果为ping外网失败;[root@localh

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
本机以太网卡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.net/169850.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号