在上一篇中,我们成功将U-boot移植到自己的板子上,并适配了板子的硬件,可以正确ping通主机。并且使用网络启动的方式启动了内核。详情参考:ZYNQ专题-在自己的板子上移植U-boot无法联网问题
然而内核起来后还没有那么简单,刚刚解决了U-boot的网络问题,这里Linux又出现了网络问题,依然是无法ping通主机。
root@PETALINUX-TEST:~# ping 192.168.1.18
PING 192.168.1.18 (192.168.1.18): 56 data bytes
ping: sendto: Network is unreachable
查看此时的网络信息
root@PETALINUX-TEST:~# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0A:35:00:01:22
inet6 addr: fe80::20a:35ff:fe00:122%lo/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:74 errors:0 dropped:0 overruns:0 frame:0
TX packets:126 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4798 (4.6 KiB) TX bytes:39380 (38.4 KiB)
Interrupt:28 Base address:0xb000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1%1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
可以看到这里的eth0中没有ip地址,netmask子网掩码,gateway网关等
再检查一下网卡信息
root@PETALINUX-TEST:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0a:35:00:01:22 brd ff:ff:ff:ff:ff:ff
inet6 fe80::20a:35ff:fe00:122/64 scope link
valid_lft forever preferred_lft forever
3: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/sit 0.0.0.0 brd 0.0.0.0
好的,这里确认了确实啥也没有,那我们尝试着手动给它添加看看呢。
我们执行下面的命令,去编辑网络
vi /etc/network/interfaces//注意这里是不识别vim命令的,其他的操作都是和vim一样
执行完后打开如下编辑内容
# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
# The loopback interface
auto lo
iface lo inet loopback
# Wireless interfaces
iface wlan0 inet dhcp
wireless_mode managed
wireless_essid any
wpa-driver wext
wpa-conf /etc/wpa_supplicant.conf
iface atml0 inet dhcp
# Wired or wireless interfaces
auto eth0
iface eth0 inet dhcp //这里的内容需要修改
iface eth1 inet dhcp
# Ethernet/RNDIS gadget (g_ether)
# ... or on host side, usbnet and random hwaddr
iface usb0 inet static
address 192.168.7.2
netmask 255.255.255.0
network 192.168.7.0
gateway 192.168.7.1
# Bluetooth networking
iface bnep0 inet dhcp
看到上面的内容可以知道,此时的Linxu网络是选择的DHCP模式,但是我们没有接路由器,所以没办法分配ip地址,因此我们对这里进行修改,即把DHCP更改成STATIC以及手动配置IP地址等内容,修改完如下:
# The loopback interface
auto lo
iface lo inet loopback
# Wireless interfaces
iface wlan0 inet dhcp
wireless_mode managed
wireless_essid any
wpa-driver wext
wpa-conf /etc/wpa_supplicant.conf
iface atml0 inet dhcp
# Wired or wireless interfaces
auto eth0
iface eth0 inet static //这里的配置信息一定要写全哦,不然后面会报错的,网络起不来
address 192.168.1.10
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
iface eth1 inet dhcp
# Ethernet/RNDIS gadget (g_ether)
# ... or on host side, usbnet and random hwaddr
iface usb0 inet static
address 192.168.7.2
netmask 255.255.255.0
network 192.168.7.0
gateway 192.168.7.1
# Bluetooth networking
iface bnep0 inet dhcp
到这里应该可以了,保存后退出。
然后需要做的一步,重启网卡
执行下面命令
ifdown eth0
macb e000b000.ethernet eth0: link down
ifup eth0
macb e000b000.ethernet eth0: link up (1000/Full)
此时再看网络信息就已经更新到了
root@PETALINUX-TEST:/etc/init.d# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0A:35:00:01:22
inet addr:192.168.1.10 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20a:35ff:fe00:122%lo/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:67 errors:0 dropped:0 overruns:0 frame:0
TX packets:124 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4380 (4.2 KiB) TX bytes:37048 (36.1 KiB)
这时候再去ping主机就没问题了,那么这个就结束了。