Hosts 檔案是作業系統將主機名稱對應至 IP 位址的本地查詢表,優先順序高於 DNS。它不僅是網路基礎設施的關鍵元件,也是廣告攔截、惡意網站封鎖、開發測試環境切換、繞過區域限制的實用工具。本文系統整理其歷史、位置、用途與修改技巧。
ARPANET 早期無中心化 DNS,各節點自維護 Hosts 對照表。隨網路規模擴大,管理困難催生了 DNS 系統,但 Hosts 保留為本地覆寫機制至今。
| 作業系統 | 路徑 |
|---|---|
| Windows 95/98/Me | C:\Windows\hosts |
| Windows NT/2000/XP/2003/Vista/7/8/10/11 | C:\Windows\System32\drivers\etc\hosts |
| Linux / Unix / macOS | /etc/hosts |
| Android (需 Root) | /system/etc/hosts |
127.0.0.1 localhost
::1 localhost
127.0.0.1 或 0.0.0.0,減少頻寬與干擾。127.0.0.1 ads.example.comdev.local 指向本機或測試伺服器 IP,無需修改 DNS 即可驗證網站。C:\Windows\System32\drivers\etc\hosts。notepad %windir%\System32\drivers\etc\hosts
同上需以管理員身分執行 CMD。
Add-Content -Path "$env:SystemRoot\System32\drivers\etc\hosts" -Value "`n127.0.0.1 block.example.com" -Force
ping block.example.com
# 應回傳 127.0.0.1
ipconfig /flushdns # 清除 DNS 快取確保立即生效