问题记录
主机通过共享网络给以太网口想让正点原子的阿尔法Linux开发板连上网,网络配置过程如下:
开发板连接的是eth1口,通过在终端输入以下命令进入网络配置文件。
vi /etc/network/interfaces
将其配置为了以下地址
但是出现了一些问题,当我让开发板ping主机和Ubuntu虚拟机的网络地址时可以正常ping通,但是无法ping通百度和8.8.8.8,报错如下图所示:
root@ATK-IMX6U:~# ifdown eth1 && ifup eth1
[ 718.367006] IPv6: ADDRCONF(NETDEV_UP): eth1: link is not ready
[ 718.473845] fec 2188000.ethernet eth1: Freescale FEC PHY driver [Generic PHY] (mii_bus:phy_addr=20b4000.ethernet:02, irq=-1)
root@ATK-IMX6U:~# [ 720.529332] IPv6: ADDRCONF(NETDEV_UP): eth1: link is not ready
[ 722.474234] fec 2188000.ethernet eth1: Link is Up - 100Mbps/Full - flow control rx/tx
[ 722.482120] IPv6: ADDRCONF(NETDEV_CHANGE): eth1: link becomes readyroot@ATK-IMX6U:~# ping 192.168.137.1
PING 192.168.137.1 (192.168.137.1) 56(84) bytes of data.
64 bytes from 192.168.137.1: icmp_seq=1 ttl=128 time=0.593 ms
64 bytes from 192.168.137.1: icmp_seq=2 ttl=128 time=0.657 ms
64 bytes from 192.168.137.1: icmp_seq=3 ttl=128 time=0.423 ms
64 bytes from 192.168.137.1: icmp_seq=4 ttl=128 time=0.498 ms
64 bytes from 192.168.137.1: icmp_seq=5 ttl=128 time=0.530 ms
^C
--- 192.168.137.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3996ms
rtt min/avg/max/mdev = 0.423/0.540/0.657/0.081 ms
root@ATK-IMX6U:~# ping 192.168.137.4
PING 192.168.137.4 (192.168.137.4) 56(84) bytes of data.
From 192.168.137.1: icmp_seq=1 Redirect Network(New nexthop: 192.168.137.4)
64 bytes from 192.168.137.4: icmp_seq=1 ttl=63 time=0.791 ms
64 bytes from 192.168.137.4: icmp_seq=1 ttl=64 time=0.817 ms (DUP!)
From 192.168.137.1: icmp_seq=2 Redirect Network(New nexthop: 192.168.137.4)
64 bytes from 192.168.137.4: icmp_seq=2 ttl=63 time=0.420 ms
64 bytes from 192.168.137.4: icmp_seq=2 ttl=64 time=0.447 ms (DUP!)
64 bytes from 192.168.137.4: icmp_seq=2 ttl=63 time=0.464 ms (DUP!)
64 bytes from 192.168.137.4: icmp_seq=2 ttl=64 time=0.482 ms (DUP!)
From 192.168.137.1: icmp_seq=3 Redirect Network(New nexthop: 192.168.137.4)
64 bytes from 192.168.137.4: icmp_seq=3 ttl=63 time=0.364 ms
64 bytes from 192.168.137.4: icmp_seq=3 ttl=64 time=0.389 ms (DUP!)
64 bytes from 192.168.137.4: icmp_seq=3 ttl=63 time=0.407 ms (DUP!)
64 bytes from 192.168.137.4: icmp_seq=3 ttl=64 time=0.426 ms (DUP!)
^HFrom 192.168.137.1: icmp_seq=4 Redirect Network(New nexthop: 192.168.137.4)
64 bytes from 192.168.137.4: icmp_seq=4 ttl=63 time=0.376 ms
64 bytes from 192.168.137.4: icmp_seq=4 ttl=64 time=0.402 ms (DUP!)
64 bytes from 192.168.137.4: icmp_seq=4 ttl=63 time=0.420 ms (DUP!)
64 bytes from 192.168.137.4: icmp_seq=4 ttl=64 time=0.439 ms (DUP!)
^H^C
--- 192.168.137.4 ping statistics ---
4 packets transmitted, 4 received, +10 duplicates, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.364/0.474/0.817/0.140 ms
root@ATK-IMX6U:~# ping www.baidu.com
ping: unknown host www.baidu.com
root@ATK-IMX6U:~# ping www.baidu.com
ping: unknown host www.baidu.com
root@ATK-IMX6U:~# ./app_main
This is a SmartHome Demo!
ds_ui_page_thread created successfully
humi_arc created successfully: 0x3b65a0
aiot_mqtt_connect failed: -0x0F0Bplease check variables like mqtt_host, produt_key, device_name, device_secret in demo
net_mqtt_aliyun_init failed.
root@ATK-IMX6U:~# ./app_main
This is a SmartHome Demo!
ds_ui_page_thread created successfully
aiot_mqtt_connect failed: -0x0F0Bplease check variables like mqtt_host, produt_key, device_name, device_secret in demo
net_mqtt_aliyun_init failed.
root@ATK-IMX6U:~# ./app_main
This is a SmartHome Demo!
ds_ui_page_thread created successfully
aiot_mqtt_connect failed: -0x0F0Bplease check variables like mqtt_host, produt_key, device_name, device_secret in demo
net_mqtt_aliyun_init failed.
root@ATK-IMX6U:~# ping 8.8.8.
ping: unknown host 8.8.8.
root@ATK-IMX6U:~# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
From 192.168.137.10 icmp_seq=1 Destination Host Unreachable
From 192.168.137.10 icmp_seq=2 Destination Host Unreachable
From 192.168.137.10 icmp_seq=3 Destination Host Unreachable
From 192.168.137.10 icmp_seq=4 Destination Host Unreachable
From 192.168.137.10 icmp_seq=5 Destination Host Unreachable
From 192.168.137.10 icmp_seq=6 Destination Host Unreachable
^C
--- 8.8.8.8 ping statistics ---
7 packets transmitted, 0 received, +6 errors, 100% packet loss, time 6002ms
pipe 3
root@ATK-IMX6U:~#
可以看到可以ping通主机和Ubuntu,但是ping 8.8.8.8和百度就有问题
解决问题
通过chatgpt知道了当前的情况是局域网内的连接正常,但无法连接到互联网。这通常与路由或DNS设置有关。发现的确是DNS没有设置不导致无法解析域名的问题。
所以检查并配置 /etc/resolv.conf
文件,通过命令:
nano /etc/resolv.conf
然后在其中添加
nameserver 8.8.8.8
nameserver 8.8.4.4
再重启一下网络
ifdown eth1 && ifup eth1
再尝试ping了8.8.8.8和baidu,问题解决。
永久解决/etc/resolv.conf重启失效问题
删除原有的文件重新建立一个
sudo rm -f /etc/resolv.conf
sudo nano /etc/resolv.conf
#填写上指定的DNS服务器:
nameserver 8.8.8.8
nameserver 8.8.4.4
此时resolv.conf文件的内容就会被锁定不会被重启覆盖
chattr +i /etc/resolv.conf
想要解锁的话运行:
chattr -i /etc/resolv.conf
复位重启后cat下resolv.conf,发现确实没有被修改,也可以正常ping通百度,nice!!!
结尾
希望能够帮助到大家解决这个问题,欢迎点赞收藏关注一波,如果无法解决也可以留言,或者也可以提供其他方法供其他小伙伴参考!!!