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服务的常见问题合集:
|
机器学习/深度学习 监控 Kubernetes
使用 Seldon Alibi 进行模型监控
虽然 Seldon 使在生产中部署和服务模型变得容易,但一旦部署,我们如何知道该模型是否在做正确的事情? 训练期间的良好表现并不一定意味着在生产运行几个月后表现良好。 现实世界中发生的事情是我们无法解释的,例如:输入数据逐渐偏离训练数据,以及异常值和偏差。
|
Python API 监控
将Python CLI工具发布为pip模块的完整指南
注册PyPI账户 访问PyPI官网注册账户 推荐使用双因素认证增强安全性 生成API令牌 访问PyPI账户管理 生成具有"Upload packages"权限的令牌,妥善保存 确保模块名唯一性 在PyPI搜索页面验证模块名未被使用 建议使用小写字母和连字符的组合(如my-cli-tool)
361 9
|
5月前
|
数据采集 运维 安全
2025年T-BOX产品综合评测发布:这几款产品为什么值得重点关注?
树根科技T-BOX以“终端+平台+应用”一体化模式,深度融合工业互联网,支持多协议接入、高精度定位与云边协同,广泛适配工程机械、物流、特种车辆等场景。依托根云平台4.0,提供数据采集、远程控制、电子围栏、OTA升级等能力,助力设备数智化管理。产品通过CCC、CE、FCC等全球30国认证,服务于三一重工、普茨迈斯特等头部企业,实现设备透明化运维、后市场服务升级与全球化合规出海,是工业物联网时代高可靠性、强扩展性的优选方案。
312 0
|
域名解析 网络协议 安全
DNS服务器地址大全
DNS(域名系统)是互联网的“电话簿”,将域名解析为IP地址。选择优质DNS服务器可提升网络速度、降低延迟。以下是全球及中国各运营商的DNS服务器列表,包括公共DNS(如Google DNS、Cloudflare DNS)、中国电信、联通、移动等。根据地理位置、稳定性、安全性与隐私保护等因素选择适合的DNS服务器,优化上网体验。
45374 6
|
Linux 存储 监控
关于 Systemd
Linux 系统使用 init 管理启动进程的历史已久(例如通过 sudo /etc/init.d/apache2 start 或 service apache2 start 启动服务),但传统 init 采用串行启动方式,效率较低。为解决这一问题, systemd 应运而生(名称中的d是 daemon 的缩写,表明其守护进程管理器的定位)。如今,systemd 已取代 initd,成为主流 Linux 发行版的默认主进程(PID 1)。
355 62
|
网络协议 安全 容灾
哪些 DNS 服务器的响应速度快且稳定可靠?
哪些 DNS 服务器的响应速度快且稳定可靠?
37837 4
|
安全 数据安全/隐私保护 算法
安全基础概述
安全基础概述
223 1
|
域名解析 负载均衡 安全
DNS技术标准趋势和安全研究
本文探讨了互联网域名基础设施的结构性安全风险,由清华大学段教授团队多年研究总结。文章指出,DNS系统的安全性不仅受代码实现影响,更源于其设计、实现、运营及治理中的固有缺陷。主要风险包括协议设计缺陷(如明文传输)、生态演进隐患(如单点故障增加)和薄弱的信任关系(如威胁情报被操纵)。团队通过多项研究揭示了这些深层次问题,并呼吁构建更加可信的DNS基础设施,以保障全球互联网的安全稳定运行。
|
域名解析 缓存 网络协议
DNS(Domain Name System,域名系统)详解
DNS(Domain Name System,域名系统)是互联网中用于将域名转换为相应IP地址的分布式命名系统。虽然不是严格意义上的局域网协议,但它在中大型局域网起着至关重要的作用。它负责将域名映射为IP地址,使得用户可以使用易记的名称访问网络资源。
2072 0