Ubuntu搭建DNS服务器

下载bind9

  1. 执行如下命令

    apt-get install bind9

编辑bind9配置文件

  1. 找到/etc/bind/name.conf.default-zones文件
    并在末尾添加

    zone "www.domainname.com" {
         type master;
         file "/etc/bind/db.ip2domainname.com";
    };
     
    zone "jkl.ghi.def.abc.in-addr.arpa" {
         type master;
         file "/etc/bind/db.domainname2ip";
    };

    其中jkl.ghi.def.abc为服务器ip的反写

  2. 然后在/etc/bind中创建db.ip2domainname.com文件和db.domainname2ip文件,注意这两个文件名要和name.conf.default-zones中配置的file相同
  3. 然后在db.ip2domainname.com文件中填写如下内容

    ;
    ; BIND data file for local loopback interface
    ;
    $TTL    604800
    @       IN      SOA     ns.www.domainname.com. root.www.domainname.com. (
                               2         ; Serial
                          604800         ; Refresh
                           86400         ; Retry
                         2419200         ; Expire
                          604800 )       ; Negative Cache TTL
    ;
    @       IN      NS      ns.www.domainname.com.
    @       IN      A       abc.def.ghi.jkl
    ns      IN      A       abc.def.ghi.jkl
    cn      IN      A       abc.def.ghi.jkl

    注意:需要把下面的www.domainname.com换成你的域名,不要漏掉了域名后面的小数点,abc.def.ghi.jklwww.domainname.com服务器的ip,需要替换成你自己的。

  4. 然后在db.domainname2ip文件中填写如下内容,同样要替换www.domainname.com为你的域名。100是www.domainname.com服务器的ip的最后一段,需要替换成你自己的。

     ;
     ; BIND reverse data file for local loopback interface
     ;
     $TTL    604800
     @       IN      SOA     ns.www.domainname.com. root.www.domainname.com. (
                                1         ; Serial
                           604800         ; Refresh
                            86400         ; Retry
                          2419200         ; Expire
                           604800 )       ; Negative Cache TTL
     ;
     @       IN      NS      www.domainname.com.
     1.0.0   IN      PTR     cn.domainname.com.
     100     IN      PTR     domainname.com.
     100     IN      PTR     www.domainname.com.
     100     IN      PTR     dns.domainname.com.
     100     IN      PTR     cn.domainname.com.
  5. 然后修改/etc/bind/named.conf.options文件为如下内容

    options {
     directory "/var/cache/bind";
     
     // If there is a firewall between you and nameservers you want
     // to talk to,you may need to fix the firewall to allow multiple
     // ports to talk. See http://www.kb.cert.org/vuls/id/800113
     
     // If your ISP provided one or more IP addresses for stable 
     // nameservers,you probably want to use them as forwarders. 
     // Uncomment the following block,and insert the addresses replacing 
     // the all-0's placeholder.
     //forward first;  
     forwarders {
         223.5.5.5;
         223.6.6.6;
     };
     
     //========================================================================
     // If BIND logs error messages about the root key being expired,
     // you will need to update your keys. See https://www.isc.org/bind-keys
     //========================================================================
     listen-on{ any; };
     recursion yes;
     allow-query { any; };
    };
  6. 然后执行service bind9 restart重启bind9 DNS服务器
  7. 接下来还需要修改路由器配置,把DNS服务器ip换成自己搭建的DNS服务器的ip即可
文章目录