root@OpenWrt:/# ping zhihu.com
PING zhihu.com (103.41.167.234): 56 data bytes
ping: sendto: Network unreachable
- 这个错误一般是由于网关配置错误导致的
- 通过
route
查看路由表
root@OpenWrt:/# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.17.0.0 * 255.255.0.0 U 0 0 0 docker0
192.168.11.0 * 255.255.255.0 U 1 0 0 eth1
192.168.12.0 * 255.255.255.0 U 1 0 0 eth2
192.168.233.0 * 255.255.255.0 U 0 0 0 br-lan
- 可以看到Gateway一栏为空,也就是没有配置网关,路由不知将数据转发到哪个网口。解决办法是增加一条默认网关。添加命令:
route add default gw XXX.XXX.XXX.XXX
,XXX.XXX.XXX.XXX为你的上级网关,一般为光猫的IP。添加完成后通过 route
查看路由表是否生效
route add default gw 192.168.11.1
root@OpenWrt:/# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.11.1 0.0.0.0 UG 0 0 0 eth1
172.17.0.0 * 255.255.0.0 U 0 0 0 docker0
192.168.11.0 * 255.255.255.0 U 1 0 0 eth1
192.168.12.0 * 255.255.255.0 U 1 0 0 eth2
192.168.233.0 * 255.255.255.0 U 0 0 0 br-lan
root@OpenWrt:/#
- 可以看到默认网关default已被添加进路由表,ping外网成功
-
root@OpenWrt:/# ping bilibili.com
PING bilibili.com (110.43.34.66): 56 data bytes
64 bytes from 110.43.34.66: seq=0 ttl=51 time=27.996 ms
64 bytes from 110.43.34.66: seq=1 ttl=51 time=18.906 ms
64 bytes from 110.43.34.66: seq=2 ttl=51 time=19.691 ms
64 bytes from 110.43.34.66: seq=3 ttl=51 time=19.945 ms
64 bytes from 110.43.34.66: seq=4 ttl=51 time=19.169 ms
64 bytes from 110.43.34.66: seq=5 ttl=51 time=22.988 ms
64 bytes from 110.43.34.66: seq=6 ttl=51 time=19.152 ms
^C
--- bilibili.com ping statistics ---
7 packets transmitted, 7 packets received, 0% packet loss
round-trip min/avg/max = 18.906/21.121/27.996 ms
root@OpenWrt:/#