前兩天寫了個腳本通過https://freeapi.ipip.net/獲取IP信息,在公司電信網絡沒問題,在家裡有時可以有時不行。對比dns後發現獲得的ip是不同的,於是打算在hosts文件里寫死。安卓的hosts文件在/system/etc/hosts,但是在/etc/hosts也有鏈接,獲取Termux已經在root用戶下,仍然提示文件只讀。搜索後發現我的android6用的可能是system-headless root,也就是/system是已只讀方式掛載的,這樣或許可以保護系統吧。解決辦法在How to edit 'etc/hosts' file?找到:
#for Android 6
su # become the root (don't miss confirmation request!)
mount -o remount,rw /system # allow to write
vi /system/etc/hosts ## edit the file in place - do what you want, then <ESC>:wq ##
mount -o remount,ro /system # get things back to normal
exit # unroot
#for Android 8 with magisk and busybox
su # become the root (don't miss confirmation request!)
busybox mount -o remount,rw /system # allow to write
vi /system/etc/hosts ## edit the file in place - do what you want, then <ESC>:wq ##
busybox mount -o remount,ro /system # get things back to normal
exit # unroot
#for Android 10
#remount /system may get this error
#mount: '/system' not in /proc/mounts
#just remount the root dir / will be ok
su # become the root (don't miss confirmation request!)
mount -o remount,rw / # allow to write
vi /system/etc/hosts ## edit the file in place - do what you want, then <ESC>:wq ##
mount -o remount,ro /system # get things back to normal
exit # unroot
後來呢,發現有時還時不行,所以不單單是dns的問題。再後來,發現ip-api家的免費接口也很不錯,現在兩個一起用。
本文更新於 2021/07/13。