Android 下 hosts 文件修改教程

    技术2022-05-20  48

    由于某些原因,可能需要修改 hosts 文件指定域名到对应的 IP 地址。Android 是基于 Linux 的系统,与 Linux 类似,通过 hosts 文件来设置。

      在 Android 下,/etc 是 link 到 /system/etc 的,我们需要修改 /system/etc/hosts 来实现。但是这个文件是只读,不能通过 shell 直接修改。可以通过连接到 PC 上使用 adb 来修改。步骤如下:

      1、获得root权限:adb root

      2、设置/system为可读写:adb remount

      3、将hosts文件复制到PC:adb pull /system/etc/hosts

      4、修改PC机上文件

      5、将PC机上文件复制到手机:adb push /system/etc/hosts

      如果要查看是否修改成功,可以在PC上执行adb shell,运行cat /system/etc/hosts;或者在手机上运行cat /system/etc/hosts。

      在Android 1.6系统中,hosts文件格式有一点与PC机Linux不同:不能在一行中一个IP对应多个域名,比如:

      127.0.0.1 host1.example.com host2.example.com host3.example.com

      在大多PC机Linux系统是合法的,但不能在Android 1.6上起作用,需要拆成每个域名一行才能使用:

      127.0.0.1 host1.example.com

      127.0.0.1 host2.example.com

      127.0.0.1 host3.example.com

     

    -------------------------------------------------------------------------------------------android 下修改 hosts文件 及 out of memory的解决

    需要使用 android模拟器访问某个网站,其实是指向开发机器上的,这时需要修改 andorid上的 hosts文件,

    下面这个网站介绍了方法

    http://www.hiapk.com/bbs/thread-63992-1-1.html

    具体步骤如下

    1、获得root权限:adb root

    2、设置/system为可读写:adb remount

    3、将hosts文件复制到PC:adb pull /system/etc/hosts <PC机上文件名>

    4、修改PC机上文件

    5、将PC机上文件复制到手机:adb push <PC机上文件名> /system/etc/hosts

    但在第五步时,有的人会报  out of memory的错误

    这是因为直接用命令行启动,而没加一个参数造成的,所以用下面这个命令来启动就行了

    $emulator –avd youravdname –partition-size 128

    来源于(可能被墙了)

    http://www.cuteandroid.com/tips-for-android-developer-failed-to-copy-file-to-system

    ==================================================================================================================

    更改Android模拟器中的hosts文件下载 android 模拟器后解压得到 android-sdk-linux_x86 文件夹

    进入到android-sdk-linux_x86/tools/

    执行 ./android 创建一个test的虚拟手机

    启动虚拟手机并更改分区大小为128M

    ./emulator -avd test -partition-size 128

    新打开一个终端在当前目录执行./adb pull /system/etc/hosts /home/jecelyin/取出hosts文件到PC上

    然后执行./adb root./adb remount获得root权限并将虚拟手机上的/system重新挂载为可写

    在电脑上修改/home/jecelyin/hosts文件,如果需要虚拟手机访问电脑上的web网站,hosts的ip地址应该为10.0.2.2

    执行下面命令传回./adb push /home/jecelyin/hosts /system/etc/hosts

    重启模拟器


    最新回复(0)