marsboard Android 4.4 添加开机自动设置静态IP地址

本文介绍了一种在Android设备上设置静态IP的方法。通过修改init.rc文件和服务配置,实现了开机自动设置静态IP地址,并解决了动态IP冲突的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在帮忙调试Android设备marsboard,需求就是开机设置静态IP地址。

调试过程与查找了很多资料,经过两天的测试终于成功了。

首先,init.rc文件最后添加开机自启动:

on property:sys.boot_completed=1    //系统启动完成后会执行以下命令设置静态IP
 /sbin/busybox ifconfig eth0 up
    /sbin/busybox ifconfig eth0 192.168.1.48 netmask 255.255.255.0 up
    /sbin/busybox route add default gw 192.168.1.1 dev eth0
    setprop net.eth0.dns1 218.30.19.40 
    setprop net.eth0.dns2 59.75.42.2

这样启动后,系统本来应该可以设置静态IP的了。可惜init.rc里有这样一个服务:

service dhcpcd_eth0 /system/bin/dhcpcd -ABKL  
    class main
    disabled
    oneshot

这个服务在插拔网线时都会运行一次 /system/bin/dhcpcd,设置动态IP,就这样导致eth0一直没有IP地址。

后面想了个折中的办法,就是修改这个服务:

service dhcpcd_eth0 /system/bin/setStaticIP.sh //把原先的“ /system/bin/dhcpcd -ABKL”改为我们自己的静态IP地址脚本/system/bin/setStaticIP.sh
    class main
    disabled
    oneshot

#on property:sys.boot_completed=1    //系统启动完成后会执行以下命令设置静态IP
#  /sbin/busybox ifconfig eth0 up
#    /sbin/busybox ifconfig eth0 192.168.1.48 netmask 255.255.255.0 up
#    /sbin/busybox route add default gw 192.168.1.1 dev eth0
#    setprop net.eth0.dns1 218.30.19.40 
#    setprop net.eth0.dns2 59.75.42.2

这样自己每次插拔网线就会执行一次我们自己的脚本/system/bin/setStaticIP.sh

记得把脚本setStaticIP.sh放到/system/bin/目录,并且设置好执行权限:chmod 777 /system/bin/setStaticIP.sh

/system/bin/setStaticIP.sh内容如下:

#!/system/bin/sh

# eth0 network setting static ip address
    /sbin/busybox ifconfig eth0 up
    /sbin/busybox ifconfig eth0 192.168.1.48 netmask 255.255.255.0 up
    /sbin/busybox route add default gw 192.168.1.1 dev eth0
    setprop net.eth0.dns1 218.30.19.40 
    setprop net.eth0.dns2 59.75.42.2
    echo "set static ip done"

注意:千万别用windows上编辑器编写setStaticIP.sh,不然会出现no such file。原因就是window编辑后每行都有一个^M结尾的(推荐使用VIM编写,VIM可以看到^M字符)。

我就是因为这样,出现no such file。解决办法:fromdos setStaticIP.sh


调试过程使用过的一些编译命令:

Android比较重要的三个img文件:
make systemimage - system.img
make userdataimage - userdata.img
make ramdisk - ramdisk.img
make snod - 快速打包system.img (with this command, it will build a new system.img very quickly. well, you cannot use “make snod” for all the situations. it would not check the dependences. if you change some code in the framework which will effect other applications)
因為
system.img 是 從 out/target/product/xxxx/system 做出來的。
如果改了 這個 folder 的內容,想要重新產生 system.img。不要管 system folde 裡面的 file 的 dependency,可以用 snod 這個 target:
make snod


调试命令:

start dhcpcd_eth0 //手动运行一次这个服务

stop dhcpcd_eth0 //手工停止这个服务


挂载分区命令:

# mount //查看挂载分区情况和权限

# mount -o remount,rw rootfs /

# mount -o remount,ro /dev/block/mtdblock0 /system 

能否挂载成功,涉及权限问题。init.rc有相关的设置,可以参考以下:


参考网页:

http://blog.chinaunix.net/uid-24856020-id-3389654.html

http://www.360doc.com/content/11/1213/19/3700464_172018977.shtml

http://www.tuicool.com/articles/Nbu6Bv


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值