Android设备测试服务器时,可能须要修改 hosts 文件指定域名到对应的 IP 地址。Android 是基于 Linux 的系统,与 Linux 相似,经过 hosts 文件来设置。
在 Android 下,/etc 是 link 到 /system/etc 的,咱们须要修改 /system/etc/hosts 来实现。步骤有两种,一种时在pc上修改,而后再push到手机中。一种是直接在手机上修改以添加“127.0.0.1 host1.example.com”为例子shell
在pc上修改再push到电脑中:服务器
一、得到root权限:adb root
二、设置/system为可读写:adb remount
三、将hosts文件复制到PC:adb pull /system/etc/hosts
四、修改PC机上文件
五、将PC机上文件复制到手机:adb push /system/etc/hosts测试
直接在手机上修改以下:
一、得到root权限:adb root
二、设置/system为可读写:adb remountspa
三、进入adb shell :adb shell
四、打开host文件 : cd etc ;cat hostsrem
五、将“127.0.0.1 host1.example.com”添加到hosts文件末尾:echo ”127.0.0.1 host1.example.com”>> hosts 或者 用“127.0.0.1 host1.example.com”重写hosts文件,将以前的覆盖掉:echo ”127.0.0.1 host1.example.com”> hosts 域名
若是要查看是否修改为功,能够在PC上执行adb shell,运行cat /system/etc/hosts;或者在手机上运行cat /system/etc/hosts。注意:可能直接cat /system/etc/hosts会出现read only file的错误,此时就分两步走:先cd etc ,在cat hosts,若是还不行,在进入adb shell命令前先执行adb remount ,把手机文件先挂载一次。
在Android 系统中,hosts文件格式有一点与PC机Linux不一样:不能在一行中一个IP对应多个域名,好比:
127.0.0.1 host1.example.com host2.example.com host3.example.com
在大多PC机Linux系统是合法的,但不能在Android上起做用,须要拆成每一个域名一行才能使用:
127.0.0.1 host1.example.com
127.0.0.1 host2.example.com
127.0.0.1 host3.example.comfile