Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

解決 Vista/Win7 修改 hosts 檔不生效:停用 DNS Client 服務即時生效

編輯:Windows Vista 常見問題解答

修改 C:\Windows\System32\drivers\etc\hosts 封鎖惡意網域,卻發現不生效。主因是「DNS Client」服務快取了舊解析結果,且 ipconfig /flushdns 有時無效。停用該服務並設為手動,即可讓 hosts 修改即時生效。

hosts 檔案基礎

  • 位置:%windir%\System32\drivers\etc\hosts
  • 格式:IP 空白 域名 (如 127.0.0.1 ad.example.com)
  • 編碼:必須為 ANSI/ASCII,**不可為 Unicode**,否則系統不讀取。
  • 本機回環:預設 127.0.0.1 localhost

問題根因:DNS Client 服務快取

「DNS Client」 (Dnscache) 服務在本地快取 DNS 解析結果 (含 hosts),優先回應快取,導致 hosts 修改延遲生效甚至完全不生效。

解決方案:停用 DNS Client 服務

  1. 控制台 → 系統管理工具 → 服務 (services.msc)。
  2. 找到 DNS Client (顯示名稱可能為「DNS 用戶端」)。
  3. 右鍵 →「內容」 →「啟動類型」改為 手動 →「停止」服務 → 確定。

之後修改 hosts 檔案,**即刻生效**,無需刷新快取。

影響與權衡

  • 優點:hosts 即時生效,適合頻繁調整封鎖清單的開發/測試環境。
  • 缺點:失去本地 DNS 快取,每次解析均發起網路查詢,可能略微增加解析延遲 (通常無感知)。
  • 不適用場景:依賴本地快取效能的高頻 DNS 查詢環境、企業強制原則禁止停用服務的電腦。

替代方案 (不停用服務)

  • ipconfig /flushdns (管理員 CMD) → 清除快取,但 Vista/Win7 有時無效。
  • net stop dnscache && net start dnscache 重啟服務強制刷新。
  • PowerShell:Clear-DnsClientCache (Win8+/Server 2012+)。
Copyright © Windows教程網 All Rights Reserved