無論是 Windows 主機或 Linux 主機都有個 hosts 檔案,裡面定義著讓本機使用的 Domain Name <--> IP 對應表,任何程式在進行網路連線時通常會進行 DNS 伺服器進行 IP 的查詢,不過在查詢之前都一定會先來 hosts 檔查詢是否已經有定義過該 Domain Name 的對應 IP,如果沒有的話才會正式進行 DNS 查詢。
在 Windows 下通常 hosts 檔的路徑如下:(winXP/win7皆相同)
C:\WINDOWS\system32\drivers\etc\hosts
複製代碼
在 Linux 下通常 hosts 檔的路徑如下:
/etc/hosts
複製代碼
在 Windows 下的 hosts 檔案格式與 Linux 下的 hosts 檔案格式完全相同,以 Windows 為例,通常預設的內容如下:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.