用命令行编辑 Windows 的 hosts 文件

以管理员身份用记事本打开 hosts 文件,或用 Add-Content 直接追加条目。

Windows文件DNS管理员Hosts 文件中风险
运行 `Start-Process notepad "$env:SystemRoot\System32\drivers\etc\hosts" -Verb RunAs`,在弹出的 UAC 提示中确认后即可编辑 hosts 文件。

01命令

命令
$ Start-Process notepad "$env:SystemRoot\System32\drivers\etc\hosts" -Verb RunAs

02作用说明

hosts 文件会在查询 DNS 之前把域名映射到指定 IP,常用于测试新服务器或在本地屏蔽某个网站。它是受保护的系统文件,必须以管理员身份修改;上面的命令会先弹出 UAC 提示再用管理员权限打开记事本。不想打开编辑器时,也可以在管理员终端运行 `Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value "93.184.216.34 example.com"` 直接追加一行。

03风险

风险中风险
使用条件Administrator elevation required (the command triggers the UAC prompt).
!注意: 写错条目会导致网站无法访问。每行一条映射,格式为 `IP 域名`,改完后运行 `ipconfig /flushdns` 让其生效。

04撤销

撤销
$ Remove the added lines from the hosts file to restore default name resolution.

05来源

  • Reg - Read and edit the registry
    SS64 · technical · 2026-09-17
    依据: reg query syntax for reading registry values such as ProxyEnable under HKCU Internet Settings.
  • Get-NetConnectionProfile
    Microsoft Learn · vendor-doc · 2026-09-17
    依据: Returns the network category (Public/Private/DomainAuthenticated) of each active connection.

最后验证: 2026-09-18 · 分类: 网络

06相关方案