Friday, July 10, 2015

5.1 lollipop - How can I permanently change my hotspot tethering IP address?



IP address is 192.168.43.1 by default and there is no option to change it permanently. Is it possible to change it without root access?


My phone is Huawei Android 5.1.



Answer



Before Android Pie, tethering IP (192.168.43.1) was hard-coded (1, 2). But now it's randomized on each session(3). You can use Android's builtin ip command to set an additional fixed IP address but it requires root access:


~# ip address add 192.168.43.100/24 dev wlan0

* Replace add with del to delete.


Make sure the name of your Wi-Fi interface (wlan0 usually) is correct. Check with ip link or ls /sys/class/net.


Now there comes two problems:




  • You can set whatever IP address you want to use (within same subnet obviously). But it should not contradict with IP leased to another host (saved in /data/misc/dhcp/dnsmasq.leases) by DHCP server (dnsmasq up to Pie) from DHCP range (192.168.43.2 to 192.168.43.254 by default) (4). Since the range is hard coded, you either need to replace /system/bin/dnsmasq with a shell script (as explained here) to change --dhcp-range= (5) option before starting DHCP server. Or use /etc/dnsmasq.conf to assign fixed IP's only (6) (as explained here).

  • Also the IP change is not permanent. Once you switch of hotspot, IP will get cleared.


A simple solution to both of the above problems is to occupy the desired IP soon after tethering is turned ON. We can use an init trigger to achieve this. Add these lines to (/vendor)/etc/init/hostapd.android.rc file (or any .rc file under /etc/init/):


on property:init.svc.hostapd=running
exec - -- /system/bin/sleep 2
exec u:r:magisk:s0 -- /system/bin/ip address add 192.168.43.100/24 dev wlan0

* hostapd is the system service which manages access points.
* Service uses Magisk's SELinux context assuming that device is rooted with Magisk.



Use ss or netstat to make sure that DHCP server is listening on reserved IP address (or on all IP addresses i.e. 0.0.0.0) so that to avoid IP address collision.


To modify the .rc file, /vendor partition needs to be mounted R/W which requires dm-verity disabled. Otherwise on non-SAR devices you can edit /init.rc in ramdisk by unpacking boot.img.


No comments:

Post a Comment

samsung galaxy s 2 - Cannot restore Kies backup after firmware upgrade

I backed up my Samsung Galaxy S2 on Kies before updating to Ice Cream Sandwich. After the upgrade I tried to restore, but the restore fails ...