Detailed steps for modifying hosts file in Android

preface

During development, we may need to modify the Android hosts file. The path of Android hosts file is / system / etc / hosts. Before modifying this file, the Android phone needs to obtain root permission. As for how to root your mobile phone, it is not detailed here. You can find it on the network by yourself. Many are also very simple.

This article will explain how to access the root mobile phone through the ADB program under the command line, drag the hosts to the computer for modification, and then copy it back to the mobile phone to realize the method of modifying the hosts.

Here are the specific steps:

The above command is to drag the hosts file on the mobile phone to the computer, [100%] indicates that the file has been transferred, and the hosts file can be modified. After the modification is completed, use the following command to upload to the mobile phone:

It can be seen from the echoed message that the file system is read-only, so it cannot be uploaded directly.

Try the following command to see if it works?!

Still not. It seems that you need to mount the / system directory again.

No permission? This is the rhythm of sacrificing the shell Dharma!

As can be seen from the last line above, only its owner can write the hosts file, which is read-only to others. If you want others to make changes, you must use the following command to raise the right, and then change the properties of the hosts file.

If you execute this command for the first time, the phone will light up and the supersu application will prompt you whether you agree to the permission assignment. Of course, we have to agree here! Then you can see that the prompt under the terminal changes from $to #, and the character before @ changes from shell to root. Then we can modify the permission attribute of the hosts file.

Read only file system again! Enter the following command to see the file system details of the / system directory.

See the RO behind ext4? It is the abbreviation of read only, which means read-only. This indicates that the / system directory is read-only. Next, we'll change it to read and write.

The - O above specifies options when loading the file system. These options include:

If you look again, you can see that the original ro position has become RW.

However, we can't copy the hosts file to the mobile phone, because the permission attribute of the hosts file has not been changed. If you upload it forcibly, you will get the following error message:

Run the following command:

Then look at the properties of the hosts file.

You can see that everyone can read and write the hosts file.

Now that everything is ready, try uploading the modified hosts file again.

What's the matter? Why did the copy fail? Haven't you changed the file system to read and write?

And in the process of finding out the cause, I found a strange thing. In root mode, the / system directory is read-write, but in shell mode, the / system directory is read-only.

Moreover, after the / system directory is mounted to read / write in the terminal emulator on the mobile phone, the status of / system is still displayed as read-only in the root mode of ADB shell.

Looking and searching on the Internet, I was almost desperate, and finally found the possible problem. This is the post: Mount in shell as user or root with different output. One answer mentioned something like Mount namespace. Then I know:

A mount namespace is the set of filesystem mounts that are visible to a process.

The mount point of each process is invisible to other processes. The mount point after mount in the terminal emulator belongs to the process, while the mount points of shell mode and root mode in ADB shell belong to their own processes respectively. This is the reason why the / system directory is still displayed as read-only in shell mode after being changed to read-write in root mode.

I know the cause of the problem. How to solve it?

There is an option of Mount namespace separation in the settings of supersu application, as shown in the following figure:

Uncheck it, and then the mount point is global and is no longer unique to their processes. However, remember that this modification is only effective after restarting the phone.

The following is the upload result after canceling the mount namespace separation:

You can see [100%] echo, indicating that the file has been uploaded.

To view the properties of the hosts file:

The file size has changed from 25 to 137679, indicating that the file has been replaced.

The following is to restore the phone to its original state:

At this point, the work of modifying the hosts file is complete.

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>