Dns_在Ubuntu和Centos上安装并配置Dns服务器
- 一、Ubuntu上安装Dns
- 1.安装 BIND9
- 2.配置 DNS 服务器
- 3.配置防火墙(如果启用 UFW)
- 4.在客户端使用 DNS 服务器
- 5.测试 DNS 服务器
- 6.常见问题及解决方案
- 二、Centos上安装Dns
以下记录在Ubuntu和Centos系统上安装Dns服务器的过程
一、Ubuntu上安装Dns
在 Ubuntu 上安装并配置 DNS 服务器可以通过 BIND9(Berkeley Internet Name Domain)实现,这是 Linux 系统中最常用的 DNS 服务器软件。以下是如何安装、配置和使用 DNS 服务器的完整流程。
1.安装 BIND9
- 1.更新软件包列表并安装 BIND9
sudo apt update
sudo apt install bind9 bind9utils bind9-doc -y
- 2.检查 BIND9 服务状态:
sudo systemctl status bind9
如果服务没有启动,使用以下命令启动:
sudo systemctl start bind9
2.配置 DNS 服务器
假设你想创建一个本地域名 example.local 并将其映射到 192.168.1.10。
- 1.编辑 BIND 配置文件
打开 /etc/bind/named.conf.local:
sudo vi /etc/bind/named.conf.local
添加以下区域配置:
zone "example.local" {type master;file "/etc/bind/db.example.local";
};
- 2.创建区域数据库文件: 基于 /etc/bind/db.local 创建 db.example.local:
sudo cp /etc/bind/db.local /etc/bind/db.example.local
sudo vi /etc/bind/db.example.local
修改内容如下:
; db.example.local
$TTL 604800
@ IN SOA ns.example.local. admin.example.local. (1 ; Serial604800 ; Refresh86400 ; Retry2419200; Expire604800 ); Minimum TTL@ IN NS ns.example.local.
ns IN A 192.168.1.10
www IN A 192.168.1.10
- 3.检查配置文件是否正确:
sudo named-checkconf
sudo named-checkzone example.local /etc/bind/db.example.local
- 4.重启 BIND9 服务:
sudo systemctl restart bind9
3.配置防火墙(如果启用 UFW)
确保允许 DNS 流量(53 端口):
sudo ufw allow 53/tcp
sudo ufw allow 53/udp
sudo ufw reload
4.在客户端使用 DNS 服务器
- 1.修改 Ubuntu 自己的 DNS 设置(作为客户端): 编辑 /etc/systemd/resolved.conf:
sudo vi /etc/systemd/resolved.conf
在 [Resolve] 部分添加你的 DNS 服务器 IP:
[Resolve]
DNS=127.0.0.1
- 2.重启网络服务:
[Resolve]
sudo systemctl restart systemd-resolved
- 3.在其他客户端(如 Android、Windows)配置 DNS:
设置 DNS 地址为 Ubuntu 服务器的 IP(如 192.168.1.10)。
5.测试 DNS 服务器
- 使用 nslookup 测试:
nslookup www.example.local 127.0.0.1
注:如果nslookup找不到, 执行sudo apt install dnsutils -y安装
- 使用 dig 命令测试:
dig @127.0.0.1 www.example.local
如果解析正确,你会看到类似以下输出:
;; ANSWER SECTION:
www.example.local. 604800 IN A 192.168.1.10
6.常见问题及解决方案
- 解析失败:
确保 BIND9 服务正在运行:
sudo systemctl status bind9
检查防火墙是否允许 53 端口。
- DNS 缓存问题:
清除本地 DNS 缓存:
sudo systemd-resolve --flush-caches
- 区域文件语法错误:
使用以下命令检查语法:
sudo named-checkzone example.local /etc/bind/db.example.local
二、Centos上安装Dns
参考:https://www.jianshu.com/p/ceaa2cc5715c
0.Dns搭建####安装 BIND 服务器软件并启动
yum -y install bind bind-utils
systemctl start named.service // 启动服务
systemctl enable named // 设为开机启动####查看named进程是否正常启动
ps -eaf|grep named // 检查进程
ss -nult|grep :53 // 检查监听端口####开放 TCP 和 UDP 的 53 端口
firewall-cmd --permanent --add-port=53/tcp
firewall-cmd --permanent --add-port=53/udp
firewall-cmd --reload // 重新加载防火墙配置,让配置生效####修改主要文件 /etc/named.conf修改前先备份: cp -p /etc/named.conf /etc/named.conf.bak // 参数-p表示备份文件与源文件的属性一致。
修改配置:vi /etc/named.conf , 配置内容如下:cat > /etc/named.conf << EOF
//
// 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.htmloptions {listen-on port 53 { any; };listen-on-v6 port 53 { any; };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 willcause your server to become part of large scale DNS amplification attacks. Implementing BCP38 within your network would greatlyreduce 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";
EOF####// 检查named.conf是否有语法问题
named-checkconf####配置正向解析和反向解析
####修改/etc/named.rfc1912.zonescat >> /etc/named.rfc1912.zones << EOFzone "reading.zt" IN {type master;file "named.reading.zt";allow-update { none; };
};zone "56.168.192.in-addr.arpa" {type master;file "named.192.168.56";allow-update { none; };
};
EOF####添加正向解析域
cp -p /var/named/named.localhost /var/named/named.reading.ztcat >> /var/named/named.reading.zt << EOF
web A 192.168.56.102
web A 192.168.56.103
test A 192.168.56.101
EOF说明:http://test.reading.zt/ 将会解析为 http://192.168.56.101/####授权 named 用户
chown :named /var/named/named.reading.zt####检查区域文件是否正确
named-checkzone "reading.zt" "/var/named/named.reading.zt"[root@master1 ~]# named-checkzone "reading.zt" "/var/named/named.reading.zt"
zone reading.zt/IN: loaded serial 0
OK####添加反向解析域
####基于 name.localhost 模板,创建配置文件:
cp -p /var/named/named.localhost /var/named/named.192.168.56####配置反向域名解析文件 named.192.168.56 : vi /var/named/named.192.168.56cat >> /var/named/named.192.168.56 << EOF
102 PTR web.reading.zt
103 PTR web.reading.zt
101 PTR test.reading.zt
EOF####授权 named 用户
chown :named /var/named/named.192.168.56####检查区域文件是否正确
named-checkzone "56.168.192.in-addr.arpa" "/var/named/named.192.168.56"[root@master1 ~]# named-checkzone "56.168.192.in-addr.arpa" "/var/named/named.192.168.56"
zone 56.168.192.in-addr.arpa/IN: loaded serial 0
OK####重启 named 服务,让配置生效
systemctl restart named####在 Linux 下的 DNS 客户端的设置及测试
####注册域名解析服务器到配置文件
配置 ifcfg-xxxx vi /etc/sysconfig/network-scripts/ifcfg-enp0s3 , 增加一行
DNS2=192.168.56.101 // 新增,本机就是域名解析服务器####重启网络服务,让配置生效
systemctl restart network.service####使用 nslookup 测试
####正向域名解析测试
nslookup test.reading.zt结果---->>>
[root@node2 ~]# nslookup test.reading.zt
Server: 192.168.56.101
Address: 192.168.56.101#53Name: test.reading.zt
Address: 192.168.56.101nslookup web.reading.zt结果---->>>
[root@node2 ~]# nslookup web.reading.zt
Server: 192.168.56.101
Address: 192.168.56.101#53Name: web.reading.zt
Address: 192.168.56.102
Name: web.reading.zt
Address: 192.168.56.103####反响域名解析测试
nslookup 192.168.56.101[root@node2 ~]# nslookup 192.168.56.101
101.56.168.192.in-addr.arpa name = test.reading.zt.56.168.192.in-addr.arpa.nslookup 192.168.56.102[root@node2 ~]# nslookup 192.168.56.102
102.56.168.192.in-addr.arpa name = web.reading.zt.56.168.192.in-addr.arpa.nslookup 192.168.56.103