历史上最详细的Linux DNS配置教程 推荐
2020-07-10 12:03:20
供稿:网友
 
						朋友们好,我也是一个刚刚接触Linux的 ,对于Linux下的DNS配置有点复杂,我也在网上搜索了好久,经过我的整理做了这个教程。希望给广大Linux新手朋友们一点帮助。 
任务1:构建主域名服务器 
任务2:构建辅助域名服务器 
任务3:测试DNS服务 
任务4:缓存DNS服务的配置与测试 
任务5:DNS转发器的配置 
任务6:新建子域及子域委派。 
建立两个虚拟机 
1.#ifconfig 
2.#netconfig 
3.ip address 192.168.13.1 
netmask: 255.255.255.0 
default gateway: 192.168.13.154 
primary nameserver: 192.168.13.1 
4.# service network restart 
5.ifconfig 
6.rpm -ql |grep bind 
--查看安装的和dns相关的软件包。 
bind-utils-9.2.4-2 
ypbind-1.17.2-2 
bind-6.2.4-2 
bind-libs-9.2.4-2 
7.#rpm -qa |grep caching 
caching-nameserver-7.3-3 
8.#rpm -ql caching-nameserver 
--可以看到好到十一个文件。 
有主配置文件和区域文件。 
/var/named/named.ca--目前互联网上的的根域服务器清单。 
9.#service named start 
10.#cat /var/named/named.ca 
我们现在配置主dns服务器。 
1.ll /etc/named.conf 
-rw-r--r-- 1 root root 1323 Aug 26 2010 /etc/named.com 
2.ll /var/named/ 
--可以看到九个文件 
3.vi /etc/named.conf 
默认用六个配置。 
我们复制 
zone"localhsot" IN { 
type master; 
file "localhost.zone"; 
allow-update {nane; }; 
}; 
zone "0.0.127.in-addr.arpa" IN { 
type master; 
file "localhost.zone"; 
allow-update {none; }; 
}; 
粘贴到下面,修改一下域名和区域。 
zone "xapc.com" IN { 
type master; 
file "xapc.com.zone"; 
allow-update {none; }; 
}; 
zone "13.168.192.in-addr.arpa" IN { 
type master; 
file "xapc.com.rev"; 
allow-update {none;}; 
}; 
:wq 
4.#clear 
5.#cd /var/named 
6.#ll 
7.#cp localhost.zone xapc.com.zone 
8.#cp named.local xapc.com.rev 
9.#vi xapc.com.zone 
--我们要修改里面的配置了 
@ IN SOA root ( 
42 ; serial(d.adams) 
3H ; refresh 
15M ; retry 
1W ; expiry 
1D ) ; minimum 
IN NS @ 
IN A 172.0.0.1 
IN AAAA ::1 
---------------这是系统默认的配置。我们要修改了 
@ IN SOA dns1.xapc.com. root.xapc.com. ( 
42 ; serial(d.adams) 
3H ; refresh 
15M ; retry 
1W ; expiry 
1D ) ; minimum 
IN NS dns1.xapc.com. 
IN NS dns2.xapc.com. 
IN MX 5 mail.xapc.com. 
dns1 IN A 192.168.13.1 
dns2 IN A 192.168.13.2 
www IN CNAME dns1.xapc.com. 
ftp IN CNAME dns2.xapc.com. 
mail IN CNAME dns2.xapc.com. 
:wq 
10.#vi xapc.com.rev 
@ IN SOA localhost.root.localhost. ( 
132323232 ; Serial 
28808 ; Refresh 
14400 ; Retry 
3600000 ; Expire 
86400 ) ; Mimimum 
IN NS localhost. 
1 IN PTR localhost. 
-----------以上是默认的配置,我们要修改了 
@ IN SOA dns1.xapc.com root.xapc.com. ( 
132323232 ; Serial 
28808 ; Refresh 
14400 ; Retry 
3600000 ; Expire 
86400 ) ; Mimimum 
IN NS dns1.xapc.com. 
IN NS dns2.xapc.com. 
1 IN PTR dns1.xapc.com. 
1 IN PTR www.xapc.com. 
2 IN PTR dns2.xapc.com. 
2 IN PTR ftp.xapc.com. 
:wq 
11.# named-checkconf 
--如果没有提示说明成功了 
12.# named-checkzone xapc.com xapc.com.zone 
13.#service named restart 
14.#nslookup 
>service 192.168.13.1-----联系一下。 
15.#ifconfig 
16.#ping 192.168.13.1 
17.#netstat -ntl 
18.#cat /etc/resolv.conf 
nameserver 192.168.13.1 
19.#nslookup www.xapc.com 
server: 192.168.13.1 
Adderss: 192.168.13.1#53 
www.xapc.com cononical name = dns1.xapc.com. 
Name: dns1.xapc.com 
Address: 192.168.13.1 
20.#nslookup ftp.xapc.com 
server: 192.168.13.1 
Adderss: 192.168.13.1#53 
ftp.xapc.com canonical name = dns2.xapc.com. 
Name: dns2.xapc.com 
Address: 192.168.13.2 
----以上都的正向解析。 
----下面开始反相解析了 
21.nslookup 192.l68.13.2 
Server: 192.168.13.1 
Address: 192.168.13.1#53 
2.13.168.192.in-addr.arpa name = ftp.xapc.com. 
2.13.168.192.in-addr.arpa name = dns2.xapc.com. 
22.cat /etc/resolv.conf 
nameserver 192.168.13.1 
---默认保存在这个文件中,会上这个文件中去找。 
23.#ping dns1.xapc.com 
24 #nslookup 
>server ip地址 --这里可以临时改变DNS服务器。 
>set type=mx 
>xapc.com 
Server: 192.168.13.1 
Address: 192.168.13.1#53 
xapc.com mail exchamger = 5 mail.xapc.com. 
>set type=a 
>mail.xapc.com 
Server: 192.168.13.1 
Address: 192.168.13.1#53 
mail.xapc.com comomical name = dns2.xapc.com. 
Name: dns2.xapc.com 
Address: 192.168.13.2 
>exit 
上面是正向和反向的配置。 
二:现在我们配置辅助服务器。 
好了,我们现在进入第二台虚拟机上 
1.#ifconfig 
2.#netconfig 
ip address: 192.168.13.2 
netmask: 255.255.255.0 
default gateway : 192.168.13.254 
primary nameserver: 192.168.13.2 
3.#service network restart 
4.#ifconfig 
5.#ping 192.168.13.1 
6.# rpm -qa | grep bind 
bind-utils-9.2.4-2 
ypbind--1.17.2-3 
bind-9.2.4.4-2 
bind-libs-9.2.4-2 
7.#rpm -q cachimg-nameserver 
cachim-nameserver-7.3-3 
8.vi /etc/named.conf 
zone"localhsot" IN { 
type master; 
file "localhost.zone"; 
allow-update {nane; }; 
}; 
zone "0.0.127.in-addr.arpa" IN { 
type master; 
file "localhost.zone"; 
allow-update {none; }; 
}; 
-----这是它默认的配置,我们要添加配置内容。 
zone "xapc.com" IN { 
type slave; 
file "slaves/xapc.com.zone"; 
masters {192.168.13.1; }; 
}; 
zone "13.168.192.in-addr.arpa" IN { 
type slave; 
file "slaves/xapc.com.rev"; 
masters {192.168.13.1;}; 
}; 
:wq 
9.#ll /var/named/ 
--可以看到九个文件 
10.#ll /var/named/slaves/ 
total 0 
11.#service named start 
12.#ll /var/named/slaves 
-rw---- 1 named named 436 xapc.com.rev 
-rw---- 1 named named 424 xapc.com.zone 
13.#nslookup ftp.xapc.com 
Server: 192.168.13.2 
Address: 192.168.13.2#53 
ftp.xapc.com canonical name = dns2.xapc.com. 
Name: dns2.xapc.com 
Address: 192.168.13.2 
14.#nslookup 192.168.13.1 --反向解析 
Server: 192.168.13.2 
Address: 192.168.13.2#53 
1.13.168.192.in-addr.arpa name = www.xapc.com. 
1.13.168.192.in-addr.arpa name = dns1.xapc.com. 
我们可不可以做 有些区域我是辅助的,有些区域我是主服务器。 
我们要做下一个实验了 
1.#vi /etc/named.conf 
zone "xapc.com" IN { 
type slave; 
file "slaves/xapc.com.zone"; 
masters {192.168.13.1; }; 
}; 
zone "13.168.192.in-addr.arpa" IN { 
type slave; 
file "slaves/xapc.com.rev"; 
masters {192.168.13.1;}; 
}; 
---- 这是前面配置好的结果,我们现在要在添加配置。 
zone "pcgj.com" IN { 
type master; 
file "pcgj.com.zone"; 
allow-update {none; }; 
}; 
:wq 
2.# cd /var/named 
3.#ll 
4.#cp localhost.zone pcgj.com.zone 
5.#vi pcgj.com.zone 
@ IN SOA @ root ( 
42 ; serial(d.adams) 
3H ; refresh 
15M ; retry 
1W ; expiry 
1D ) ; minimum 
IN NS @ 
IN A 172.0.0.1 
IN AAAA ::1 
---------------这是系统默认的配置。我们要修改了 
@ IN SOA dns.pcgj.com. root.pcgj.com.( 
42 ; serial(d.adams) 
3H ; refresh 
15W ; retry 
1W ; expiry 
1D ) ; minimum 
IN NS dns.pcgj.com. 
dns IN A 192.168.13.2 
:wq 
6.#service named restart 
7.#nslookup dns.pcgj.com 
Server: 192.168.13.2 
Address: 192.168.13.2#53 
Name: dns.pcgj.com 
Address: 192.168.13.2 
8.#nslookup dns1.xapc.com 
Server: 192.168.13.2 
Address: 192.168.13.2#53 
Name: dns1.xapc.com 
Address: 192.168.13.1 
现在反向解析的问题。所以要会到主服务器上配置。 
9.#vi /xapc.com.rev 
只增加一行配置。 
2 IN PTR dns.pcgj.com. 
注意: 132323232 ; Serial要改为 132323233 ; Serial 要在新的配置上加‘1’这样可以更新。 
:wq 
10.#service named restart 
11.#nslookup 192.168.13.2 
Server: 192.168.13.1 
address: 192.168.13.1#53 
2.13.168.192.in-addr.arpa name = ftp.xapc.com. 
2.13.168.192.in-addr.arpa name = dns2.xapc.com. 
2.13.168.192.in-addr.arpa name = dns.pcgj.com. 
现在我们在回到客户端: 
12.#cat slaves/xapc.com.rev 
可以看的配置文件 
如: 
2 PTR dns.pcgj.com. 
PTR ftp.xapc.com. 
PTR dns2.xapc.com. 
13.#nslookup 192.168.13.2 --反向解析 
Server: 192.168.13.2 
Address: 192.168.13.2#53 
2.13.168.192.in-addre.arpa name = ftp.xapc.com. 
2.13.168.192.in-addre.arpa name =dns2.xapc.com. 
2.13.168.192.in-addre.arpa name = dns.pcgj.com. 
14.#nslookup dns.pcgj.com 
Server: 192.168.13.2 
Address: 192.168.13.2#53 
Name: dns.pcgj.com 
Address: 192.168.13.2 
好了 ! 
现在我们要在做一个新的实验 
DNS的转发 
我们还是在辅助dns服务器上配置 
1.#service named stop 
2.#ll 
3.vi /etc/name.conf 
我们要添加两个转发配置,我们还要把科研解析的配置删除掉。只保留“pcgj.com”因为他不能解析服务器。 
statistics-file "/var/named/data/name_stats.txt" 
forward only; 
forwarders {192.168.13.1;}; 
:wq 
如果不放心的话,我们可以查看有没有错误。 
4.#named-checkconf 
5.#service named restart 
这台计算机已经不是辅助的DNS服务器了 
6.# ll 
7.#ll slaves/ 
可以看到两个配置文件。 
8.#rm slaves/* 
9.# nslookup www.xapc.com 
Server: 192.168.13.2 
Address: 192.168.13.2#53 
nom-authoritative answer;---非权威 
www.xapc.com camomical name = dns1.xapc.com. 
Name: dns1.xapc.com 
Address: 192.168.13.1 
10.#nslookup dns.pcgj.com 
Server: 192.168.13.2 
Address: 192.168.13.2#53 
Name: dns.pcgj.com 
Address: 192.168.13.2 
我们能不能让部分区域转发,两个域,我让这个区域转发给你,其他域,我可以找根。 
我们还在辅助dns上修改配置文件。 
1.#vi /etc/named.conf 
我们们要注释掉两个配置内容。在新建一个区域。 
// forward only; 
// forwarders {192.168.13.1; }; 
zone "xapc.com" IN { 
type forward; 
forward only; 
forwarder {192.168.13.1; }; 
}; 
:wq 
2.#service named restart 
3.#nslookup ftp.xapc.com 
Server: 192.168.13.2 
Address: 192.168.13.2#53 
Nom-authoritative answer; 
ftp.xapc.com camomical name = dns2.xapc.com. 
Name: dns2.xapc.com 
Address: 192.168.13.2 
4.#nslookup 192.168.13.2 
它解析不了,因为没有让这个区域转发,也找不到根。 
所以我们可以让一个区域转发,其他区域找根。 
DNS服务器的主要测试方法; 
我们使用nslookup,dig和host等专用工具可以对DNS服务器进行较全面的测试。 
使用nslookup测试DSN服务器1 
(1)进入nslookup命令交换环境 
# nslookup 
> 
(2)设置使用指定的DNS服务器 
>server 192.168.1.2 
(3)测试localhost主机域名的正向解析 
>localhost 
(4)测试localhost主机域名的反向解析 
>127.0.0.1 
在辅助dns上练习一下: 
1.#nslookup 
>server 192.168.13.1 
Default server: 192.168.13.1 
Address: 192.168.13.1#53 
>192.168.13.2 
Server: 192.168.13.1 
Address: 192.168.13.1#53 
2.13.168.192.in-addr.arpa name=dns.pcgj.com. 
2.13.168.192.in-addr.arpa name=ftp.xapc.com. 
2.13.168.192.in-addr.arpa name=dns2.xapc.com. 
>exit 
使用nslookup测试DSN服务器2 
(1)测试互联网中的域名解析 
>www.yahoo.com.cn 
(2)测试test.com域中的A记录 
>host1.test.com 
(3)测试test.com域中的PTR记录 
>192.168.1.11 
(4)测试test.com域中的CNAME记录 
>www.test.com 
使用nslookup测试DSN服务器3 
(1)测试test.com域中的NS记录 
>set type=ns 
>test.com 
(2)测试test.com域中的MX记录 
>set type=mx 
>test.com 
(3)设置进行A记录的测试 
>set type=a 
使用dig命令测试DNS服务器1 
dig @dns域名或ip 区域 记录类型 
eg: dig @192.168.13.2 xapc.com NS 
意思是到192.168.13.2这个网段来挖掘xapc.com这个区域中的NS记录,结果会显现NS所以得记录。 
我们在辅助dns上演示一下 
1.#dig @192.168.13.2 xapc.com NS 
2.ll /var/name/named.ca 
--保存了所有的根域服务器的域名解析,dns的清单这个文件是怎么是得到的那。它就是用dig命令得到的。 
dig @a.root-servers.net . NS>/var/named/named.ca 
好了 下面我们在讲一个重要的内容。 
DNS子域的委派。 
这两台服务器是父子关系。但两个人各有各得dns服务器来解析。 
好了,我们现在从辅助dns上开始吧。 
1:#pwd 
/var/named 
2: #vi /etc/named.conf 
zone "0.in-addr.arpa" IN { 
type master; 
file"named.zero"; 
allow-update{none; }; 
}; 
zone "pcgj.com" IN { 
type master; 
file "pcgj.com.zone"; 
allow-update {none; }; 
}; 
zone "xapc.com" IN { 
type forward; 
forward only; 
forwarders {192.168.13.1; }; 
}; 
----上面是前面修改好的我们要删除一些配置, 
下面在添加一些内容。 
zone "ca.xapc.com" IN { 
type master; 
file "ca.xapc.com.zone"; 
ailow-update {none; }; 
}; 
:ok 
我们还要新建一个ca.xapc.com.zone文件 
2.#ll 
3.#cp pcgj.com.zone ca.xapc.com.zone 
4.#vi ca.xapc.com.zone 
@ IN SOA dns.pcgj.com. root.pcgj.com.( 
42 ; serial(d.adams) 
3H ; refresh 
15W ; retry 
1W ; expiry 
1D ) ; minimum 
IN NS dns.pcgj.com. 
dns IN A 192.168.13.2 
----这是前面我们修改好的文件,我们要修改它。 
@ IN SOA dns.ca.xapc.com. root.ca.xapc.com.( 
42 ; serial(d.adams) 
3H ; refresh 
15W ; retry 
1W ; expiry 
1D ) ; minimum 
IN NS dns.ca.xapc.com. 
dns IN A 192.168.13.2 
www IN A 192.168.13.1 
:wq 
5:# service named restart 
6:# nslookup www.ca.xapc.com 
Server: 192.168.13.2 
Address: 192.168.13.2#53 
Name: www.ca.xapc.com 
Address: 192.168.13.1 
现在我们来的主服务器,看看主服务器能不能解析,它能解析它的孙子吗? 
7:#nslookup www.ca.xapc.com 
---不能解析的,因为没有做委派,所以不能解析。 
8:#vi /etc/named.conf 
做委派是时候不需要修改主配置文件。 
我们只需要修改xapc.com.zone这个住配置文件就好了 
9:#vi xapc.com.zone 
@ IN SOA dns1.xapc.com. root.xapc.com. ( 
42 ; serial(d.adams) 
3H ; refresh 
15M ; retry 
1W ; expiry 
1D ) ; minimum 
IN NS dns1.xapc.com. 
IN NS dns2.xapc.com. 
IN MX 5 mail.xapc.com. 
dns1 IN A 192.168.13.1 
dns2 IN A 192.168.13.2 
www IN CNAME dns1.xapc.com. 
ftp IN CNAME dns2.xapc.com. 
mail IN CNAME dns2.xapc.com. 
-------这是前面我们修改好的, 我们还要修改此文件。 
@ IN SOA dns1.xapc.com. root.xpac.com.( 
42 ; serial(d.adams) 
3H ; refresh 
15M ; retry 
1W ; expiry 
1D ) ; minimum 
IN NS dns.ca.xapc.com. 
IN NS dns1.xapc.com. 
IN NS dns2.xapc.com. 
IN MX 5 mail.xapc.com. 
dns1 IN A 192.168.13.1 
dns2 IN A 192.168.13.2 
www IN CNAME dns1.xapc.com. 
ftp IN CNAME dns2.xapc.com. 
mail IN CNAME dns2.xapc.com. 
dns.ca IN A 192.168.13.2 
:wq 
10:#services named restart 
11:#nslookup www.ca.xapc.com 
Server: 192.168.13.1 
Address: 192.168.13.1#53 
Non-authoritative amswer: 
Name: www.ca.xapc.com 
Address: 192.168.13.1 
ok了, 所以的实验都完成了 
我们可以的课后练习一下。 
任务1:构建主域名服务器 
任务2:构建辅助域名服务器 
任务3:测试DNS服务 
任务4:缓存DNS服务的配置与测试 
任务5:DNS转发器的配置 
任务6:新建子域及子域委派。