• 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目录下。

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

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

    1. #!/bin/sh
    2. # udhcpc script edited by Tim Riker
    3. RESOLV_CONF="/etc/resolv.conf"
    4. [ -n "$1" ] || { echo "Error: should be called from udhcpc"; exit 1; }
    5. NETMASK=""
    6. [ -n "$subnet" ] && NETMASK="netmask $subnet"
    7. BROADCAST="broadcast +"
    8. [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
    9. case "$1" in
    10. deconfig)
    11. echo "Setting IP address 0.0.0.0 on $interface"
    12. ifconfig $interface 0.0.0.0
    13. ;;
    14. renew|bound)
    15. echo "Setting IP address $ip on $interface"
    16. ifconfig $interface $ip $NETMASK $BROADCAST
    17. if [ -n "$router" ] ; then
    18. echo "Deleting routers"
    19. while route del default gw 0.0.0.0 dev $interface ; do
    20. :
    21. done
    22. metric=10
    23. for i in $router ; do
    24. echo "Adding router $i"
    25. if [ "$subnet" = "255.255.255.255" ]; then
    26. # special case for /32 subnets:
    27. # /32 instructs kernel to always use routing for all outgoing packets
    28. # (they can never be sent to local subnet - there is no local subnet for /32).
    29. # Used in datacenters, avoids the need for private ip-addresses between two hops.
    30. ip route add $i dev $interface
    31. fi
    32. route add default gw $i dev $interface metric $((metric++))
    33. done
    34. fi
    35. echo "Recreating $RESOLV_CONF"
    36. # If the file is a symlink somewhere (like /etc/resolv.conf
    37. # pointing to /run/resolv.conf), make sure things work.
    38. realconf=$(readlink -f "$RESOLV_CONF" 2>/dev/null || echo "$RESOLV_CONF")
    39. tmpfile="$realconf-$$"
    40. # set the eth0 nameserver 114.114.114.114
    41. echo "nameserver 114.114.114.114" > "$tmpfile"
    42. [ -n "$domain" ] && echo "search $domain" >> "$tmpfile"
    43. for i in $dns ; do
    44. echo " Adding DNS server $i"
    45. echo "nameserver $i" >> "$tmpfile"
    46. done
    47. mv "$tmpfile" "$realconf"
    48. ;;
    49. esac
    50. 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查看路由表。如下:

    1. root@imx6ull:~# route -n
    2. Kernel IP routing table
    3. Destination Gateway Genmask Flags Metric Ref Use Iface
    4. 0.0.0.0 10.33.149.161 0.0.0.0 UG 0 0 0 eth2
    5. 0.0.0.0 10.10.26.1 0.0.0.0 UG 0 0 0 eth0
    6. 10.10.26.0 0.0.0.0 255.255.254.0 U 0 0 0 eth0
    7. 10.33.149.160 0.0.0.0 255.255.255.252 U 0 0 0 eth2
    8. root@imx6ull:~# ip route show
    9. default via 10.10.26.1 dev eth0 onlink
    10. default via 10.33.149.161 dev eth2
    11. 10.10.26.0/23 dev eth0 proto kernel scope link src 10.10.26.8
    12. 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文件,具体如下:

     

         

  • 相关阅读:
    可编程直流回馈式负载箱的工作原理
    Python3,5行代码,制作Gif动图,太简单了。
    ADO.NET+kafka实现发布订阅保存到数据库
    Windows10启用WSL2,安装子系统Ubuntu20.04.4 LTS并在Ubuntu中部署docker
    函数柯里化
    (一)Lenet5 手写数字识别原理及代码解析
    不同架构的详细分析VIP架构
    数据挖掘与分析课程笔记(Chapter 21)
    音频频谱显示-基于fmod设计音乐播放器并动态显示频谱图(二)
    Git reset 之 soft、mixed、hard 区别(实战)
  • 原文地址:https://blog.csdn.net/fhqlongteng/article/details/127651053