本章我们来搭建一个局域网内的域名解析服务器试试,让大家先从主观上感受下bind 的实际用处是什么

1)环境准备:

准备两台机器

1,192.168.56.6(alias s6) #装有bind服务,作为域名服务器使用

2, 192.168.56.7 (alias s7) #跟域名服务器属于同一网段,可以相互访问,作为测试机器,将其域名解析服务的地址指向第一台服务器

2)安装bind

yum install bind bind-chroot bind-utils -y

 我们参照第一章的内容安装这三个软件包

3)配置named.conf文件

//
// 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.
//
// See the BIND Administrator's Reference Manual (ARM) for details about the
// configuration located in /usr/share/doc/bind-{version}/Bv9ARM.html
#全局配置段定义named进程的工作特性
options {
    listen-on port 53 { any; };  ##修改为网卡的实际地址或者写为any,但端口一定别改
    listen-on-v6 port 53 { ::1; };  #ipv6的地址
    directory     "/var/named";  #zone数据文件存放路径
    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";
    recursing-file  "/var/named/data/named.recursing";
    secroots-file   "/var/named/data/named.secroots";
    #对所有ip提供域名解析
    allow-query     { any; };   

    /* 
     - If you are building an AUTHORITATIVE DNS server, do NOT enable recursion.
     - If you are building a RECURSIVE (caching) DNS server, you need to enable 
       recursion. 
     - If your recursive DNS server has a public IP address, you MUST enable access 
       control to limit queries to your legitimate users. Failing to do so will
       cause your server to become part of large scale DNS amplification 
       attacks. Implementing BCP38 within your network would greatly
       reduce such attack surface 
    */
    recursion yes;

    dnssec-enable yes;
    dnssec-validation yes;

    /* Path to ISC DLV key */
    bindkeys-file "/etc/named.root.key";

    managed-keys-directory "/var/named/dynamic";

    pid-file "/run/named/named.pid";
    session-keyfile "/run/named/session.key";
};
#定义日志功能
logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};
#定义本named进程负责解析的区域;zone可以有多个
zone "." IN {
    type hint;
    file "named.ca";
};

include "/etc/named.rfc1912.zones";  #zone 文件除了可以直接放在主配置文件外,也可放在此文件中,一般我们不会将区域数据文件放入主配置文件当中,而是选择直接将文件放在此文件中
include "/etc/named.root.key";

修改完配置文件可以用以下命令进行检查:

named-checkconf #不能检查zone文件的正确性,默认检查 named.conf 和 named.rfc1912.zones 文件
named-checkzone #可以用来检查zone文件的正确性

启动服务:

systemctl start named

查看运行状态:

[root@bogon etc]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      15595/named         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      919/sshd            
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN      15595/named         
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1143/master         
tcp6       0      0 ::1:53                  :::*                    LISTEN      15595/named         
tcp6       0      0 :::22                   :::*                    LISTEN      919/sshd            
tcp6       0      0 ::1:953                 :::*                    LISTEN      15595/named         
tcp6       0      0 ::1:25                  :::*                    LISTEN      1143/master         
[root@bogon etc]# 

可以看到bind服务已经运行起来了

4)配置dns服务器成为某区域的主服务器:

1,在主配置文件(/etc/named.rfc1912.zones)中添加zone,内容如下:

// 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 http://www.ietf.org/internet-drafts/draft-ietf-dnsop-default-local-zones-02.txt
// (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; };
};

zone "repo.com" IN {
        type master;
        file "named/repo.com.zone";   #定义区域数据文件,在yum安装的情况下,默认存放于/var/named下 我们这里是定义在了 /var/named/named/repo.com.zone
};

2,编写区域数据文件repo.com.zone, 文件内容如下:

$TTL    1D
@    IN    SOA    @ repo.com(
                  2        ;
             604800        ; 
              86400        ; 
            2419200        ; 
             604800 )    ; 
            IN  NS  ns             #至少有一条NS记录,其指向下面的A记录表名此域名解析的域名服务器地址,如果有多个域名服务器,则需要指明多个NS记录和其对应的A记录
ns         IN  A   192.168.56.6  
www     IN  A   192.168.56.6       #我们自己定义的www.repo.com FQDN A记录
注:
(1) "$"符号:定义宏。最常见的是"$TTL"、"$ORIGIN"。
(2) FQDN自动补齐:在区域数据文件中,没有使用点号"."结尾的,在实际使用的时候都会自动补上域名,使其变为 FQDN。
    例如:上面文件中的 ns,会自动补全为 ns.repo.com.
(3) 若上一条记录与下一条记录主机名相同,则下一条可以省略,默认为上一条的主机名。如上文件中 www下为空,默认为下一条主机名也为www。
(4) "@" 默认代表域名。

完成编写后我们执行命令检查下文件是否符合格式需求:

[root@bogon named]# named-checkzone repo.com repo.com.zone 
zone repo.com/IN: loaded serial 2
OK
[root@bogon named]# 

文件格式正确,重启bind服务

systemctl restart named

至此我们所有配置已经完成,而后我们通过dig命令去测试其结果:

3,测试:

测试前请先关闭防火墙,或修改iptables INPUT链,使其开放53,953端口,这里我们直接关闭防火墙

dig 命令需要自行安装,它存放于bind-utils工具包中,如果前面已经安装过这步可以省略。

[root@bogon named]# dig @192.168.56.6 www.repo.com

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.3 <<>> @192.168.56.6 www.repo.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41413
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2

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

;; ANSWER SECTION:
www.repo.com.        86400    IN    A    192.168.56.6

;; AUTHORITY SECTION:
repo.com.        86400    IN    NS    ns.repo.com.

;; ADDITIONAL SECTION:
ns.repo.com.        86400    IN    A    192.168.56.6

;; Query time: 0 msec
;; SERVER: 192.168.56.6#53(192.168.56.6)
;; WHEN: 一 1月 11 19:33:36 CST 2021
;; MSG SIZE  rcvd: 90

可以看到我们正常解析了www.repo.com域名所对应的ip地址。

现在我们再修改s7上的域名解析服务地址,让其使用此域名服务进行域名解析

修改文件:/etc/resolv.conf,内容如下:

# Generated by NetworkManager
nameserver 192.168.56.6

在s7上执行解析操作:

[root@bogon ~]# dig www.repo.com

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.3 <<>> www.repo.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 49350
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2

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

;; ANSWER SECTION:
www.repo.com.        86400    IN    A    192.168.56.6

;; AUTHORITY SECTION:
repo.com.        86400    IN    NS    ns.repo.com.

;; ADDITIONAL SECTION:
ns.repo.com.        86400    IN    A    192.168.56.6

;; Query time: 10 msec
;; SERVER: 192.168.56.6#53(192.168.56.6)
;; WHEN: 一 1月 11 19:40:08 CST 2021
;; MSG SIZE  rcvd: 90

而后你可以尝试从s7 ssh 到此域名下试试

ssh root@www.repo.com

可以看到能正常解析,至此我们的入门教程就结束了,后面我会继续更新细节的东西。

 

posted on 2021-01-11 17:00  EZgod  阅读(597)  评论(0编辑  收藏  举报