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


相关文章
|
4月前
|
Ubuntu Linux
Ubuntu 23.04 用上 Linux 6.2 内核,预计下放到 22.04 LTS 版本
Linux 6.2 带来了多项内容更新,修复了 AMD 锐龙处理器设备在启用 fTPM 后的运行卡顿问题,还增强了文件系统。
|
4月前
|
Ubuntu Linux
Ubuntu24.04LTS默认采用Linux 6.8内核,实验性版本可通过PPA获得
IT之家提醒,当下的 Ubuntu 23.10 也是一个“短期支持版本”,该版本将在今年 7 月终止支持,而今年 4 月推出的 Ubuntu 24.04 LTS 长期支持版本将获得 5 年的更新支持。
|
4月前
|
弹性计算 安全 Linux
阿里云服务器ECS安装宝塔Linux面板、安装网站(新手图文教程)
本教程详解如何在阿里云服务器上安装宝塔Linux面板,涵盖ECS服务器手动安装步骤,包括系统准备、远程连接、安装命令执行、端口开放及LNMP环境部署,手把手引导用户快速搭建网站环境。
|
5月前
|
Java Linux 网络安全
Linux云端服务器上部署Spring Boot应用的教程。
此流程涉及Linux命令行操作、系统服务管理及网络安全知识,需要管理员权限以进行配置和服务管理。务必在一个测试环境中验证所有步骤,确保一切配置正确无误后,再将应用部署到生产环境中。也可以使用如Ansible、Chef等配置管理工具来自动化部署过程,提升效率和可靠性。
570 13
|
4月前
|
Ubuntu 安全 小程序
linux|ubuntu.v18.10版本即将发布,linux桌面让您动心
如果你使用闭源系统,那永远也就别想了!有了这样的需求,也许最终将linux带到人类大众通用市场的是我们中国!
133 0
|
4月前
|
Web App开发 Ubuntu Linux
又该换Linux版本了!
如果你经常用谷歌搜索,使用终端输入命令,推荐你使用Fedora而不是Ubuntu。 如果你不是一个技术用户或程序员,仍推荐使用Ubuntu,还不动手去试试,别忘了将你的体验留在评论区哦~
|
4月前
|
Ubuntu 安全 小程序
服务器版本的CentOS和Ubuntu哪个更适合你?
但是以上的比较并不说明Ubuntu是不稳定的或者是不安全的,只是以上比较过程中,在稳定性方面Ubuntu稍微逊色了一点。由于Ubuntu在个人桌面电脑的使用率远远高于CentOS,用Ubuntu搭建服务器,如果遇到什么问题,寻找解决方案相对比较容易,这让Ubuntu在选择方面更优于CentOS。如果你是一个初学者,那么毫无疑问Ubuntu是更适合的选择。如果你正在经营自己的公司,在这两者之间,CentOS会更好一些。
|
5月前
|
监控 Linux 网络安全
FinalShell SSH工具下载,服务器管理,远程桌面加速软件,支持Windows,macOS,Linux
FinalShell是一款国人开发的多平台SSH客户端工具,支持Windows、Mac OS X和Linux系统。它提供一体化服务器管理功能,支持shell和sftp同屏显示,命令自动提示,操作便捷。软件还具备加速功能,提升访问服务器速度,适合普通用户和专业人士使用。
1513 0
|
5月前
|
Linux Docker Windows
windows docker安装报错适用于 Linux 的 Windows 子系统必须更新到最新版本才能继续。可通过运行 “wsl.exe --update” 进行更新。
适用于 Linux 的 Windows 子系统需更新至最新版本(如 wsl.2.4.11.0.x64.msi)以解决 2025 年 Windows 更新后可能出现的兼容性问题。用户可通过运行 “wsl.exe --update” 或访问提供的链接下载升级包进行更新。
1887 0

相关产品

  • 云解析DNS