DNS简明教程

简介: 在我看来,DNS(域名系统)是互联网的核心。我始终认为,控制了DNS就等于控制了网络世界。下面我们就来深入了解DNS。

本文翻译自我的英文博客,最新修订内容可随时参考:DNS简明教程

在我看来,DNS(域名系统)是互联网的核心。我始终认为,控制了DNS就等于控制了网络世界。下面我们就来深入了解DNS。

主机映射表(Hosts)

早期访问计算机需要记住IP地址,但这既难记忆又不便维护(IP变更时需通知所有人)。于是人们创建了一个文件,将计算机名称映射到IP地址,并在所有设备上存储该文件,同时通过一台特定计算机维护更新,这个文件就是hosts(例如:timerring: 88.88.88.88)。这便是ARPANET的起源。

随着互联网发展,IP数量激增,hosts文件变得庞大且容易出现命名冲突,DNS应运而生。

DNS 核心原理

1983年,Paul Mockapetris提出域名系统(DNS),这是一种将域名映射到IP地址的分布式数据库。如今访问网站时,只需向DNS服务器查询域名,即可获取对应IP,不再依赖本地hosts文件(现代计算机的hosts文件通常为空)。

DHCP动态分配

DNS服务器的IP可能是动态的(如家庭宽带),每次联网时由网关通过DHCP(动态主机配置协议)分配。可在Linux系统的/etc/resolv.conf文件中查看当前DNS服务器IP。

DNS协议规则

为确保域名唯一,DNS采用类似现实地址的层级结构(如home.google.com)。

域名层级

  1. 根域名(root domain):以.表示(通常省略),如math.stackexchange.com.末尾的点。
  2. 顶级域名(TLD):如.com.org
  3. 二级域名(SLD):用户可注册的域名,如google.com
  4. 主机名(host):用户自定义的主机标识,如www

域名资源记录(Resource Record)

每条域名在DNS中对应一条记录,格式为:

域名 生存时间 协议类型 记录类型 值
  • 记录类型(Type)
    • A:IPv4地址记录(如google.com -> 142.250.78.14)。
    • AAAA:IPv6地址记录。
    • NS:名称服务器记录,指向该层级域名的权威服务器(如.com的NS记录指向.com域的服务器)。
    • MX:邮件交换记录,指定邮件接收服务器(如github.com的MX记录指向其邮件服务器)。
    • CNAME:别名记录,将一个域名指向另一个域名(如blog.timerring.com可能是timerring.github.io的CNAME)。
    • PTR:反向解析记录,将IP映射回域名(用于验证IP所属域名)。

DNS服务器架构

DNS通过域名分区(DNS Zone)实现分布式管理,每个分区有主服务器和从服务器(备份并加速查询),统称为权威服务器(Authoritative Name Servers)。分区存储两类记录:

  1. 本分区的域名资源记录(如google.com的A记录)。
  2. 父级和子级域名的NS记录(用于层级查询)。

根域名服务器(.) 是DNS的基础,全球共有13组根服务器,可通过root-servers.org查看详情。

eg.

Now every zone has a master server and many slave servers to backup and expedite the query. These servers are called Authoritative Name Servers.

And it save the two types of records:

  • This zone's domain name resource records.
  • This zone's parent DNS and sub-DNS server records(mainly NS records).

域名解析流程

连接网络时,设备会获取默认DNS服务器(运营商提供或手动设置的公共DNS,如8.8.8.8)。解析过程如下:

  1. 本地缓存优先:浏览器和操作系统先检查缓存,若有记录直接返回IP。
  2. 递归查询:若缓存无记录,本地DNS服务器向根服务器查询,根服务器返回顶级域名服务器地址(如.com的NS记录),依次递归直至获取目标IP(如www.google.com的解析路径:. -> com -> google.com -> www.google.com)。

缓存机制

为提升效率,浏览器、操作系统和DNS服务器都会缓存解析结果,缓存时间由记录中的Time_to_live(TTL)决定:

  • 稳定域名(如google.com)的TTL较长(通常数小时至数天)。
  • 动态变更的域名(如临时测试域名)的TTL较短(几分钟至几十分钟)。

DNS泄露与防护

DNS泄露是指DNS请求以明文传输,途经的中间节点可获取用户访问的域名。即使使用代理,未匹配规则的域名仍可能触发本地DNS请求(如浏览器通过WebRTC发送UDP探测包)。

检测工具

防护方法

  • DoH(DNS over HTTPS):通过HTTPS加密DNS请求(端口443)。
  • DoT(DNS over TLS):通过TLS加密DNS请求(端口853)。
  • 全局代理模式:将所有流量加密后发送至远程节点处理(如Clash的Fake IP模式)。

完整访问流程示例

以访问google.com为例,整个过程涉及OSI模型的多层协作:

To begin with, you should know the OSI model. I have drawn a good image to explain it. You can also read my previous blog Real Computer Network.
OSI model

So just imagine the process of go surfing the internet. eg. you query google.com on your browser, and then you get the page, what happened?

In the common case, when you purchase the broadband, the operator will provide you a Fiber - optic Modem, and you will buy a router. The router connects to the internet via PPPoE and get the public WAN ip (in fact still a intranet ip) and the DNS servers ip (common two DNS servers). Your router is as the gateway of your local network, so it will has own local network ip, and it will allocate the ip and DNS server ip to your devices through DHCP(commonly the DNS ip and the gateway ip are all the router's local ip).

  1. Your browser first checks the browser's cache to see if it has the ip of google.com, and then checks the OS's cache(include the host file if there is the mapping relationship). If there isn't, it will send a DNS request.(eg. tell me the ip of google.com)
  2. In the transport layer, the source port is eg.222 and the default destination DNS port is 53.
  3. In the network layer, the source ip is your computer's ip 192.168.1.10 and the destination ip is the DNS server's ip 8.8.8.8. But the DNS ip you cannot find locally, so you need to send the DNS request to the gateway you connect. And because the communication via MAC address in the same network, so it will be processed in data link layer.
  4. In data link layer, the source MAC address AA-AA-AA-AA is your computer's MAC address and the destination MAC address CC-CC-CC-CC (get through ARP protocol) is the gateway's MAC address.
  5. Then it will be sent through NIC and in the cable.
  6. The switch will receive the packet and forward it to the gateway(eg.router).
  7. In the data link layer of router, router finds the MAC is itself and resolve it pass to the network layer. But it cannot find the DNS 8.8.8.8 in its routing table, so it will send the packet to the default router(in the public network).
  8. Before sends to the public network, the router will use NAT to change the source private ip to the public ip(the WAN ip of router).
  9. In the public network, the routers will find and change the MAC addressed to forward the packet to the next router.
  10. Then the DNS server will receive the packet, and resolve the packet, in the transport layer, it find the destination port is 53, so it knows it is a DNS request. And it resolve the ip of google.com and return the ip.
  11. And the return process is similar to the request process. Finally, after those, your computer will receive the ip of google.com and request the ip of google.com to obtain the page, and the process is similar as above.

But sometimes, the process won't be so smooth, due to the DNS server is overseas, so the traffic needs to go through the public exit port(except for using the IPLC intranet of ISP). Every packet will be checked, thus causing the DNS pollution(tampers a not exist ip), TCP reset(sends the RST packet in advance toreject the connection request), block ip or active detection.

实用工具:dig命令

dig是强大的DNS查询工具,常用用法:

  • dig baidu.com:查询A记录(等价于dig a baidu.com)。
    ;; ANSWER SECTION:  
    baidu.com.        6    IN    A    198.18.28.63  ; TTL为6秒,IP为198.18.28.63
    
  • dig +short baidu.com:仅显示IP地址。
  • dig @8.8.8.8 baidu.com:指定DNS服务器(如谷歌的8.8.8.8)进行查询。
  • dig +trace baidu.com:追踪解析路径,显示从根服务器到目标服务器的查询过程。
;; Received 239 bytes from 198.19.0.3#53(198.19.0.3) in 9 ms  
baidu.com.        6    IN    A    198.18.28.63  ; 最终从c.root-servers.net获取结果
  • dig cname facebook.github.io:查询CNAME记录,显示facebook.github.io指向github.map.fastly.net
...  

;; ANSWER SECTION:  
facebook.github.io. 3370    IN  CNAME   github.map.fastly.net.  
github.map.fastly.net.  600 IN  A   103.245.222.133

whois:查询域名注册信息

通过whois命令可获取域名的注册商、过期时间等信息:

whois github.com  ; 注意与系统命令`whoami`(查询当前用户)区分

参考资料

通过理解DNS的层级结构、解析流程和安全机制,可更好地优化网络配置、排查访问问题。如需进一步探讨,欢迎访问博客获取最新内容。

目录
相关文章
|
缓存 Kubernetes 网络协议
阿里云DNS常见问题之在手机上使用阿里的私人dns失败如何解决
阿里云DNS(Domain Name System)服务是一个高可用和可扩展的云端DNS服务,用于将域名转换为IP地址,从而让用户能够通过域名访问云端资源。以下是一些关于阿里云DNS服务的常见问题合集:
|
网络安全
[网络安全]xss-labs level-15 解题详析
[网络安全]xss-labs level-15 解题详析
377 0
|
机器学习/深度学习 人工智能 达摩院
ModelScope 社区介绍和操作入门| 学习笔记
快速学习 ModelScope 社区介绍和操作入门
ModelScope 社区介绍和操作入门| 学习笔记
|
网络协议 安全 网络安全
DNS服务器加密传输
【8月更文挑战第18天】
2826 15
|
域名解析 网络协议 安全
DNS服务器地址大全
DNS(域名系统)是互联网的“电话簿”,将域名解析为IP地址。选择优质DNS服务器可提升网络速度、降低延迟。以下是全球及中国各运营商的DNS服务器列表,包括公共DNS(如Google DNS、Cloudflare DNS)、中国电信、联通、移动等。根据地理位置、稳定性、安全性与隐私保护等因素选择适合的DNS服务器,优化上网体验。
51945 6
|
域名解析 网络协议 Ubuntu
dig 命令深入学习
dig 命令(Domain Information Groper)是一个用于查询 DNS (域名系统)记录的强大工具,它提供了详细的DNS信息,主要用于帮助用户诊断、调试和验证与域名解析相关的问题。
|
Python API 监控
将Python CLI工具发布为pip模块的完整指南
注册PyPI账户 访问PyPI官网注册账户 推荐使用双因素认证增强安全性 生成API令牌 访问PyPI账户管理 生成具有"Upload packages"权限的令牌,妥善保存 确保模块名唯一性 在PyPI搜索页面验证模块名未被使用 建议使用小写字母和连字符的组合(如my-cli-tool)
412 9
|
网络协议 安全 容灾
哪些 DNS 服务器的响应速度快且稳定可靠?
哪些 DNS 服务器的响应速度快且稳定可靠?
43325 4
|
缓存 负载均衡 监控
slb基于DNS的负载均衡
slb基于DNS的负载均衡
1194 8
|
缓存 NoSQL 关系型数据库
秒杀项目实战:遇到的问题及解决方案分享
构建了一个基于Springboot2的秒杀系统。项目利用K8S上的主从结构部署Redis和MySQL,通过Traefik作为网关。RabbitMQ在本地虚拟机的docker环境中,用Prometheus+Grafana监控。设计思路包括隐藏秒杀地址以防止脚本攻击,使用Lua脚本保证库存预扣原子性,但初期版本未处理重复订单校验。为防止MQ故障,将订单信息先保存到Redis,再通过脚本发送到MQ。采用分布式锁防止用户重复下单和缓存击穿问题,使用编程式事务确保库存扣减与订单保存一致性。项目通过JMeter测试,观察性能并分析Redis和RabbitMQ的使用情况。完整代码可在GitHub找到。
541 1
秒杀项目实战:遇到的问题及解决方案分享