新站提交
热搜: 目录平台

一、域名

在讲解DNS之前,首先粗略的说一下域名这个东西

比如说我们在用浏览器访问网址的时候,我们输入的网站的地址就是一个域名,例如 www.google.com,但其实我们知道我们标志互联网中的每一个机器都是有一个IP地址的,我们若需要访问一个网站的服务器,实际上是需要输入该网站的IP地址来进行访问的,但是IP地址往往难以记忆,IPv4尚且难记,IPv6就更不用说了,所以我们就通过使用域名来对其进行管理,但是在访问的时候,系统底层还是通过ip地址来进行访问的,域名和IP地址直接的转换就是通过DNS服务器来完成的。首先我们先来看看域名的一些知识,就拿 www.google.com 这个域名来说。

通常对于一个域名来说,其主要分为3个部分

www.google.com主机名 域名 类型

``①类型标志着该域名的类型(com、cn、edu、org等)

②域名域的名称,如上面的 google

③主机名域里面一台主机的名字,如www这台主机

其实严格的来说,域名后面还有一个 ".",所以完整的域名应该是 www.google.com.

对于域名来说,其大小写是不敏感的,在浏览器里面我们输入大小写都指向的同一个ip地址

二、DNS

从上面可以知道,每一个域名都对应了一个IP地址,我们访问一个域名时,其实底层的操作系统进行通信时还是通过IP地址来进行通信,所以我们就需要有一个服务能将域名转换成对应的IP地址,或者反向的将IP地址转换成域名,这个服务就是DNS(Domain Name System)。

DNS提供的服务就是在IP和域名之间进行转换,DNS服务是由DNS服务器来提供的,一般提供DNS服务的都有一个单独的DNS服务器,这个DNS服务器的作用就是用来保存我们的域名到其IP地址之间的解析,例如我们在访问 www.google.com 这个域名的时候,我们的主机首先会向一个DNS服务器发起一个DNS的请求,请求DNS服务器告诉我www.google.com这个域名的IP地址,此时DNS服务器就会给我们返回该域名的IP地址,这个时候我们再根据这个IP地址访问www.google.com这台服务器

DNS服务就是将我们的域名解析成其对应的IP地址,我们日常使用的支持网络的计算机一般是做为DNS客户端来使用的,应用程序、服务、进程等等通过操作系统底层的功能发起对DNS服务器的查询,来对指定的域名进行解析

在Linux系统当中,一般是使用系统底层提供的gethostbyname()这个函数来进行域名解析的

在Linux系统当中,解析域名通常可以基于以下三种方式:

①文件 (/etc/hosts、/etc/networks)

hosts是主机文件,里面存放的是域名到IP地址的一个对应,在windows下也有这样一个文件,通常linux下通过文件来解析域名都是通过 /etc/hosts 这个文件

[[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

``因为hosts文件里面保存的就是域名到IP地址直接的对应关系,我们也可以手动的去将我们的域名指定为其它的IP,例如我们将 www.google.com 的 ip 修改为 192.168.1.1

[[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# ping www.google.com PING www.google.com (173.194.72.103) 56(84) bytes of data. [[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# vim /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.1.1 www.google.com[[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# ping www.google.com PING www.google.com (192.168.1.1) 56(84) bytes of data.

``我们看到,我们在 /etc/hosts 文件里加上 192.168.1.1 www.google.com 这条映射关系以后,此时在ping www.google.com时,其ip地址变成了我们自己设定的 192.168.1.1了,但是我们知道其IP地址肯定不是这个。为此,我们可以在操作系统上将一些域名的IP地址直接写到 hosts 文件里面,这样在进行解析时可以直接去访问我们指定的IP地址

②DNS这个就是通过我们指定的DNS服务器来对我们的域名进行解析了

③NIS这个用的非常少

我们可以通过查看 /etc/nsswitch.conf 这个文件来查看DNS解析的顺序

[[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# cat /etc/nsswitch.conf # # /etc/nsswitch.conf # # An example Name Service Switch config file. This file should be # sorted with the most-used services at the beginning. # # The entry '[NOTFOUND=return]' means that the search for an # entry should stop if the search in the previous entry turned # up nothing. Note that if the search failed due to some other reason # (like no NIS server responding) then the search continues with the # next entry. # # Valid entries include: # # nisplus Use NIS+ (NIS version 3) # nis Use NIS (NIS version 2), also called YP # dns Use DNS (Domain Name Service) # files Use the local files # db Use the local database (.db) files # compat Use NIS on compat mode # hesiod Use Hesiod for user lookups # [NOTFOUND=return] Stop searching if not found so far # # To use db, put the "db" in front of "files" for entries you want to be # looked up first in the databases # # Example: #passwd: db files nisplus nis #shadow: db files nisplus nis #group: db files nisplus nispasswd: files shadow: files group: files#hosts: db files nisplus nis dns hosts: files dns# Example - obey only what nisplus tells us... #services: nisplus [NOTFOUND=return] files #networks: nisplus [NOTFOUND=return] files #protocols: nisplus [NOTFOUND=return] files #rpc: nisplus [NOTFOUND=return] files #ethers: nisplus [NOTFOUND=return] files #netmasks: nisplus [NOTFOUND=return] files bootparams: nisplus [NOTFOUND=return] files ethers: files netmasks: files networks: files protocols: files rpc: files services: files netgroup: nisplus publickey: nisplus automount: files nisplus aliases: files nisplus

``我们看到我们 hosts 解析的顺序先是通过 file 文件来进行解析,再是通过DNS进行解析,我们可以直接修改该文件来控制我们的查询顺序

三、DNS查询

我们可以通过 host 或者 dig 命令来查询我们域名对应的IP地址

①host(显示内容较少)

[[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# host www.google.com www.google.com has address 74.125.31.104www.google.com has address 74.125.31.147www.google.com has address 74.125.31.106www.google.com has address 74.125.31.99www.google.com has address 74.125.31.103www.google.com has address 74.125.31.105www.google.com has IPv6 address 2404:6800:4008:c01::6a

``②dig (显示内容详细)

[[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# dig www.google.com ; > DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 > www.google.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER

我们看到dig命令查询出来的内容非常多,对于里面的每一行语句的含义,将在后续随笔中详细讲解

对于DNS查询来说,其实DNS是一个树状的结构,查询的时候是根据域名从右到左来进行查询,域名每一级由独立的一个或者多个DNS服务器来进行查询

我们看到,当我们在对 www.google.com 这个域名进行查询时,首先是通过 . 这个根DNS服务器去查询 com 这个类型的 DNS 服务器地址,然后再通过 com 对应的多台DNS服务器(每一层都有多个DNS服务器)去查询 google 这个域名的DNS服务器,最后再通过 google的DNS服务器去查询主机名为 www 的这台主机的资源记录,从而得到 www.google.com 的IP地址。我们可以通过 dig +trace www.google.com 这个命令来追踪整个DNS查询的过程

[[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# dig +trace www.google.com; > DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 > +trace www.google.com ;; global options: +cmd . 5 IN NS f.root-servers.net. . 5 IN NS g.root-servers.net. . 5 IN NS h.root-servers.net. . 5 IN NS i.root-servers.net. . 5 IN NS j.root-servers.net. . 5 IN NS k.root-servers.net. . 5 IN NS l.root-servers.net. . 5 IN NS m.root-servers.net. . 5 IN NS a.root-servers.net. . 5 IN NS b.root-servers.net. . 5 IN NS c.root-servers.net. . 5 IN NS d.root-servers.net. . 5 IN NS e.root-servers.net. ;; Received 508 bytes from 192.168.198.2#53(192.168.198.2) in 28138 ms com. 172800 IN NS a.gtld-servers.net. com. 172800 IN NS b.gtld-servers.net. com. 172800 IN NS c.gtld-servers.net. com. 172800 IN NS d.gtld-servers.net. com. 172800 IN NS e.gtld-servers.net. com. 172800 IN NS f.gtld-servers.net. com. 172800 IN NS g.gtld-servers.net. com. 172800 IN NS h.gtld-servers.net. com. 172800 IN NS i.gtld-servers.net. com. 172800 IN NS j.gtld-servers.net. com. 172800 IN NS k.gtld-servers.net. com. 172800 IN NS l.gtld-servers.net. com. 172800 IN NS m.gtld-servers.net. ;; Received 492 bytes from 193.0.14.129#53(193.0.14.129) in 26867 ms google.com. 172800 IN NS ns2.google.com. google.com. 172800 IN NS ns1.google.com. google.com. 172800 IN NS ns3.google.com. google.com. 172800 IN NS ns4.google.com. ;; Received 168 bytes from 192.54.112.30#53(192.54.112.30) in 8681 ms www.google.com. 300 IN A 173.194.72.103www.google.com. 300 IN A 173.194.72.99www.google.com. 300 IN A 173.194.72.106www.google.com. 300 IN A 173.194.72.104www.google.com. 300 IN A 173.194.72.147www.google.com. 300 IN A 173.194.72.105;; Received 128 bytes from 216.239.38.10#53(216.239.38.10) in 82 ms

``我们看到整个DNS查询过程就是上述的根据域名从右到左来一步步查询, . root DNS -> com DNS -> google DNS -> www

DNS查询类型一共有两种,一种是 迭代查询 (Iterative Query),另一种是 递归查询 (Recursive Query)我们通过下面两个示意图就可以知道这两种查询的区别了

①迭代查询 (Iterative Query)

②递归查询 (Recursive Query)

我们看到迭代查询时本地DNS Server会首先向 . DNS Server发出请求,此时 . DNS Server会返回给本地DNS Server com的 DNS Server,这样依次类推下去,最后将 www.google.com的ip地址返回,而递归查询,则只是本地DNS Server向 . DNS Server发出一个请求,然后解析IP是交给了其下属的DNS Server来完成,最后将IP地址返回,由于递归查询在每个DNS Server上会有缓存信息,所以一般我们都是使用两种查询方式相结合的方式来进行DNS查询的

四、资源记录

在DNS服务器上,DNS的信息都是通过一个叫做资源记录(RR Resource Record)的格式来进行保存的,RR不仅能够保存域名到IP地址的信息,还能保持其他很多的信息

资源记录常用的属性有:

- NAME(主机名)

- CLASS (类别,通常是IN)

- TYPE(类型,资源记录的类型)

- RDATA(信息)

如:

wwwINA192.168.1.1

mailINA192.168.1.2

server1INCNAMEwww

INMX 10 mail.gmail.com.(对于MX记录,后面的域名一定要写完整,即最后的 . 也要加上)

NAME**CLASSTYPERDATA**

上面都是我们的资源记录。DNS的资源记录可以记录许多类型的资源,而不仅仅是IP地址,常见的资源记录类型如下:

资源记录类型(TYPE)表示内容AIPv4地址 AAAAIPv6地址 MX邮件记录 CNAME别名 PTR 指针(逆向解析) SRV服务资源我们可以使用DNS的高级查询命令来查看这些资源记录的信息

①dig -t a www.google.com查看域名的IPv4地址

[[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# dig -t a www.google.com; > DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 > -t a www.google.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER

②dig -t mx gmail.com查看gmail.com这个域名的邮件记录

[[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# dig -t mx gmail.com; > DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 > -t mx gmail.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER

③dig -x 42.121.135.98逆向解析 42.121.135.98 这个IP地址得到其域名 (博客园的IP)

[[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# host www.cnblogs.com www.cnblogs.com has address 42.121.135.98[[email protected]/ <![CDATA[ /!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/ ]]> / ~]# dig -x 42.121.135.98; > DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 > -x 42.121.135.98;; global options: +cmd ;; Got answer: ;; ->>HEADER

本文转自xiaoluo501395377的博客

本文地址:http://www.cnblogs.com/xiaoluo501395377/archive/2013/06/03/3116064.html

标签: dns 域名