DNS服务器搭建(Linux版本)

简介: DNS服务器搭建(Linux版本)

一、安装BIND

[root@server ~]# yum clean all
[root@server ~]# yum repolist
[root@server ~]# yum list | grep '^bind\.'
[root@server ~]# yum -y install bind*


二、配置主配置文件

  • 备份需配置的文件,防止配置当中出错。
[root@server ~]# cp /etc/named.conf /etc/named.conf.backup

3e81224606403df7548d78f292dd1b3e.png


  • 配置named.conf主配置文件

主要修改这两处信息。其余信息根据情况自行修改设置。

listen-on port 53 { any; };

allow-query { any; };

:wq保存退出

//
// 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
options {
        listen-on port 53 { any; };   #允许所有IP地址监听53号端口
        #listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        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";
        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;
        };
};
zone "." IN {
        type hint;
        file "named.ca";
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";


三、配置区域配置文件。添加正向解析配置。

在末尾添加如下配置。

vim /etc/named.rfc1912.zones

zone “xybdns.com” IN {

type master;

file “xybdns.com.zone”;

allow-update { none; };

按:wq保存退出

// 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 "xybdns.com" IN {   #正向解析为“pakho.com”
        type master;   #类型:主缓存为master
        file "xybdns.com.zone"; #指定区域数据文件为xybdns.com.zone
        allow-update { none; };
};


四、配置正向区域数据文件

  • 拷贝主配置文件,保留源文件的权限和属主的属性复制

cp -a named.localhost xybdns.com.zone

[root@server ~]# cd /var/named/
[root@server named]# cp -a named.localhost xybdns.com.zone
[root@server named]# ll
total 28
drwxr-x--- 7 root  named   61 Jul  9 05:18 chroot
drwxrwx--- 2 named named   49 Jul 20 03:11 data
-rw-r----- 1 root  named  259 Jul 14 03:42 dnsdiy.com.zone
drwxrwx--- 2 named named   31 Jul 20 01:25 dynamic
-rw-r----- 1 root  named 2253 Apr  5  2018 named.ca
-rw-r----- 1 root  named  152 Dec 15  2009 named.empty
-rw-r----- 1 root  named  152 Jun 21  2007 named.localhost
-rw-r----- 1 root  named  168 Dec 15  2009 named.loopback
drwxrwx--- 2 named named    6 Apr 29 10:05 slaves
-rw-r----- 1 root  named  515 Jul 20 04:26 xybdns.com.zone
-rw-r----- 1 root  named  538 Jul 14 03:53 xybdns.com.zone.bakup


  • 配置正向区域数据文件

注意:“.”的书写格式,其代替了@,别遗漏

[root@server named]# vim xybdns.com.zone  #进入配置文件
[root@server named]# cat xybdns.com.zone  #查看配置文件
$TTL 1D #有效解析记录的生成周期
@       IN SOA  xybdns.com. root.xybdns.com. (
#@表示当前的DNS区域名表示这个域名  SOA表示授权信息开启 后面表示邮件地址因为@有特殊含义 所以使用.代替 
                                        0       ; serial  #更新序列号,可以是10以内的整数
                                        1D      ; refresh #刷新时间,重新下载地址数据的间隔
                                        1H      ; retry   #重试延迟,下载失败后的重试延迟
                                        1W      ; expire #失效时间,超过该时间仍无法下载则放弃
                                        3H )    ; minimum #无效解析记录的生存周期
        IN      NS      server.xybdns.com.  #记录当前区域DNS服务器的名称
        IN      MX 10   server.xybdns.com.  #MX为邮件服务器 10表示优先级 数字越大优先级越低
server  IN      A       192.168.200.115   #记录正向解析域名对应的IP,即将域名与IP绑捆
web     IN      A       192.168.200.115
vsan7   IN      A       192.168.200.118


  • 修改主机名
[root@server ~]# hostnamectl set-hostname server.xybdns.com
[root@server ~]# bash
[root@server ~]# hostname
server.xybdns.com
  • 配置文件语法检查工具

named-checkconf -z /etc/named.conf

仅检查语法不检查逻辑关系。当显示的全为0时表示没有语法错误

[root@server ~]# named-checkconf -z /etc/named.conf
zone localhost.localdomain/IN: loaded serial 0
zone localhost/IN: loaded serial 0
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: loaded serial 0
zone 1.0.0.127.in-addr.arpa/IN: loaded serial 0
zone 0.in-addr.arpa/IN: loaded serial 0
zone xybdns.com/IN: loaded serial 0


五、启动DNS服务

  • 启动前,检查防火墙、SELINUX安全模式是否是关闭或允许状态

关闭防火墙并设置开机不自启动防火墙

[root@server ~]# systemctl stop firewalld && systemctl disable firewalld
[root@server ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

关闭SELINUX安全模式

[root@server ~]# cat /etc/selinux/config
# 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=disabled  #修改为disabled保存退出
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@server ~]# getenforce #重启生效
Disabled


  • 启动dns服务

systemctl start named

systemctl enable named

[root@server ~]# systemctl start named
[root@server ~]# systemctl enable named
[root@server ~]# systemctl status named

7d82680af785e66d40821103cbecd7b1.png


  • 查看53号监听端口是否开启

若执行不了netstat命令,请先输入yum install -y net-tools命令安装net-tools工具

netstat -anpt | grep 53

[root@server ~]# netstat -anpt | grep 53
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN      2416/named
tcp        0      0 192.168.200.115:53      0.0.0.0:*               LISTEN      2416/named
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      2416/named
tcp6       0      0 ::1:953                 :::*                    LISTEN      2416/named


六、测试DNS服务器

  • 在Windows 10环境下测试

设置所在网络配置,添加DNS服务器地址、默认网关等信息。如图所示。

e103908968da66a7b6c5e4235511a0be.png

e481d00de45980e81fda775b0a027b84.png

42d379969e60b5ca1dd8d81a5fd72311.png


  • 在linux环境下测试

设置dns

DNS=192.168.200.115

按:wq保存退出

[root@test ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=ens32
UUID=db4e154b-6cc7-420c-a43c-e5a27af7749d
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.200.120
NETMASK=255.255.255.0
GATEWAY=192.168.200.1
DNS=192.168.200.115


安装nslookup

yum provides nslookup

yum install -y bind-utils

[root@test ~]# yum provides nslookup
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
32:bind-utils-9.11.4-9.P2.el7.x86_64 : Utilities for querying DNS name servers
Repo        : centos
Matched from:
Filename    : /usr/bin/nslookup
32:bind-utils-9.11.4-9.P2.el7.x86_64 : Utilities for querying DNS name servers
Repo        : @centos
Matched from:
Filename    : /usr/bin/nslookup
[root@test ~]# yum install -y bind-utils
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package 32:bind-utils-9.11.4-9.P2.el7.x86_64 already installed and latest version
Nothing to do
[root@test ~]# ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=45.0 ms
^C
--- baidu.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 45.080/45.080/45.080/0.000 ms
[root@test ~]# ping server.xybdns.com
PING server.xybdns.com (192.168.200.115) 56(84) bytes of data.
64 bytes from 192.168.200.115 (192.168.200.115): icmp_seq=1 ttl=64 time=0.148 ms
64 bytes from 192.168.200.115 (192.168.200.115): icmp_seq=2 ttl=64 time=0.330 ms
^C
--- server.xybdns.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.148/0.239/0.330/0.091 ms
[root@test ~]# nslookup www.baidu.com
Server:         192.168.200.115
Address:        192.168.200.115#53
Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
Name:   www.a.shifen.com
Address: 180.101.49.11
Name:   www.a.shifen.com
Address: 180.101.49.12
[root@test ~]# nslookup server.xybdns.com
Server:         192.168.200.115
Address:        192.168.200.115#53
Name:   server.xybdns.com
Address: 192.168.200.115


相关文章
|
18天前
|
Linux
Linux下版本控制器(SVN) -服务器端环境搭建步骤
Linux下版本控制器(SVN) -服务器端环境搭建步骤
36 0
Linux下版本控制器(SVN) -服务器端环境搭建步骤
|
27天前
|
存储 安全 Ubuntu
从Linux到Windows:阿里云服务器系统镜像适配场景与选择参考
阿里云为用户提供了丰富多样的服务器操作系统选择,以满足不同场景下的应用需求。目前,云服务器的操作系统镜像主要分为公共镜像、自定义镜像、共享镜像、镜像市场和社区镜像五大类。以下是对这些镜像类型的详细介绍及选择云服务器系统时需要考虑的因素,以供参考。
|
1月前
|
Ubuntu Linux 网络安全
在Linux云服务器上限制特定IP进行SSH远程连接的设置
温馨提示,修改iptables规则时要格外小心,否则可能导致无法远程访问你的服务器。最好在掌握足够技术知识和理解清楚操作含义之后再进行。另外,在已经配置了防火墙的情况下,例如ufw(Ubuntu Firewall)或firewalld,需要按照相应的防火墙的规则来设置。
105 24
|
1月前
|
消息中间件 NoSQL Linux
Redis的基本介绍和安装方式(包括Linux和Windows版本),以及常用命令的演示
Redis(Remote Dictionary Server)是一个高性能的开源键值存储数据库。它支持字符串、列表、散列、集合等多种数据类型,具有持久化、发布/订阅等高级功能。由于其出色的性能和广泛的使用场景,Redis在应用程序中常作为高速缓存、消息队列等用途。
448 16
|
1月前
|
数据挖掘 Linux 数据库
服务器数据恢复—Linux系统服务器数据恢复案例
服务器数据恢复环境: linux操作系统服务器中有一组由4块SAS接口硬盘组建的raid5阵列。 服务器故障: 服务器工作过程中突然崩溃。管理员将服务器操作系统进行了重装。 用户方需要恢复服务器中的数据库、办公文档、代码文件等。
|
2月前
|
安全 Linux
阿里云linux服务器使用脚本通过安全组屏蔽异常海外访问ip
公网网站可能会遭受黑客攻击导致访问异常,使用此脚本可以屏蔽掉异常IP 恢复访问。也可自行设置定时任务定期检测屏蔽。
239 28
|
2月前
|
关系型数据库 MySQL Linux
在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾
以上就是在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾的步骤。这个过程就像是一场接力赛,数据从MySQL数据库中接力棒一样传递到备份文件,再从备份文件传递到其他服务器,最后再传递回MySQL数据库。这样,即使在灾难发生时,我们也可以快速恢复数据,保证业务的正常运行。
162 28
|
2月前
|
JavaScript Linux Python
在Linux服务器中遇到的立即重启后的绑定错误:地址已被使用问题解决
总的来说,解决"地址已被使用"的问题需要理解Linux的网络资源管理机制,选择合适的套接字选项,以及合适的时间点进行服务重启。以上就是对“立即重启后的绑定错误:地址已被使用问题”的全面解答。希望可以帮你解决问题。
176 20
|
3月前
|
Ubuntu Linux
Linux系统管理:服务器时间与网络时间同步技巧。
以上就是在Linux服务器上设置时间同步的方式。然而,要正确运用这些知识,需要理解其背后的工作原理:服务器根据网络中的其他机器的时间进行校对,逐步地精确自己的系统时间,就像一只犹豫不决的啮齿动物,通过观察其他啮齿动物的行为,逐渐确定自己的行为逻辑,既简单,又有趣。最后希望这个过程既能给你带来乐趣,也能提高你作为系统管理员的专业素养。
408 20
|
1月前
|
Ubuntu 搜索推荐 Linux
详解Ubuntu的strings与grep命令:Linux开发的实用工具。
这就是Ubuntu中的strings和grep命令,透明且强大。我希望你喜欢这个神奇的世界,并能在你的Linux开发旅程上,通过它们找到你的方向。记住,你的电脑是你的舞台,在上面你可以做任何你想做的事,只要你敢于尝试。
122 32

相关产品

  • 云解析DNS