DNS 相关知识

336 阅读2分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

简介

DNS 相关的知识十分重要,可能在平时开发过程中用到的比较少,但是如果需要配置域名、路由等,或者一旦出了域名解析相关问题,这些知识是非常重要的

DNS 解析的过程

  1. 本地浏览器缓存是否有存,ip和域名的映射关系
  2. 本地 hosts 文件是否有存映射关系
  3. 本地 DNS 解析记录是否有缓存
  4. 本地 DNS 服务器是否有缓存,一般是公司内部的 DNS 服务
  5. 如果本地 DNS 服务器不能查询到,就会根据它设置的转发,向上一级 DNS 服务器查询,直到根 DNS 服务器

查看 DNS 解析情况

DNS 解析出问题可能是本地的 DNS 服务器的问题,可以更换下配置的 DNS 服务器地址

查询本机的 DNS 服务器地址 cat /etc/resolv.conf

➜  ~ cat /etc/resolv.conf
#
# macOS Notice
#
# This file is not consulted for DNS hostname resolution, address
# resolution, or the DNS query routing mechanism used by most
# processes on this system.
#
# To view the DNS configuration used by this system, use:
#   scutil --dns
#
# SEE ALSO
#   dns-sd(1), scutil(8)
#
# This file is automatically generated.
#
nameserver xx.xx.xx.xx

查询某一个域名目前解析记录 nslookup xxx.com

  ~ nslookup baidu.com
Server:		10.8.8.18
Address:	10.8.8.18#53

Non-authoritative answer:
Name:	baidu.com
Address: 220.181.38.148
Name:	baidu.com
Address: 220.181.38.251

查询整个 DNS 解析的过程 dig +trace +nodnssec xxx.com

➜  ~ dig +trace +nodnssec baidu.com

; <<>> DiG 9.10.6 <<>> +trace +nodnssec baidu.com
;; global options: +cmd
;; connection timed out; no servers could be reached

正常情况下会显示出整个的解析过程,类似 image.png

本机 DNS 解析出问题了怎么办

在本地 DNS 服务器出问题的时候,可以用公共的 DNS 服务做解析,在网络设置里设置本机的 DNS 解析地址