Android通过shell命令修改网络信息
2018年08月14日 11:33:20 feiniao8651 阅读数:568
文章允许转载,请注明来源:https://blog.csdn.net/feiniao8651/article/details/81664039
Android一般是通过设置界面来配置各种网络信息,今天讲一下当不方便使用设置的时候,通过shell命令来查看和配置网络的方法。
注:以下shell命令在不同版本Android系统上可能存在差异。
查看网络连接信息
查看全部连接
#netcfg
ccmni2 DOWN 0.0.0.0/0 0x00000080 11:22:22:33:44:11
ccmni0 DOWN 0.0.0.0/0 0x00000080 2a:3d:e1:22:5d:11
ccmni1 DOWN 0.0.0.0/0 0x00000080 1e:bc:dd:5a:32:11
wlan0 UP 192.168.1.151/24 0x00001043 5c:f7:c3:dd:44:11
tunl0 DOWN 0.0.0.0/0 0x00000080 00:00:00:00:00:00
lo UP 127.0.0.1/8 0x00000049 00:00:00:00:00:00
sit0 DOWN 0.0.0.0/0 0x00000080 00:00:00:00:00:00
p2p0 UP 0.0.0.0/0 0x00001003 5e:f7:c3:dd:44:11
ifb0 DOWN 0.0.0.0/0 0x00000082 e6:a9:76:38:04:11
ifb1 DOWN 0.0.0.0/0 0x00000082 96:d6:f4:59:10:11
ip6tnl0 DOWN 0.0.0.0/0 0x00000080 00:00:00:00:00:11
- 1
查看指定连接
#ifconfig wlan0
wlan0: ip 192.168.1.151 mask 255.255.255.0 flags [up broadcast running multicast]
- 1
- 2
设置IP
方法1:
//ifconfig <dev> <ip> netmask <value>
例:ifconfig wlan0 192.168.1.10 netmask 255.255.255.0
- 1
- 2
方法2:
//ip address add <ip> dev <value>
例:ip address add 192.168.1.10/24 dev wlan0
- 1
- 2
设置默认网关
设置默认网关主要是通过路由表
ip route add 192.168.1.0/24 via 192.168.1.1
- 1
也可以直接使用dhcp命令来自动获取
netcfg wlan0 dhcp
- 1
设置DNS
设置dns的方法比较多,但是不同环境,以及不同的系统版本,能生效的方式不一定相同,可以多试几种,这里提供一种我用的方式,适用于Android 5.1,其他系统参照https://github.com/ukanth/afwall/wiki/DNS#resolver-commands
ndc resolver setnetdns <netid> <domain> <dns1> <dns2>
例:ndc resolver setnetdns wlan0 "" 8.8.8.8 8.8.4.4
- 1
- 2
之前在xda论坛上看到过其他方式,如果以上方式无效,也可以看一下这个论坛里的方法https://forum.xda-developers.com/general/xda-university/guide-how-to-change-dns-android-device-t3273769
这些命令有的比较简单,用法也比较单一,比如netcfg,但是有的就要复杂一些,比如路由表的配置。Android 的shell环境里没有man的命令,查看具体的命令用法一般就是在命令后加-h 或者help。
参考:
DNS-commands
ifconfig vs ip
[GUIDE] How to Change DNS in Android Device (8 methods)
Android 命令设置获取、IP地址、网关、dns
2017年10月17日 17:16:22 xz——华 阅读数:5076
https://blog.csdn.net/zhang__ao/article/details/78262738
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhang__ao/article/details/78262738
通过shell命令设置(获取)IP、网关、dns信息,需要获取root权限
查看所有网络信息
-
C:\>adb shell
-
root@android:/ # netcfg
-
netcfg
-
ip6tnl0 DOWN 0.0.0.0/0 0x00000080 00:00:00:
-
00:00:00
-
gre0 DOWN 0.0.0.0/0 0x00000080 00:00:00:
-
00:00:00
-
eth0 UP 192.168.0.180/24 0x00001043 00:00:00:
-
ec:0a:00
-
sit0 DOWN 0.0.0.0/0 0x00000080 00:00:00:
-
00:00:00
-
lo UP 127.0.0.1/8 0x00000049 00:00:00:
-
00:00:00
-
tunl0 DOWN 0.0.0.0/0 0x00000080 00:00:00:
-
00:00:00
查看eth0
-
root@android:/ # ifconfig eth0
-
ifconfig eth0
-
eth0: ip 192.168.0.180 mask 255.255.255.0 flags [up broadcast running multicast]
查看dns
-
root@android:/ # getprop net.eth0.dns1
-
getprop net.eth0.dns1
-
8.8.8.8
-
root@android:/ # getprop net.eth0.dns2
-
getprop net.eth0.dns2
-
8.8.4.4
设置ip
-
root@android:/ # ifconfig eth0 192.168.0.173 netmask 255.255.255.0
-
ifconfig eth0 192.168.0.173 netmask 255.255.255.0
-
root@android:/ # ifconfig eth0
-
ifconfig eth0
-
eth0: ip 192.168.0.173 mask 255.255.255.0 flags [up broadcast running multicast]
设置网关Gateway
-
root@android:/ # route add default gw 192.168.0.1 dev eth0
-
route add default gw 192.168.0.1 dev eth0
添加dns
-
root@android:/ # setprop net.eth0.dns1 8.8.8.8
-
setprop net.eth0.dns1 8.8.8.8
-
root@android:/ # setprop net.eth0.dns2 8.8.4.4
-
setprop net.eth0.dns2 8.8.4.4
查看eth配置信息
-
root@android:/ # getprop | grep eth0
-
getprop | grep eth0
-
[dhcp.eth0.dns1]: [192.168.0.1]
-
[dhcp.eth0.dns2]: []
-
[dhcp.eth0.dns3]: []
-
[dhcp.eth0.dns4]: []
-
[dhcp.eth0.gateway]: [192.168.0.1]
-
[dhcp.eth0.ipaddress]: [192.168.0.180]
-
[dhcp.eth0.leasetime]: [3600]
-
[dhcp.eth0.mask]: [255.255.255.0]
-
[dhcp.eth0.pid]: [13800]
-
[dhcp.eth0.reason]: [PREINIT]
-
[dhcp.eth0.result]: [failed]
-
[dhcp.eth0.server]: [192.168.0.1]
-
[dhcp.eth0.vendorInfo]: []
-
[net.change]: [net.eth0.dns2]
-
[net.eth0.dns1]: [8.8.8.8]
-
[net.eth0.dns2]: [8.8.4.4]