2

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.

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.