DNS是域名系統(tǒng)(Domain Name System)的縮寫,是因特網(wǎng)的一項核心服務(wù),它能提供域名與IP地址之間對應(yīng)關(guān)系的轉(zhuǎn)換服務(wù)。
這樣我們就可以更方便地去訪問互聯(lián)網(wǎng)了,不用去記住那一串IP數(shù)字。本文檔主要是說明如何把一臺centos主機(jī)配置成一個DNS
服務(wù)器,以便能提供域名解析服務(wù)。
(1) DNS服務(wù)器端配置
操作系統(tǒng):CentOS 6.4
IP地址:172.16.1.4
DNS軟件:Bind 9.8
測試域名:realhostip.com
作用:主要提供解析realhostip.com域名的服務(wù)
1. 安裝bind
# yum install bind
2. 修改/etc/named.conf配置文件
# vi /etc/named.conf
[html] view plaincopy
- //
- // named.conf
- //
- // Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
- // server as a caching only nameserver (as a localhost DNS resolver only).
- //
- // See /usr/share/doc/bind*/sample/ for example named configuration files.
- //
- options {
- listen-on port 53 { any; }; //開啟監(jiān)聽端口53,接受任意IP連接
- listen-on-v6 port 53 { ::1; }; //支持IP V6
- directory "/var/named"; //所有的正向反向區(qū)域文件都在這個目錄下創(chuàng)建
- dump-file "/var/named/data/cache_dump.db";
- statistics-file "/var/named/data/named_stats.txt";
- memstatistics-file "/var/named/data/named_mem_stats.txt";
- allow-query { 0.0.0.0/0; }; //允許任意IP查詢
- recursion yes;
- dnssec-enable yes;
- dnssec-validation yes;
- dnssec-lookaside auto;
- /* Path to ISC DLV key */
- bindkeys-file "/etc/named.iscdlv.key";
- managed-keys-directory "/var/named/dynamic";
- };
- logging {
- channel default_debug {
- file "data/named.run";
- severity dynamic;
- };
- };
- zone "." IN {
- type hint;
- file "named.ca";
- };
- include "/etc/named.rfc1912.zones"; //主要配置文件
- include "/etc/named.root.key";
3. 修改/etc/named.rfc1912.zones文件,添加realhostip.com的正向和反向區(qū)域
# vi /etc/ named.rfc1912.zones
[html] view plaincopy
- // named.rfc1912.zones:
- //
- // Provided by Red Hat caching-nameserver package
- //
- // ISC BIND named zone configuration for zones recommended by
- // RFC 1912 section 4.1 : localhost TLDs and address zones
- // and
- // (c)2007 R W Franks
- //
- // See /usr/share/doc/bind*/sample/ for example named configuration files.
- //
- zone "localhost.localdomain" IN {
- type master;
- file "named.localhost";
- allow-update { none; };
- };
- zone "localhost" IN {
- type master;
- file "named.localhost";
- allow-update { none; };
- };
- zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" IN {
- type master;
- file "named.loopback";
- allow-update { none; };
- };
- zone "1.0.0.127.in-addr.arpa" IN {
- type master;
- file "named.loopback";
- allow-update { none; };
- };
- zone "0.in-addr.arpa" IN {
- type master;
- file "named.empty";
- allow-update { none; };
- };
- //realhostip.com的正向區(qū)域
- zone "realhostip.com" IN {
- type master;
- file "named.realhostip.com";
- allow-update { none; };
- };
- //realhostip.com的反向區(qū)域
- zone "1.16.172.in-addr.arpa" IN {
- type master;
- file "172.16.1.arpa";
- allow-update { none; };
- };
這里需要注意的是,添加反向區(qū)域時,網(wǎng)絡(luò)號要反過來寫(網(wǎng)絡(luò)號是IP地址與子網(wǎng)掩碼進(jìn)行與操作后的結(jié)果)。
例如,我現(xiàn)在配置的網(wǎng)絡(luò)號172.16.1這個網(wǎng)段,那么它的反向區(qū)域是1.16.172.in-addr.arpa。區(qū)域里面的file
字段表明解析時的數(shù)據(jù)來源文件,接下來我們?nèi)?chuàng)建named.realhostip.com和172.16.1.arpa文件。
4. 創(chuàng)建正向和反向區(qū)域資源文件
在配置named.conf時,指明的資源文件目錄是/var/named,故先進(jìn)入該目錄。
# cd /var/named
# vi named.realhostip.com
[html] view plaincopy
- $TTL 1D
- @ IN SOA realhostip.com. rname.invalid. (
- 0 ; serial
- 1D ; refresh
- 1H ; retry
- 1W ; expire
- 3H ) ; minimum
- NS @
- A 127.0.0.1
- AAAA ::1
- 172-16-1-50 IN A 172.16.1.50
- 172-16-1-51 IN A 172.16.1.51
以上我添加了兩條記錄,其中172-16-1-50 IN A 172.16.1.50表明域名172-16-1-50.realhostip.com對應(yīng)的IP地址為172.16.1.50。
如果需要添加多條,按此類似添加,留意realhostip.com后面的那個不起眼的點(.)。
# vi 172.16.1.arpa
[html] view plaincopy
- $TTL 1D
- @ IN SOA realhostip.com. rname.invalid. (
- 0 ; serial
- 1D ; refresh
- 1H ; retry
- 1W ; expire
- 3H ) ; minimum
- NS @
- AAAA ::1
- 50 PTR 172-16-1-50.realhostip.com.
- 51 PTR 172-16-1-51.realhostip.com.
以上我也添加了兩條記錄,其中50 PTR 172-16-1-50.realhostip.com表明IP地址172.16.1.50對應(yīng)的
域名為172-16-1-50.realhostip.com。如果要添加多條,按此類似添加,留意realhostip.com后面的那個不起眼的點(.)。
5. 啟動named服務(wù)
#service named start
至此,DNS服務(wù)器端的配置已完成,下面我們稍微配置一下客戶端來測試我們的DNS服務(wù)器是否正常工作。
(2) 客戶端配置
操作系統(tǒng):windows和linux都可以,我這里是CentOS 6.4
IP地址:能夠ping通DNS服務(wù)器的IP(172.16.1.4)都可以,我這里是172.16.1.104
作用:測試DNS服務(wù)器是否正常工作。
1. 安裝bind-utils包,以便能使用nslookup、dig和host工具
yum install bind-utils
2. 修改DNS配置使用我們的DNS服務(wù)器
vi /etc/resolv.conf
[html] view plaincopy
- nameserver 172.16.1.4
- nameserver 192.168.13.31
- nameserver 172.16.1.1
如果是windows客戶端,需要在本地連接屬性里面修改,如下圖:
resolv.conf文件中可能會有多個nameserver,必須把我們的DNS服務(wù)器放在所有nameserver的最前面,
這樣當(dāng)需要解析域名時,第一個使用的就是我們配置的DNS服務(wù)器,其它的都是候選項。
3. 正向解析測試,使用nslookup命令
#nslookup
[html] view plaincopy
- > 172-16-1-50.realhostip.com
- Server: 172.16.1.4
- Address: 172.16.1.4#53
- Name: 172-16-1-50.realhostip.com
- Address: 172.16.1.50
- >
- > 172-16-1-51.realhostip.com
- Server: 172.16.1.4
- Address: 172.16.1.4#53
- Name: 172-16-1-51.realhostip.com
- Address: 172.16.1.51
- >
從結(jié)果可以看到,我們配置的兩個域名都能成功解析,并且DNS服務(wù)器就是我們配置的那個服務(wù)器。
4. 反向解析,使用nslookup命令
#nslookup
[html] view plaincopy
- >
- > 172.16.1.51
- Server: 172.16.1.4
- Address: 172.16.1.4#53
- 51.1.16.172.in-addr.arpa name = 172-16-1-51.realhostip.com.
- >
- >
- > 172.16.1.50
- Server: 172.16.1.4
- Address: 172.16.1.4#53
- 50.1.16.172.in-addr.arpa name = 172-16-1-50.realhostip.com.
- >
- >
從結(jié)果來看,可以正確解析我們的IP地址,并且DNS服務(wù)器就是我們配置的那個服務(wù)器。
5. 查看realhostip.com這個域名是哪個DNS服務(wù)器管理的,使用dig命令
# dig -t ns realhostip.com
[html] view plaincopy
- ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> -t ns realhostip.com
- ;; global options: +cmd
- ;; Got answer:
- ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37964
- ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 2
- ;; QUESTION SECTION:
- ;realhostip.com. IN NS
- ;; ANSWER SECTION:
- realhostip.com. 86400 IN NS realhostip.com.
- ;; ADDITIONAL SECTION:
- realhostip.com. 86400 IN A 172.16.1.4
- realhostip.com. 86400 IN AAAA ::1
- ;; Query time: 1 msec
- ;; SERVER: 172.16.1.4#53(172.16.1.4)
- ;; WHEN: Wed Oct 23 14:15:22 2013
- ;; MSG SIZE rcvd: 90
6. 使用dig命令進(jìn)行正向解析
# dig 172-16-1-50.realhostip.com
[html] view plaincopy
- ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> 172-16-1-50.realhostip.com
- ;; global options: +cmd
- ;; Got answer:
- ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21109
- ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
- ;; QUESTION SECTION:
- ;172-16-1-50.realhostip.com. IN A
- ;; ANSWER SECTION:
- 172-16-1-50.realhostip.com. 86400 IN A 172.16.1.50
- ;; AUTHORITY SECTION:
- realhostip.com. 86400 IN NS realhostip.com.
- ;; ADDITIONAL SECTION:
- realhostip.com. 86400 IN A 172.16.1.4
- realhostip.com. 86400 IN AAAA ::1
- ;; Query time: 1 msec
- ;; SERVER: 172.16.1.4#53(172.16.1.4)
- ;; WHEN: Wed Oct 23 14:17:57 2013
- ;; MSG SIZE rcvd: 118
注意:
windows客戶端上只有nslookup工具。