ubuntu16 ARM 4G双网卡的上网配置

1、硬件
     

     Ubuntu 16.04.5 LTS运行在正点原子 阿尔法 开发板上面,cpu imx6ull具有2个有线以太网接口,再接上一下移远的EC20模块,目有3个网卡,实现一个有线网卡与4G网卡的上网配置。

2、配置网络


      移植ubuntu 16根文件系统到上面以后,设置开机 eth0为静态IP, eth1为dhcp,动态获取IP。需要修改配置文件如下:/etc/network/interfaces.d/目录下创建2个文件eth0, eth1。

     配置eth0网卡为静态IP,eth1网卡为自动IP.

 3、EC20移植设置

       EC20移植把驱动加入内核中编译,再编译生成qeucet-CM文件,quectel-CM用启动控制EC20的启动入网,获取IP地址等操quectel-CM文件放在/usr/bin/目录下。由于quectel-CM启动中需要使用udhcpc和busybox两个文件,可以通过apt-get install udhcpc来安装一下即可。最后可以查看到usr/bin/目录下要有udhcpc, busybox这两个文件。

     quectel-CM启动时会调用udhcpc命令,如下面和启动记录中。udhcpc命令会使用一个default.script的脚本来设置4G网卡的IP地址。default.script脚本可以从开发板光盘的根文件系统中获取,并且放在usr/share/udhcpc目录下。

root@imx6ull:~# quectel-CM -s cenet &
[1] 345
root@imx6ull:~# [11-02_09:35:31:253] WCDMA&LTE_QConnectManager_Linux&Android_V1.1.34
[11-02_09:35:31:256] quectel-CM profile[1] = cenet///0, pincode = (null)
[11-02_09:35:31:262] Find /sys/bus/usb/devices/2-1.2 idVendor=2c7c idProduct=0125
[11-02_09:35:31:264] Find /sys/bus/usb/devices/2-1.2:1.4/net/eth2
[11-02_09:35:31:266] Find usbnet_adapter = eth2
[11-02_09:35:31:268] Find /sys/bus/usb/devices/2-1.2:1.4/GobiQMI/qcqmi2
[11-02_09:35:31:270] Find qmichannel = /dev/qcqmi2
[11-02_09:35:31:333] Get clientWDS = 7
[11-02_09:35:31:364] Get clientDMS = 8
[11-02_09:35:31:396] Get clientNAS = 9
[11-02_09:35:31:428] Get clientUIM = 10
[11-02_09:35:31:460] Get clientWDA = 11
[11-02_09:35:31:493] requestBaseBandVersion EC20CEHCR06A03M1G
[11-02_09:35:31:588] requestGetSIMStatus SIMStatus: SIM_READY
[11-02_09:35:31:588] requestSetProfile[1] cenet///0
[11-02_09:35:31:653] requestGetProfile[1] cenet///0
[11-02_09:35:31:684] requestRegistrationState2 MCC: 460, MNC: 0, PS: Attached, DataCap: LTE
[11-02_09:35:31:716] requestQueryDataCall IPv4ConnectionStatus: DISCONNECTED
[11-02_09:35:31:780] requestRegistrationState2 MCC: 460, MNC: 0, PS: Attached, DataCap: LTE
[11-02_09:35:31:813] requestSetupDataCall WdsConnectionIPv4Handle: 0xe17f8390
[11-02_09:35:31:907] requestQueryDataCall IPv4ConnectionStatus: CONNECTED
[11-02_09:35:31:940] ifconfig eth2 up
[11-02_09:35:31:980] busybox udhcpc -f -n -q -t 5 -i eth2
udhcpc: started, v1.29.0
[11-02_09:35:32:046] Setting IP address 0.0.0.0 on eth2
udhcpc: sending discover
udhcpc: sending select for 10.33.149.162
udhcpc: lease of 10.33.149.162 obtained, lease time 7200
[11-02_09:35:32:234] Setting IP address 10.33.149.162 on eth2

     以下是default.script的源代码,也可以直接复制创建文件。

#!/bin/sh
# udhcpc script edited by Tim Riker <[email protected]>

RESOLV_CONF="/etc/resolv.conf"

[ -n "$1" ] || { echo "Error: should be called from udhcpc"; exit 1; }

NETMASK=""
[ -n "$subnet" ] && NETMASK="netmask $subnet"
BROADCAST="broadcast +"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"

case "$1" in
	deconfig)
		echo "Setting IP address 0.0.0.0 on $interface"
		ifconfig $interface 0.0.0.0
		;;

	renew|bound)
		echo "Setting IP address $ip on $interface"
		ifconfig $interface $ip $NETMASK $BROADCAST

		if [ -n "$router" ] ; then
			echo "Deleting routers"
			while route del default gw 0.0.0.0 dev $interface ; do
				:
			done

			metric=10
			for i in $router ; do
				echo "Adding router $i"
				if [ "$subnet" = "255.255.255.255" ]; then
	# special case for /32 subnets:
	# /32 instructs kernel to always use routing for all outgoing packets
	# (they can never be sent to local subnet - there is no local subnet for /32).
	# Used in datacenters, avoids the need for private ip-addresses between two hops.
					ip route add $i dev $interface
				fi
				route add default gw $i dev $interface metric $((metric++))
			done
		fi

		echo "Recreating $RESOLV_CONF"
		# If the file is a symlink somewhere (like /etc/resolv.conf
		# pointing to /run/resolv.conf), make sure things work.
		realconf=$(readlink -f "$RESOLV_CONF" 2>/dev/null || echo "$RESOLV_CONF")
		tmpfile="$realconf-$$"
		
		# set the eth0 nameserver 114.114.114.114 
		echo "nameserver 114.114.114.114" > "$tmpfile"
		[ -n "$domain" ] && echo "search $domain" >> "$tmpfile"
		for i in $dns ; do
			echo " Adding DNS server $i"
			echo "nameserver $i" >> "$tmpfile"
		done
		mv "$tmpfile" "$realconf"
		;;
esac

exit 0

       经过以上一系列的操作,可以启动4G网卡了。在命令行输入quectel-CM -s cenet &命令后,启动进行设置4G网卡,输出的打印记录中有arithmetic expression: expecting primary: "metric++"这样一句提示,意思是执行default.script脚本错误,原因是程序启动使用的命令行中不支持脚本中route add default gw $i dev $interface metric $((metric++))。

     

     Ubuntu 中/bin/sh链接默认指向的是dash shell,而服务器上指向的是bash shell。
dash是一个小巧的shell,他的功能自然也就没有bash强大,上述问题就是有与dash shell不支持metric++运算和let命令造成的。

解决办法:sudo ln -sf /bin/bash /bin/sh

    经过以上操作4G网卡可以正常出来,获取到了IP地址,但是在默认的网络配置下,无法连接外网了。

4、路由表的问题

        不能连接外网,使用ping www.qq.com不通,一般是路由表的问题或是dns服务器地址配置的不对。通过route -n和ip route show查看路由表。如下:

root@imx6ull:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.33.149.161   0.0.0.0         UG    0      0        0 eth2
0.0.0.0         10.10.26.1      0.0.0.0         UG    0      0        0 eth0
10.10.26.0      0.0.0.0         255.255.254.0   U     0      0        0 eth0
10.33.149.160   0.0.0.0         255.255.255.252 U     0      0        0 eth2
root@imx6ull:~# ip route show
default via 10.10.26.1 dev eth0 onlink
default via 10.33.149.161 dev eth2
10.10.26.0/23 dev eth0  proto kernel  scope link  src 10.10.26.8
10.33.149.160/30 dev eth2  proto kernel  scope link  src 10.33.149.162

    查看DNS服务器, cat /etc/resolv.conf。输出nameserver 221.179.155.193
nameserver 221.179.155.209。根据quecel-CM启动的记录可以查看到,这两个dns是4G网卡的设置的,原来的默认的有线网卡的DNS地址没有了。

     上面的ip route show查看到路由表可以知道,上外网优先使用前的默认网卡 eth0,而eth0又没有dns服务器地址,无法进行域名解析,只能连接内网,无法ping通外网。      

5、配置路由表

      通过分析quectel-CM的程序代码,关于ip与路由的设置是由default.script文件来实现的,所以修改4G卡的跃点为10, 由于4G网卡获取 dns服务器后改resolv.conf文件时,保留原来的有线网卡使用的dns。

     具体修改如下:

       有线网卡eth0的跃点配置:

       经过以上设置后,上外网会低跃点的使用4G网卡。如果想使用有线网卡就修改eth0的跃点比4G网卡跃点低即可了。

       linux连接网的路由选择是选择Mertic(跃点)小的网卡连接外网,目前的配置两个卡的跃点一样全部为0。

       经过以上设备查看路由配置可以看到eth2 4G网卡的跃点10,有线网卡eth0为50,上外网使用4G网卡。此时路由表如下:

      

6、开机自动启动设置

       开机自动启动加上4G网卡的自启动,修改rc.local文件,具体如下:

     

猜你喜欢

转载自blog.csdn.net/fhqlongteng/article/details/127651053