由於人類對於數字遠遠不如文字來得敏感,偏偏計算機的世界中僅認識0,1而已。爲了將兩者結合起來,於是就有了主機名稱與IP的對應,這個對應的協議就是DNS。
DNS的作用就是正向將ip地址解析爲域名,人們記憶起來會比較方便。本篇文章將會介紹如何在linux下部署dns服務器。
以下是部署dns的過程:
基本場景
某單位需要配置兩臺 DNS 服務器來實現域名解析。
您的實驗環境的 IP 地址
ns1.abc.local ns2.abc.local
192.168.1.241 192.168.1.242
需要完成以下域名的解析:
ftp.abc.local
10.0.0.1
mailsrv1.abc.local 10.0.0.2
smtp.abc.local
10.0.0.2
pop3.abc.local
10.0.0.2
www.abc.local
10.0.0.3、10.0.0.4(兩個主機,以平衡負荷)
smtp 及 pop3 需要使用 CNAME 來進行解析。同時,需要實現反向地址解析。
2. 實驗環境
2.1. 服務器安裝
# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.4 (Santiago)
# uname -a
Linux localhost.localdomain 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29
11:47:41 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
2.2. 服務器基本配置
ns1.abc.local 的配置。
修改 IP 地址。 (注意:根據您的實現環境進行配置)
# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.1.241 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 修改主機名 # vi /etc/sysconfig/network NETWORKING=yes #HOSTNAME=localhost.localdomain HOSTNAME=ns1.abc.local |
重新啓動以便主機名更改生效
# reboot
爲了方便實驗,將防火牆關閉
# service iptables stop
# chkconfig iptables off
同時關閉 selinux (這時十分重要的,不然很多都是實現不了)
# vi /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
與此類似,修改 ns2.abc.local 的配置。
3. DNS 客戶機配置
修改/etc/resolv.conf 將 DNS 服務器配置
# vi /etc/resolv.conf
nameserver 192.168.1.241
nameserver 192.168.1.242
測試解析域名 www.sina.com.cn
# nslookup www.sina.com.cn
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached
問題:爲什麼無法解析?
回答:本機 DNS 服務器沒有啓動,通過以下命令檢查 named 是否啓動
# ps aux | grep named
root 1776 0.0 0.0 103236 860 pts/0 S+ 20:15 0:00 grep named
切換 DNS 服務器,再重新進行嘗試
# nslookup
> server 202.102.224.68 (指向另一臺服務器,外網)
Default server: 202.102.224.68
Address: 202.102.224.68#53
> www.sina.com.cn
Server: 202.102.224.68
Address: 202.102.224.68#53
Non-authoritative answer: (非權威應答,意思是從別的dns服務器那裏得到的答案,這個提示是正常的,因爲總是有人會誤以爲是錯誤的)
www.sina.com.cn canonical name = jupiter.sina.com.cn.
jupiter.sina.com.cn canonical name = polaris.sina.com.cn.
Name: polaris.sina.com.cn
Address: 202.108.33.60
> exit
問題:爲什麼這樣可以解析?
回答:切換了 DNS 服務器,而且本機可以訪問 Internet
4. DNS 服務器組件安裝
4.1. 通過 rpm 來進行安裝
# mkdir /mnt/cdrom # mount /dev/cdrom /mnt/cdrom/ mount: block device /dev/sr0 is write-protected, mounting read-only # cd /mnt/cdrom/Packages/ # ls bind* bind-9.8.2-0.17.rc1.el6.x86_64.rpm bind-dyndb-ldap-2.3-2.el6.x86_64.rpm bind-libs-9.8.2-0.17.rc1.el6.x86_64.rpm bind-chroot-9.8.2-0.17.rc1.el6.x86_64.rpm bind-libs-9.8.2-0.17.rc1.el6.i686.rpm bind-utils-9.8.2-0.17.rc1.el6.x86_64.rpm # rpm -Uvh bind-9.8.2-0.17.rc1.el6.x86_64.rpm error: Failed dependencies: portreserve is needed by bind-32:9.8.2-0.17.rc1.el6.x86_64 # rpm -Uvh bind-9.8.2-0.17.rc1.el6.x86_64.rpm \ portreserve-0.0.4-9.el6.x86_64.rpm Preparing... ########################################### [100%] 1:portreserve ########################################### [ 50%] 2:bind ########################################### [100%] |
4.2. 通過 yum 來進行安裝
還可以通過 yum 來進行安裝,前提條件是配置過 yum 的 repositories
還有如果你的虛擬機是可以聯網的,就不用再配置yum庫了,直接用下面的命令從網上下載資源安裝
# yum -y install bind
4.3. 基本配置
查看配置文件
# rpm -qc bind
/etc/logrotate.d/named
/etc/named.conf
/etc/named.iscdlv.key
/etc/named.rfc1912.zones
/etc/named.root.key
/etc/rndc.conf
/etc/rndc.key
/etc/sysconfig/named
/var/named/named.ca
/var/named/named.empty
/var/named/named.localhost
/var/named/named.loopback
其中,/etc/named.conf 爲主配置文件。
新開一個會話,通過 tail -f 命令觀察日誌:
# tail -f /var/log/messages
啓動服務
# service named start
Generating /etc/rndc.key:
第一次生成 key 會慢一些。
[ OK ]
Starting named: [ OK ]
生成 key 慢,這是一個已知的問題。以後就不會出現此問題了。
# service named restart
Stopping named: . [ OK ]
Starting named: [ OK ]
速度很快
# ps aux | grep named
named 1862 0.0 0.5 159204 11048 ? Ssl 20:33 0:00 /usr/sbin/named -u named
root 1870 0.0 0.0 103236 860 pts/0 S+ 20:35 0:00 grep named
修改配置文件。
在修改之前,先進行備份,這是一個好習慣
# cp /etc/named.conf /etc/named.conf.original
# netstat -an | grep :53
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN
tcp 0 0 ::1:53 :::* LISTEN
udp 0 0 127.0.0.1:53 0.0.0.0:*
udp 0 0 ::1:53
# vi /etc/named.conf
注意配置文件中的分號
建議:先將原有配置註釋,再寫新的配置,如下所示
options {
//listen-on port 53 { 127.0.0.1; };
listen-on port 53 { any; };
將 dnssec 設置"yes"修改爲 no,示例如下:
//dnssec-enable yes;
dnssec-enable no;
//dnssec-validation yes;
dnssec-validation no;
dnssec-lookaside auto;
將 allow-query 設置修改爲 any,示例如下:
//allow-query { localhost; };
allow-query { any; };
重新啓動服務
# service named restart Stopping named: . [ OK ] Starting named: [ OK ] # netstat -an | grep :53 tcp 0 0 192.168.1.241:53 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN tcp 0 0 ::1:53 :::* LISTEN udp 0 0 192.168.1.241:53 0.0.0.0:* udp 0 0 127.0.0.1:53 0.0.0.0:* udp 0 0 ::1:53 # chkconfig named on # chkconfig --list named named 0:off 1:off 2:on 3:on 4:on 5:on 6:off |
此時的 DNS 服務器是 cache only 服務器,只有 root 域的記錄。
5. 主 DNS 服務器配置
5.1. 創建正向 ZONE
# vi /etc/named.conf
會看到 directory "/var/named";
配置文件的目錄中/var/named
在配置文件的後面添加如下信息
zone "abc.local" IN {
type master;
file "abc.local.zone";
};
# cd /var/named/
# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
使用空白模板來進行創建新的 zone 文件
# cp named.empty abc.local.zone
# vi abc.local.zone
$TTL 3H
@ IN SOA ns1.abc.local. admin.abc.local. (
0 ; seria 序列號
1D ; refresh 輔助域名服務器多少時間更新數據
1H ; retry 若輔助域名服務器更新數據失敗,多少時間再試
1W ; expire 若輔助域名服務器無法從主服務器上更新數據,原有數據有效期
3H ) ; minimum 若資源記錄沒有設定 TTL,則以此 TTL 爲準
NS ns1.abc.local.
ns1 A 192.168.1.241
ftp A 10.0.0.1
mailsrv1 A 10.0.0.2
www A 10.0.0.3
www A 10.0.0.4
smtp CNAME mailsrv1.abc.local.
pop3 CNAME mailsrv1.abc.local.
修改新創建配置文件的屬主。
# chown root:named /var/named/abc.local.zone
# rndc status
修改配置之後,有兩種方法以修改生效
方法 1:重新啓動服務
# service named restart
Stopping named: [ OK ]
Starting named: [ OK ]
方法 2:使用 rndc
# rndc reload
server reload successful
測試一下:
# nslookup ns1.abc.local
# nslookup mailsrv1.abc.local
# nslookup smtp.abc.local
# nslookup pop3.abc.local
# nslookup www.abc.local
# nslookup www.abc.local
注意觀察,兩次對 www.abc.local 解析的結果的不同
Server: 192.168.1.241
Address: 192.168.1.241#53
# rndc status
version: 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6
CPUs found: 1
worker threads: 1
number of zones: 20
增加了一個 zone
debug level: 0
xfers running: 0
xfers deferred: 0
soa queries in progress: 0
query logging is OFF
recursive clients: 0/0/1000
tcp clients: 0/100
server is up and running
5.2. 創建反向 ZONE
# vi /etc/named.conf
在配置文件的後面添加如下信息,這裏一定要寫反向的地址
zone "0.0.10.in-addr.arpa" IN {
type master;
file "10.0.0.zone";
};
# cd /var/named/
# cp abc.local.zone 10.0.0.zone
# vi 10.0.0.zone
修改配置文件,內容如下:
$TTL 3H
@ IN SOA ns1.abc.local. admin.abc.local. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns1.abc.local.
1 PTR ftp.abc.local.
2 PTR mailsrv1.abc.local.
3 PTR www.abc.local.
4 PTR WWW.abc.local.
~
修改新創建配置文件的屬主。
# chown root:named /var/named/10.0.0.zone
# rndc reload
server reload successful
查看日誌文件中是否有錯誤信息
# rndc status
version: 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6
CPUs found: 1
worker threads: 1
number of zones: 21
增加了一個 zone
debug level: 0
xfers running: 0
xfers deferred: 0
soa queries in progress: 0
query logging is OFF
recursive clients: 0/0/1000
tcp clients: 0/100
server is up and running
驗證一下
# nslookup 10.0.0.1
Server: 192.168.1.241
Address: 192.168.1.241#53
1.0.0.10.in-addr.arpa name = ftp.abc.local.
# nslookup 10.0.0.2
Server: 192.168.1.241
Address: 192.168.1.241#53
2.0.0.10.in-addr.arpa name = mailsrv1.abc.local.
# nslookup 10.0.0.3
Server: 192.168.1.241
Address: 192.168.1.241#53
3.0.0.10.in-addr.arpa name = www.abc.local.
# nslookup 10.0.0.4
Server: 192.168.1.241
Address: 192.168.1.241#53
4.0.0.10.in-addr.arpa name = WWW.abc.local.
6. 配置轉發器
場景:
缺省情況下, DNS 服務器爲會爲不在本機所管理的域信息通過迭代的方式從根服務器查
起。由於多數根服務器主要在國外,速度比較慢。現在希望通過配置轉發器,將查詢發給本
地 ISP 的 DNS 服務器以提高效率。
以鄭州爲例,可以使用:
網通:202.102.224.68、202.102.227.68
電信:219.150.150.150
操作:
修改 DNS 主配置文件
# vi /etc/named.conf
在全局配置語句 option 中添加 forwarders 配置,注意分號
options {
……
forwarders {202.102.224.68; 202.102.227.68; 219.150.150.150; };
};
需要重新啓動 DNS 服務
# service named restart
我們可以通過 tcpdump 等抓包工具來查看是否進行了轉發。
# tcpdump -n port 53
再打開一個會話,運行以下命令使用# nslookup www.msn.com 命令觸發一個解析
請求。
觀察 tcpdump 的輸出結果
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
11:39:25.862065 IP 192.168.1.241.60113 > 202.102.227.68.domain: 58544+% [1au] A?
www.msn.com. (40)
11:39:25.862190 IP 192.168.1.241.62826 > 202.102.227.68.domain: 57413+ [1au] NS? . (28)
11:39:26.108518 IP 202.102.227.68.domain > 192.168.1.241.60113: 58544 2/0/1 CNAME
us.co1.cb3.glbdns2.microsoft.com., A 131.253.13.140 (99)
11:39:26.108755 IP 202.102.227.68.domain > 192.168.1.241.62826: 57413 13/0/11 NS
e.root-servers.net., NS j.root-servers.net., NS m.root-servers.net., NS g.root-servers.net., NS
b.root-servers.net., NS h.root-servers.net., NS f.root-servers.net., NS a.root-servers.net., NS
k.root-servers.net., NS c.root-servers.net., NS l.root-servers.net., NS i.root-servers.net., NS
d.root-servers.net. (423)
11:39:26.109540 IP 192.168.1.241.25836 > 202.102.224.68.domain: 61158+% [1au] A?
us.co1.cb3.glbdns2.microsoft.com. (61)
11:39:26.110633 IP 202.102.224.68.domain > 192.168.1.241.25836: 61158 1/0/1 A
131.253.13.140 (77)
可以看到,隨機的轉發到202.102.227.68上,查詢到結果,解析出了www.msn.com
7. 輔助 DNS 服務器配置
場景:
爲了提高域名服務可靠性,防止單點失敗,企業新添加一個 DNS 服務器。由它作爲
abc.local 的輔助 DNS 服務器。
輔助名稱服務主機爲 ns2,IP 地址爲 192.168.1.242
7.1. DNS 組件安裝
與主 DNS 安裝類似.
# mount /dev/cdrom /mnt/cdrom/
# cd /mnt/cdrom/Packages/
# rpm -ivh bind-9.8.2-0.17.rc1.el6.x86_64.rpm \
bind-libs-9.8.2-0.17.rc1.el6.x86_64.rpm \
portreserve-0.0.4-9.el6.x86_64.rpm
7.2. 配置 Zone 傳遞
在 NS1 上修改配置主配置文件,添加 ns2 的 NS 及 AY 記錄。
[root@ns1 ~]# vi /var/named/abc.local.zone
$TTL 3H
@ IN SOA ns1.abc.local. admin.abc.local. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns1.abc.local.
NS ns2.abc.local.