English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Preface
When developing, we may need to modify the Android hosts file. The path to the Android hosts file is/system/etc/hosts, it is necessary for an Android phone to obtain root permission before modifying this file. As for how to root your phone, this will not be elaborated here, and you can search for it on the Internet on your own, as there are many resources and it is quite simple.
This article will elaborate on how to access a rooted phone through the adb program in the command line, drag the hosts to the computer for modification, and then copy it back to the phone to achieve the modification of hosts.
Now let's start with the specific steps:
C:\tools>adb pull /system/etc/hosts hosts.mod [100%] /system/etc/hosts
The above command is to drag the hosts file on the phone to the computer, [100%] indicates that the file has been transferred and it is possible to modify the hosts file. After the modification is complete, use the following command to upload it to the phone:
C:\tools>adb push hosts.mod /system/etc/hosts adb: error: failed to copy 'hosts.mod' to '/system/etc/hosts': Read-only file system
From the echoed message, we can see that the file system is read-only, so it cannot be uploaded directly.
Try the following command to see if it can be successful?!
C:\tools>adb root # The help document says that this command can make the adbd daemon obtain root permissions C:\tools>adb push hosts.mod /system/etc/hosts adb: error: failed to copy 'hosts.mod' to '/system/etc/hosts': Read-only file system
It still doesn't work, it seems that we need to remount again/only the system directory can.
C:\tools>adb remount remount failed: Operation not permitted
No permission? This is the time to invoke the shell magic!
C:\tools>adb shell * daemon not running. starting it now on port 5037 * * daemon started successfully * shell@maguro:/ $ ls -al /system/etc/hosts -rw-r--r-- root root 25 2013-08-14 07:00 hosts
From the last line above, it can be seen that the hosts file can only be written by its owner, and it is read-only for others. To allow others to make modifications, you must use the following command to elevate privileges, and then change the attributes of the hosts file.
shell@maguro:/ $ su
If this command is executed for the first time, the phone will light up, and the SuperSU application will prompt you whether to agree to the allocation of permissions. Of course, you should agree! Then you can see the prompt in the terminal change from $ to #, and the character before @ changes from shell to root. Then we can modify the permission attributes of the hosts file.
root@maguro:/ # chmod +666 /system/etc/hosts Bad mode root@maguro:/ # chmod 666 /system/etc/hosts Unable to chmod /system/etc/hosts: Read-only file system
It's Read again-only file system! Enter the following command to see/What are the details of the file system in the system directory?
root@maguro:/ # mount | grep system /dev/block/platform/omap/omap_hsmmc.0/by-name/system /system ext4 ro,seclabel,relatime...
Seeing ext4Did you see the 'ro'? It is the abbreviation for 'read only', meaning read-only. This indicates/The system directory is read-only. Next, we need to change it to be writable.
root@maguro:/ # mount -o rw,remount /system
The above-o is used to specify options when loading the file system. These options include:
Check again, and you can see that the original ro has been changed to rw.
root@maguro:/ # mount | grep system /dev/block/platform/omap/omap_hsmmc.0/by-name/system /system ext4 rw,seclabel,relatime...
However, we cannot copy the hosts file to the phone at this point, because the permission attributes of the hosts file have not been changed yet. If you force the upload, you will get the following error message:
C:\tools>adb push hosts.mod /system/etc/hosts adb: error: failed to copy 'hosts.mod' to '/system/etc/hosts': Permission denied
Run the following command:
root@maguro:/ # chmod 666 /system/etc/hosts
Then check the attributes of the hosts file.
root@maguro:/ # ls -al /system/etc/hosts -rw-rw-rw- root root 25 2013-08-14 07:00 hosts
You can see that everyone can read and write the hosts file.
Since everything is ready, let's try to upload the modified hosts file again.
C:\tools>adb push hosts.mod /system/etc/hosts adb: error: failed to copy 'hosts.mod' to '/system/etc/hosts': Read-only file system
What's going on, why is the copy still failing? Isn't the file system already changed to read and write?
And during the process of finding the cause, I also found a strange thing. In root mode/The system directory is read and write, but in shell mode/However, system is still read-only.
root@maguro:/ # mount | grep system /dev/block/platform/omap/omap_hsmmc.0/by-name/system /system ext4 rw,seclabel,relatime... root@maguro:/ # exit shell@maguro:/ # mount | grep system /dev/block/platform/omap/omap_hsmmc.0/by-name/system /system ext4 ro,seclabel,relatime...
And in the Terminal Emulator on the phone/After the system directory is mounted as read and write, check in adb shell in root mode/The status of system still shows as read-only.
I searched and searched and searched, almost desperate, and finally found the possible cause of the problem. It is this post:Mount in shell as user or root with different output. There was a reply mentioning this mount namespace. Then I realized:
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 Terminal Emulator belongs to the process itself, while the mount points in shell mode and root mode in adb shell belong to their respective processes. This is why the modification in the previous root mode was/The reason why the system directory is still displayed as read-only in the shell mode after it is set as writable.
Now that we know the cause of the problem, how do we solve it?
There is an option for mount namespace separation in the settings of the SuperSu application, as shown in the following figure:
Uncheck the box, and then the mount point is global, no longer exclusive to the process that mounts them. However, remember that this modification will only take effect after restarting the phone.
The following is the result of uploading after canceling mount namespace separation:
C:\tools>adb push hosts.mod /system/etc/hosts [100%] /system/etc/hosts
You can see [100%] echo, indicating that the file has been uploaded successfully.
Check the properties of the hosts file:
root@maguro:/ # ls -al /system/etc/hosts -rw-rw-rw- root root 137679 2017-02-16 00:20 hosts
The file size has changed from25has changed to137679indicates that the file has been replaced successfully.
The following is the process of restoring the phone to its original state:
root@maguro:/ # chmod 644 /system/etc/hosts root@maguro:/ # ls -al /system/etc/hosts -rw-r--r-- root root 137679 2017-02-16 00:20 hosts root@maguro:/ # mount -o ro,remount /system
The work of modifying the hosts file is now complete.
Summary
That's all for this article. I hope the content of this article can bring some help to everyone's learning or work. If you have any questions, you can leave a message for communication.
Declaration: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.