Command line modify file folder access permissions cacls, modify the Hosts content method

Source: Internet
Author: User


In the process of daily use of Windows, the hosts (C:\Windows\System32\drivers\etc) file may be changed by some programs, so on the internet to find the forbidden/allow to modify the Hosts file Bat script, but in the use of the process encountered a new trouble , the solution to the related problem is recorded.

Disable/Allow modification of the Hosts file's bat script
Hosts is a system file without extension, can be opened with Notepad and other tools, its role is to use some of the common URL domain name and its corresponding IP address to establish an association "database", when the user in the browser to enter a need to log in the URL, The system will automatically find the corresponding IP address from the Hosts file, once found, the system will immediately open the corresponding Web page, if not found, the system will submit the URL to the DNS domain name resolution server for IP address resolution.

Open Notepad and enter the following code as needed:
1. Prohibit modification of the Hosts file:

echo Y|cacls%windir%\system32\drivers\etc\hosts/g everyone:r

2. Allow the Hosts file to be modified:
echo Y|cacls%windir%\system32\drivers\etc\hosts/g everyone:f

Save As. BAT format after double-click to run, of course, can also be entered in CMD.
Note: The above content from Baidu know- how to prevent the hosts from being modified
New issues encountered-CACLS access denied

After executing the above " prohibit modify Hosts file " script, when you want to modify the Hosts file, do " allow to modify the Hosts file " file, sometimes encountered "cacls Access Denied" , resulting in the inability to get permission to modify the Hosts file.


Finally, in the command line to modify the file/folder access permissions cacls, modify the hosts content method found in the workaround. The contents are as follows:

One careless, will move the file permissions of the hard disk to make a change, but then quite smooth, and then add back on the line.

However, a little bit of a problem, after the inquiry and experiment, finally solved.

The problem is described as follows: A folder of permissions or can not allow me to use normal, at this time, I only need to open the Properties dialog box, click on the Security tab, click on the "Advanced" in the inside to add permissions, OK, then select a user from the security, add permissions, the basic can be resolved. However, this time is a bit tricky, I changed a computer, click the Properties dialog box, unexpectedly find the "security" tag, do not know whether it is the setting of the problem or because I am the reason for the mobile drive. Anyway, I can't use this folder until 1:30 in the moment.

FIX: Google down, decide to add the folder permissions with the command line method.

Now has been successful, the process is recorded, one to facilitate their own again encountered the same can be consulted, and secondly, if there are netizens with me the same problem, also can do a reference.

More and more users are using the NTFS file system to enhance the security of Windows systems. File or directory Access control permissions are typically set in the Security tab of the graphical user interface (GUI). There is also the use of the cacls command, which is a command line command

The CACLS command uses the following format:
CACLS filename [/T] [/E] [/C] [/g user:perm] [/R user [...]] [/P User:perm [...]] [/d User [...]]
filename--Displays the Access Control List (hereinafter referred to as ACLs);
/t--changes the ACL of the specified file in the current directory and all its subdirectories;
/e--edit an ACL without replacing it;
/c--continues when an access denied error occurs;
/g user:perm--gives the specified user access rights. Perm can be R (read), W (write), C (change, write), F (Full Control);
/R user--Revoke the access rights of the specified user (only for use with/E);
/p user:perm--Replace the access rights of the specified user;
/d user--denies access to the specified user.

1. viewing directories and ACLs
Use the cacls command to view the C-drive Windows Directory access control permissions.

Click "Start → run", run cmd, switch to c root directory, execute "cacls windows" in the window will list the user Access control permissions for Windows directory items. If you want to view all file access control permissions in the WINDOWS directory, enter cacls windows\. "Command.
2. modifying directories and ACLs
If you want to modify access permissions for directories and files, use the command "cacls windows/t/e/c/g administrator:f" to give Administrator Full control of the WINDOWS folder.
If you want to replace user access rights: Use the command "cacls windows/t/e/c/P administrator:r"
If you want to revoke user access: Use the command "cacls windows/t/e/c/R Administrator"
If you want to deny user access: Run "cacls windows/t/e/c/D Administrator".

After reloading the system again, the latest IdM was installed, and the hosts file was modified according to the online method, so that IDM could not contact the server for verification.

After the modification, worried about the IdM to modify the hosts content, so as to crack the failure, so with the cacls command to modify the permissions of the hosts he didn't know it.

Another day, want to modify the contents of the hosts to do some testing, but there is no way to change, open or not, directly to the Access Isdenied,cmd command copy also does not, execute the cacls command again, give administrator all permissions also failed. Totally screwed up. It's really not the mother who knows it.

Finally, refer to StackOverflow Last statement, modify success. Here PO out for the vast number of users reference.


CMD Right-click Run as Administrator


takeown/f C:\Windows\System32\drivers\etc\hosts && icacls c:\windows\system32\drivers\etc\hosts/grant Administrators:f

Then notepad++ Open, success.

In reference to the original, the complete command is--

takeown/f C:\Windows\System32\drivers\etc\hosts && icacls c:\windows\system32\drivers\etc\hosts/grant Administrators:f
Attrib-r-h-s C:\Windows\System32\drivers\etc\hosts
%windir%\notepad.exe C:\Windows\System32\drivers\etc\hosts

Command line modify file folder access permissions cacls, modify the Hosts content method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.