Hosts 檔案是本機 **靜態 DNS 解析表**,優先順序高於 DNS 伺服器。善用它可實現:網站封鎖、開發測試域名對應、內網服務別名、廣告/追蹤器阻擋、惡意軟體防禦。本文涵蓋位置、語法、編輯技巧、進階應用與防竄改。
| 作業系統 | 路徑 |
|---|---|
| Windows 10/11, 8, 7, Vista, 2000, XP, Server 2003+ | C:\Windows\System32\drivers\etc\hosts |
| Windows 95/98/ME | C:\Windows\hosts |
| Linux / macOS / Unix / BSD | /etc/hosts |
| Android (Root) | /system/etc/hosts |
| iOS (越獄) | /etc/hosts |
IP位址 主機名稱 [別名1 別名2 ...] # 註解
# 開頭或行尾為註解,不解析。api.example.com)、萬用字元**不支援** (需寫死完整 FQDN)。# 區域網路伺服器別名
192.168.1.10 fileserver fs nas.local
192.168.1.11 dbserver db
# 開發測試:將正式域名導向本機/測試機
127.0.0.1 api.myapp.com
127.0.0.1 static.myapp.com
::1 api.myapp.com
# 封鎖廣告/追蹤/惡意網站 (導向本機或 0.0.0.0)
0.0.0.0 ads.googleanalytics.com
0.0.0.0 pagead2.googlesyndication.com
127.0.0.1 tracking.example.com
# 強制 Google 搜尋使用特定 IP (繞過 DNS 污染)
142.250.190.46 www.google.com
notepad → 右鍵 以系統管理員身分執行 → Ctrl+O 開啟 hosts 檔。chrome://net-internals/#dns → Clear host cache)。service.namespace.svc.cluster.local 別名,方便除錯。# 群組原則 (gpedit.msc)
電腦設定 → Windows 設定 → 安全性設定 → 進階稽核原則設定 → 物件存取 → 稽核檔案系統 → 成功/失敗
# 然後對 hosts 檔案右鍵 → 內容 → 安全性 → 進階 → 稽核 → 新增 → Everyone → 類型:所有 → 勾選「寫入資料/新增檔案」
事件檢視器 Security 日誌 Event ID 4663 即可追蹤竄改者。
chrome://net-internals/#dns)、系統 DNS 快取 (ipconfig /flushdns)、代理伺服器/VPN 繞過本機解析、檔案編碼非 UTF-8/ANSI (需另存為 ANSI)。Hosts 檔案雖小,卻是網路流量的「第一道關卡」。善用它,能省去 DNS 查詢延遲、阻擋惡意連線、加速開發除錯,是每位高階使用者與運維人員的必修技能。