如何在Ubuntu和LinuxMint上配置DNS服务器

时间:2019-05-19 01:25:44  来源:igfitidea点击:

Domain Name System域名系统 DNS是一种用于网络连接的计算机的分级分布式命名系统。
它将ip地址对应解析为一个域名。

本文将在Ubuntu、Debian和LinuxMint系统上使用Bind包配置dns服务器(域名系统)。
Bind也称为命名服务。
大多数web主机提供商已经为他们的客户端提供了dns服务器。

安装DNS包

Bind9是全世界最流行的dns服务器。
它在默认的apt-get存储库中。
使用以下命令安装Bind9域名系统。

$ sudo apt-get update
$ sudo apt-get install bind9

创建转发区域文件

因为我们使用临时域名demotheitroad.com。
在/etc/bind目录下创建 dns forward区域文件。

$ sudo vi /etc/bind/demotheitroad.com.zone

并添加以下内容

; Forward Zone file for demotheitroad.com
$TTL 14400
@      86400    IN      SOA     ns1.theitroad.com. webmaster.theitroad.com. (
                3013040200      ; serial, todays date+todays
                86400           ; refresh, seconds
                7200            ; retry, seconds
                3600000         ; expire, seconds
                86400          ; minimum, seconds
      )
demotheitroad.com.   86400  IN NS ns1.theitroad.com.
demotheitroad.com.   86400  IN NS ns2.theitroad.com.
demotheitroad.com.   IN   A     192.168.0.100
demotheitroad.com.   IN   MX  0 mail.demotheitroad.com.
mail 		    IN   A     192.168.0.200
www 		    IN   CNAME demotheitroad.com.

创建反向区域文件

如果需要,还为ip地址和域名配置rDNS区域,以解析反向dns。
例如,我们在内部网中使用192.168.0.0/32 ip范围。
创建反向dns文件/etc/bind/db.0.168.192。

$ sudo vi /etc/bind/db.0.168.192

包含以下内容

; BIND reverse data file for local loopback interface
;
$TTL    604800
@ IN SOA ns1.theitroad.com. root.ns1.theitroad.com. (
                     3013040200         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
@       IN      NS      ns1.theitroad.com.

100     IN      PTR     demotheitroad.com.
101	IN 	PTR	otherdomain.com.

在主配置中添加区域条目

现在编辑绑定配置文件(/etc/bind/name .conf.local)并添加正向和反向区域条目,如下所示。

$ sudo vi /etc/bind/named.conf.local

添加以下内容

zone "demotheitroad.com" IN {
        type master;
        file "/etc/bind/demotheitroad.com.zone";
};

zone "0.168.192.in-addr.arpa" {
        type master;
        file "/etc/bind/db.0.168.192";
};

验证配置文件

完成所有配置后,使用以下命令验证所有文件。

$ named-checkzone demotheitroad.com /etc/bind/demotheitroad.com.zone
$ named-checkzone 192.168.0.0/32 /etc/bind/db.0.168.192
$ named-checkconf  /etc/bind/named.conf.local
$ named-checkconf  /etc/bind/named.conf

重启DNS

在成功地验证了所有文件之后,让我们重新启动bind9服务。

$ sudo service bind9 restart

测试设置

最后,dns服务器成功配置并可以使用了。
确保客户端系统使用dns服务器作为默认dns服务器。
让我们验证DNS是否正确响应查询。
下面是测试它的示例命令。
验证 Forward区:

$   **<orange>dig demotheitroad.com</orange>**  

; <<>> DiG 9.9.5-3ubuntu0.5-Ubuntu <<>> demotheitroad.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 58754
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;demotheitroad.com.		IN	A

;; ANSWER SECTION:
demotheitroad.com.	14400	IN	A	192.168.0.100

;; AUTHORITY SECTION:
demotheitroad.com.	86400	IN	NS	ns1.theitroad.com.
demotheitroad.com.	86400	IN	NS	ns2.theitroad.com.

;; Query time: 0 msec
;; SERVER: 192.168.0.60#53(192.168.0.60)
;; WHEN: Thu Oct 08 10:33:17 IST 2015
;; MSG SIZE  rcvd: 106

验证反向区域:

$   **<orange>dig -x 192.168.0.100</orange>**  

; <<>> DiG 9.9.5-3ubuntu0.5-Ubuntu <<>> -x 192.168.0.100
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1132
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;100.0.168.192.in-addr.arpa.	IN	PTR

;; ANSWER SECTION:
100.0.168.192.in-addr.arpa. 604800 IN	PTR	demotheitroad.com.

;; AUTHORITY SECTION:
0.168.192.in-addr.arpa.	604800	IN	NS	ns1.theitroad.com.

;; Query time: 0 msec
;; SERVER: 192.168.0.60#53(192.168.0.60)
;; WHEN: Thu Oct 08 10:34:06 IST 2015
;; MSG SIZE  rcvd: 112