How to edit /etc/hosts file in Android Studio emulator running in nougat?
Asked Answered
P

15

74

Anyone know how to edit /etc/hosts file inside an android studio emulator running in nougat? I will be editing it so I can use my virtual host in my local web server. I tried editing it through terminal using adb however, it is returning Read-only file system. Tried also using chmod but still it fails.

Update: I also tried pulling and pushing files using adb $ ./adb -s emulator-5554 push ~/Desktop/hosts /system/etc/hosts

adb: error: failed to copy '/Users/Christian/Desktop/hosts' to '/system/etc/hosts': couldn't create file: Read-only file system

Primer answered 13/12, 2016 at 9:37 Comment(2)
you need to use ADB for that, it's true. show how you tried.Cini
I tried pulling and pushing files using adb but it still returns Read-only file systemPrimer
P
8

I was able to edit the /etc/hosts file by launching the emulator with -writable-system and remounting the emulator using adb remount. After that the hosts file inside the emulator is editable. I tried pushing/replacing the file and succeeded.

Primer answered 14/12, 2016 at 4:34 Comment(1)
This does not work if your emulator has Google play iconFerebee
O
113
1) android-sdk-macosx/tools/emulator -avd <avdname> -writable-system
2) ./adb root
3) ./adb remount
4) ./adb push <local>/hosts /etc/hosts

Android file host can be

/etc/hosts <--- This worked for me
/etc/system/hosts
/system/etc/hosts

Check

1) ./adb shell
2) cat /etc/hosts
3) ping customsite.com
Opinionated answered 3/12, 2017 at 18:54 Comment(17)
I really liked how concise the solution is presented though it's pretty much the same as other answers here.Mercurialize
just a note, I had to run the emulator -avd ... in one shell, and open another shell for the adb root, adb remount and adb push ...Niersteiner
The -writable-system is very important when launching the avd. Add a & at the end of the command to launch in background.Novellanovello
if using windows, make sure that your hosts file is using unix style endings, otherwise it will be ignored/emulator will crash/etc.Telestich
Changes are lost on restart. There's a way to make the changes permanent?Emmaline
It seems the changes only persist when -writable-system is used. If you run the image without that parameter, there's no change on the hosts file; the new line will reappear if -writable-system is set again. This may force the user to always run the emulator through command line.Emmaline
I tried it with Nougat & it worked fine, but when tried it with an emulator having Oreo, it works for just seconds, then the system removes the pushed file & reverts the system back to read-only! .. do someone know how to fix this with Oreo?Picker
Make sure you have a blank line after the last entry of the hosts fileCreatine
The path for me, on OS X is /Library/Android/sdk/emulator . Within that directory is the emulator file you need to run and then everything else works.Cystitis
If you having trouble to do adb root adbd cannot run as root in production builds, you should create a new Emulator Image using Google Api's instead of Google Play. They describe it here #43924496Lietuva
this stopped working for me. even though I set -writable-system, the hosts file update doesn't "stick". When I close the emulator I see "emulator: WARNING: Discarding the changed state (command-line flag)." go by, and on next boot the change is gone.Commensal
It works but I had to add 5th step after push, "5) ./adb reboot"Schach
adb remount gives "remount failed"Watermark
Note: currently not working on API29+, so just use API28 or lower.Reticule
I had the same issue adb cannot run as root.. so i changed the emulator to version under API28 and without google play services. It worked fine. ThanksTompion
IMPORTANT : Add a blank line after the last entry in the hosts file. This should be mentioned in the answer and I'm just repeating this because it will not work if you don't! It's a really hard issue to debug.Permatron
Note - adbd cannot run as root in production builds. You can’t root a device with Play Store installed on it! Choose a device without play store.Caines
P
34

Step by Step

  1. Don’t Create the AVD with a Google Play image.
  2. Use for example Google APIs Intel x86 Atom System Image.
  3. Start the emulator with the following command…

    emulator.exe –avd <avd name> -writable-system
    

For example:

    C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\emulator>emulator.exe -avd Pixel_API_25 -writable-system

    emulator: WARNING: System image is writable
    HAX is working and emulator runs in fast virt mode.
    audio: Failed to create voice `goldfish_audio_in'
    qemu-system-i386.exe: warning: opening audio input failed
    audio: Failed to create voice `adc'
  1. Root and Remount the AVD like the followings…

    C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\platform-tools>adb root
    
    C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\platform-tools>adb remount
    remount succeeded
    
    C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\platform-tools>adb shell
    eneric_x86:/ # cd system
    generic_x86:/system # cd etc
    generic_x86:/system/etc # cat hosts
    127.0.0.1       localhost
    ::1             ip6-localhost
    
    generic_x86:/system/etc # echo "192.168.1.120   ilyasmamun.blogspot.com" >> hosts
    generic_x86:/system/etc # cat hosts
    
    127.0.0.1       localhost
    ::1             ip6-localhost
    192.168.1.120     ilyasmamun.blogspot.com
    generic_x86:/system/etc #
    
Pepsinogen answered 26/12, 2018 at 9:19 Comment(2)
Props for this method over pushing the local host file to the device. That method never worked for me, but simply writing the entries I needed worked great!Harrow
It seems adb remount is broken from API 29 onwards as per this question so will have to stick to API < 29Cardiganshire
T
23

Below are the steps I followed on my Windows machine on Windows Terminal: Run the following command to know your AVDs:

emulator -list-avds

enter image description here

Run the following command to open the emulator for writable mode:

emulator -avd Pixel_XL_API_29 -writable-system -no-snapshot-load

Replace Pixel_XL_API_29 with your AVD name.

enter image description here

Ignore the warnings there.

In a new Terminal tab run the following commands:

  • adb root
  • adb shell avbctl disable-verification
  • adb reboot

enter image description here

Wait for your emulator to reboot. It can take upto 1 minute.

enter image description here

When the emulator is rebooted, run the following commands:

  • adb root
  • adb remount

You will get a remount succeeded message after that:

enter image description here

Now is the time to push our host file from Windows machine to Android's emulator

adb push D:\hosts /system/etc/

D:\hosts is the location of the hosts file present at the D drive of my Windows machine. /system/etc/ is the location in Android's emulator where we want to copy that file.

After successfull operation you will see a message like this: enter image description here

To verify that the hosts file has been pushed you can run the following commands:

  • adb shell
  • cd system
  • cd etc
  • cat hosts

You will see the contents of hosts file in the Terminal:

enter image description here

Tilly answered 18/1, 2021 at 18:17 Comment(1)
NOTE: Before running emulator -avd Pixel_XL_API_29 -writable-system -no-snapshot-load be sure all emulators are stopped (including those in Android Studio)Tourism
C
22

Here is how i was able to do it working on OSX. After reading a bunch of different instruction nothing seemd to work for me untill someone mentioned that you have a very narrow window for copying the file from your disk to the emulated device or it becomes read-only again

  1. Start your emulator.
  2. In your terminal find the folder "platform-tools" for your devices
  3. Prepare the hosts file you want to copy to your device (in my case i put it on desktop)
  4. String together a bunch of commands to copy the file quickly. This is what worked for me ./adb root && ./adb -s emulator-5554 remount && ./adb -s emulator-5554 push ~/Desktop/hosts /system/etc/hosts 'emulator-5554' is the name of my device which you can find by typing ./adb devices

after that the terminal responded with

restarting adbd as root remount succeeded [100%] /system/etc/hosts

you can veryfy the copy was successfull by ./adb shell and then cat /system/etc/hosts

I was then able to connect to my virtual hosts from the emulated device

Just to be complete here is how my hosts file looked like 10.0.2.2 my-virtual-host

I hope this helps someone as i spet quite some time trying to figure this out.

Chantell answered 16/3, 2017 at 9:3 Comment(5)
I had to split command in step 4. into two: First adb root and then adb remount && adb push. But thank you anyway - stringing commands together solved my problem I could not solve in hours...Civism
I also found that I would have to do a cold reboot of the emulator to run the command a 2nd time.Interplead
I tried it with Nougat & it worked fine, but when tried it with an emulator having Oreo, it works for just seconds, then the system removes the pushed file & reverts the system back to read-only! .. do someone know how to fix this with Oreo?Picker
After trying other methods for hours, this finally worked flawlesslyMelodramatize
Not working anymore in 2023Tourism
F
13

Another approach to this matter would be to use the adb command line tool.

  1. Make sure you have in path emulator and tools
export ANDROID_HOME="/Users/YOUR_USERNAME/Library/Android/sdk"
export PATH=$ANDROID_HOME/emulator:$ANDROID_HOME/tools:$PATH

EDIT: For Windows should something like this (instead of tools required platform-tools [or the path where adb resides])

C:\Users\YOUR_USERNAME\AppData\Local\Android\sdk\platform-tools
C:\Users\YOUR_USERNAME\AppData\Local\Android\sdk\emulator
  1. Check device name (ideally it would be to have a short name without spaces, eg. API30X86)
emulator -list-avds
  1. Then launch the emulator with the following arguments:
emulator -avd YOUR_AVD_NAME -writable-system -no-snapshot-load -no-cache
  1. Run the following commands to run as root and remount the partition system as root
adb devices  #you should see your devices
adb root
adb shell avbctl disable-verification
adb reboot
adb root
adb remount 
  1. After remount, you should be able to push the edited host file from your machine to the emulator.
adb push ~/Documents/hostsandroid /etc/hosts

Now you should be able to see your hosts file with Device File Explorer from Android Studio.
EDIT: In the case, you don’t see the Device File Explorer, you can check the Event Log if Android framework is detected. If so, click Configure and you're done. enter image description here


When you are going to run again and push new changes with a new session, you’ll only have to do:
adb root
adb remount 
adb push ~/Documents/hostsandroid /etc/hosts
Foumart answered 3/5, 2021 at 6:13 Comment(3)
Getting an error when trying to run "adb root": adbd cannot run as root in production buildsAraby
Hi @BrillPappin please check this question on SO: #25272378Foumart
The answer to the error, is that you can't do it on an emulator with Google Play installed.Araby
P
8

I was able to edit the /etc/hosts file by launching the emulator with -writable-system and remounting the emulator using adb remount. After that the hosts file inside the emulator is editable. I tried pushing/replacing the file and succeeded.

Primer answered 14/12, 2016 at 4:34 Comment(1)
This does not work if your emulator has Google play iconFerebee
A
5

Follow the below 3 steps :

  1. Start emulator in writable mode : ./emulator -avd <emulator_name> -writable-system
  2. remount : adb remount
  3. push the hosts file attached : adb push hosts /system/etc/

Note :

  1. Run one and only one emulator_name with above steps
  2. executable emulator is located within android-sdk. For me it was under sdk/emulator.
  3. Attached hosts file will resolve www.facebook.com to 127.0.0.1, hence blocks www.facebook.com on emulator.
Apiece answered 15/11, 2017 at 19:13 Comment(0)
H
5

Tedious, but effective, you can build a new hosts file, line by line within your emulator shell.

Remount Emulator

You can edit/remount your emulator (to get a writeable filesystem) in your PC/Mac/linux command line / powershell / terminal.

(Stop your emulator if it's already running, then...):

emulator -avd <avdname> -writable-system

(this starts up a new emulator with a writable file system)

Still within your PC/Mac/Linux terminal run these two commands:

adb root

adb remount

Then connect to your running emulator via a shell:

adb shell

This part below is run from inside your emulator, inside the shell connection you just made.

Give yourself root access:

su

Change directory to where the hosts file is kept:

cd /etc

To make your emulator defer to your development machine's hosts file or DNS for a given domain, add a domain entry using ip of 10.0.2.2.

Example of appending a new domain entry line to emulator hosts file:

echo '10.0.2.2 mydev.domain.com' >> hosts

This 10.0.2.2 is a special address for Android emulators. It will proxy DNS requests for that domain to your development machine. So whatever IP address your PC/Mac/Linux machine hosts file lists for mydev.domain.com, the Android emulator will use it.

You can now exit the root shell & your emulator shell:

exit
exit

(1st gets you out of su. 2nd exits from the emulator shell, dropping you back into your development machine's terminal).

You're done. You can open up a web browser inside your emulator, type the domain you just added to hosts into the address bar and check the emulator is routing that domain properly.

Remount failed

If you're on Android emulator 29+ and getting

remount failed

when calling adb remount, check out the workaround by Kidd Tang here.

Hanselka answered 28/11, 2020 at 21:3 Comment(0)
W
5
  1. First find your system hosts file and copy it to desktop

  2. Then create a virtual device Nexus 5 with system image Nougat x86_64 Android 7.1.1 (non Google API version) once it is created

  3. Then goto /Android/sdk/emulator in terminal then run the below code please add your own device name below as mine was Nexus_S_API_25 =>

  • ./emulator -writable-system -netdelay none -netspeed full -avd Nexus_S_API_25
  1. After that Open a new terminal and goto this location /Android/sdk/platform-tools
  • then run
  • ./adb root
  • ./adb remount
  • ./adb push ~/Desktop/hosts /system/etc/hosts - (It will copy your Desktop/hosts file and paste it into your emulator hosts file which is /system/etc/hosts)
  1. That's it your emulator hosts file is updated Now if you want to re-check then run the below code
  • ./adb shell
  • cat /system/etc/hosts (it will show you the emulator hosts file)
  1. Restart the emulator to see the changes
  • ./adb reboot
Winery answered 2/4, 2021 at 13:47 Comment(0)
K
1

You can use the ADB Shell to edit the file by changing the access (Read Only to RW)

Kaylee answered 13/12, 2016 at 10:0 Comment(5)
I tried using chmod inside shell still it returns Read-only file system. But when I input whoami in shell it returns root.Primer
@BradlyMan Not chmod, try remounting the filesystem in RW mount -o rw,remount,rw <path>Kaylee
tried inputing command "mount -o,remount,rw /etc/hosts" and "mount -o,remount,rw /etc", returns mount:'etc/hosts/ not in /proc/mountsPrimer
You must do this on the filesystem not on the hosts file su mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /systemKaylee
it returns su: failed to exec -o: Permission deniedPrimer
C
1

Try @P.O.W answer,

Make sure you have a blank line after the last entry of the hosts file If you use tabs in the hosts file, replace them with spaces Restart Android and try again:

adb reboot
Creatine answered 27/7, 2018 at 5:26 Comment(0)
P
1

Here's what worked for me on my Visual Studio for Mac setup. Make a hosts file, set your custom ip/domain record, and include blank line at the end which i can't get to show up here.

    127.0.0.1       localhost
    ::1             ip6-localhost
    <ipgoeshere>    local.example.com
    

Create an android emulator with Google Api's (not Google Play) with API28 or lower. Otherwise when running ./adb you'll get "adbd cannot run as root in production builds"

Open a terminal and change directories

cd /Users/<usernamehere>/Library/Developer/Xamarin/android-sdk-macosx/tools

Launch emulator with below to allow writable system. You can find your avd name by VS4Mac - Tools - Device Manager - Cog Wheel - Reveal in Finder.

./emulator -avd <youravdnamegoeshere> -writable-system

Open another terminal and run the following commands

cd /Users/<usernamehere>/Library/Developer/Xamarin/android-sdk-macosx/platform-tools
./adb root
./adb remount

Check existing hosts file

./adb shell
cat /etc/hosts
exit

Move your custom hosts file

./adb push "/Users/<usernamehere>/Projects/hosts" /etc/hosts

Confirm ping now works

./adb shell
ping local.example.com

Will always need to start the emulator via command line to keep hosts working.

Printer answered 31/10, 2023 at 3:56 Comment(0)
W
0

To preserve the changes to the hosts file you need to always start the emulator with the "-writable-system -no-snapshot-load". Otherwise, the default system image will be used, which has the default hosts file.

Wandering answered 5/10, 2023 at 12:22 Comment(0)
M
0

I'm using macOs and finally I have created a script to make easy this task. I share it with the community in case it helps someone. Feel free to fork and customize for your needs.

https://github.com/goltra/android-hosts/

Any suggestions would be welcome

Mcanally answered 22/3 at 7:53 Comment(0)
P
-3
place all these export in z shell using terminal

vim ~/.zshrc     press enter

then zshell will open

then press i 

past all the export (verify the path i have used all default location for instalation)

then press esc

then press this :wq!  

press enter 

close terminal and open it again


export PATH="$PATH:$HOME/Dev/flutter/bin"
export GEM_HOME=$HOME/.gem
export PATH=$GEM_HOME/bin:$PATH

export ANDROID_HOME=$HOME/Library/Android/sdk
export ANDROID_SDK_ROOT=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools



only use google apis image do not usese play image

u will get list of avds 
emulator -list-avds

emulator -avd Nexus_5_API_29 -writable-system  (do not close terminal) (open a new terminal)

adb root
adb remount

copy mac host file to Downloads  from  /private/etc/hosts

adb push Downloads/hosts /system/etc/hosts
adb reboot  
Poulin answered 6/5, 2020 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.