2023年12月7日发(作者:)

怎么使用bat脚本更改本地受保护的hosts文件

想更改hosts文件添加域名与地址ip、由于hosts文件受保护并且是只读属性。因此需要先赋予权限,并取消只读属性才能正常写入

/E 编辑 ACL 而不替换。

/G    赋予指定用户访问权限。

F    完全权限。

Administrators、SYSTEM、Users(是winds用户组需要什么加什么)

attrib 是控制文件属性的

  + 添加文件属性

- 删除文件属性

R 只读文件属性

A 存档文件属性

S 系统文件属性

H 隐藏文件属性

I 无连接属性

@echo

@echo 127.0.0.1 >>E:thosts(这是我们要写入的内容)

虽然脚本比较笨拙,但是亲测有效,若是找不到好的方法可以选择试一下O(∩_∩)O

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

1、新建记事本输入一下内容

bat脚本代码如下:

echo Y|cacls C:WindowsSystem32driversetchosts /E /G Administrators:F

echo Y|cacls C:WindowsSystem32driversetchosts /E /G SYSTEM:F

echo Y|cacls C:WindowsSystem32driversetchosts /E /G Users:F

attrib -r -h C:WindowsSystem32driversetchosts

@echo

@echo.>>C:WindowsSystem32driversetchosts

@echo 127.0.0.1 >>C:WindowsSystem32driversetchosts

attrib +r +h C:WindowsSystem32driversetchosts

2、将记事本后缀名改为.bat格式的文件,然后双击即可完成修改