How can I permanently change my hotspot tethering IP address?


Question

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

Note: Adding / removing IP address requires root access.






METHOD 1:



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 (within same subnet obviously):



~# ip address add 192.168.43.100/24 dev wlan0


* Replace add with del to delete.



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






METHOD 2:



The problem with above approach is that the added IP is not permanent. Once you switch OFF the hotspot, IP will get cleared. So you can use an init trigger to set IP address whenever hotspot is turned ON. 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 the device is rooted with Magisk.






METHOD 3:



dnsmasq starts listening on all IP addresses which are set on WiFi interface before dnsmasq is started. But with both above methods there is no way to make sure that IP is added after netd sets up the interface and before dnsmasq starts. So the added IP address may conflict 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).



To address this problem:




  • Reserve the manually added IP address by editing dnsmasq.conf. See How can I permanently assign a static IP address to Wi-Fi or USB tether clients?

  • Or modify DHCP IP range, but it's hard coded. You can replace dnsmasq with a shell script to achieve this. See How to change the default hotspot DHCP IP address range?

  • Or replace /system/bin/dnsmasq with a shell script which adds IP address before executing original dnsmasq binary. Rename dnsmasq to dnsmasq.bin and create script: /system/bin/dnsmasq:



    #!/system/bin/sh

    # set fixed IP address on Wi-Fi interface
    ip address add 192.168.43.100/24 dev wlan0

    # execute original binary
    exec dnsmasq.bin $*



    • Set ownership, permission and SELinux context on both files. See this answer for details.

    • Additionally you may add listen-address=192.168.43.100 (5) to dnsmasq.conf.

    • 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.




NOTE that in order to modify .rc, dnsmasq or dnsmasq.conf files, /vendor and /system partitions need to be mounted R/W which requires dm-verity disabled. Otherwise you may use bind mounts (used by Magisk modules). On non-SAR devices /init.rc can also be edited to modify /init.rc file in ramdisk by unpacking boot.img.


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes