经常去面试会被人问到一个问题,上网的时候域名解析是怎么样解析的。
我通常会说,IE首先读取IE cache里面的内容,然后读取hosts文件,最后是通过DNS服务器进行查询。
因为很多人就会碰到下面的问题,我在hosts文件里面添加里记录,为什么用nslookup去解析的时候却解析不到hosts里面的记录呢。
先来看看我的hosts文件以及nslookup的结果。
# Copyright (c) 1993-1999 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
127.0.0.1 localhost
192.168.1.1 www.baidu.com
=================================================================
下面是运行nslookup的结果:
从上面可以看到,我nslookup解析到的是正确的IP地址(公网IP),但是我ping www.baidu.com,收到的回包是来自hosts文件里面指向的IP地址。
为什么会这样呢,原因就是:当你运行nslookup的时候,Windows查询的是DNS服务器,而不会去查询hosts文件。但是Windows实际连接的会是hosts文件里面的记录。
转载于:https://blog.51cto.com/jeromexiong/898172