由于工作原因,需要给客户搭建权威DNS服务器,在此记录一下。
DNS(Domain Name Service) 域名解析服务,就是将域名和 ip 之间做相应的转换,利用 TCP 和 UDP 的53端口。
客户机访问域名,先查看自己主机的DNS缓存(有时间限制),如果主机DNS缓存有,则直接访问对应ip。
如果主机DNS缓存没有,则查看本地 hosts文件。
如果 hosts 文件没有,则将该请求发送给主机指定的域名服务器。域名服务器收到请求后,先查询本地的缓存,如果有该纪录项,则域名服务器就直接把查询的结果返回。如果指定的域名服务器的缓存中没有该记录,则进行以下迭代查询。
本地域名服务器向根域名服务器查询,根域名服务器告诉它下一步到哪里去查询,然后它在根据结果逐层向下查询,直到得到最终结果。每次它都是以DNS客户机的身份去各个服务器查询,即迭代查询是本地服务器进行的操作。
具体流程描述:
所以,优先级: 本地DNS缓存 > hosts文件 > DNS服务器
windows中hosts文件存放路径: C:\Windows\System32\drivers\etc\hosts
Linux中hosts文件存放路径: /etc/hosts
类型 | 说明 |
---|---|
A | 主机记录,记录域名对应的ip |
PTR | 反向地址解析记录,记录ip对应的域名 |
CNAME | 别名记录 |
MX | 邮箱交换记录 |
NS | 服务器记录 |
SOA | 权威记录 |
TXT | 为记录说明 |
SRV | 列出了正在提供特定服务的服务器 |
AAAA | ipv6地址记录 |
我这里使用的是CentOS-7-x86_64-Minimal-2003
在官网下载好安装包后直接安装即可,不做过多赘述。
$ setenforce 0
$ sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
$ systemctl stop firewalld.service && systemctl disable firewalld.service
$ firewall-cmd --state
$ yum install -y epel-release yum-plugin-priorities
$ yum install -y mariadb-server mariadb
$ systemctl enable mariadb.service
$ systemctl start mariadb.service
$ mysql_secure_installation
#回车
y #设置root密码
#root密码,
#重复root密码,
y #删除匿名登入
n #禁用root远程登入
y #删除test库
y #刷新权限
$ vim /etc/my.cnf
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
vim /etc/my.cnf.d/client.cnf
[client]
default-character-set=utf8
vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
default-character-set=utf8
$ systemctl restart mariadb.service
再次登录 MariaDB,查看字符集,发现已是 utf8 了。
$ mysql -uroot -p
MariaDB[(none)]> show variables like "%character%";show variables like "%collation%";
MariaDB[(none)]> exit
$ yum install -y pdns pdns-backend-mysql
PowerDNS 的配置文件位于 /etc/pdns/pdns.conf
$ mysql -uroot -p
MariaDB[(none)]> CREATE DATABASE powerdns;
MariaDB[(none)]> GRANT ALL ON powerdns.* TO 'powerdns'@'localhost' IDENTIFIED BY 'powerdns';
MariaDB[(none)]> FLUSH PRIVILEGES;
MariaDB[(none)]> use powerdns;
CREATE TABLE domains (
id INT AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
master VARCHAR(128) DEFAULT NULL,
last_check INT DEFAULT NULL,
type VARCHAR(6) NOT NULL,
notified_serial INT DEFAULT NULL,
account VARCHAR(40) DEFAULT NULL,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE UNIQUE INDEX name_index ON domains(name);
CREATE TABLE records (
id BIGINT AUTO_INCREMENT,
domain_id INT DEFAULT NULL,
name VARCHAR(255) DEFAULT NULL,
type VARCHAR(10) DEFAULT NULL,
content VARCHAR(64000) DEFAULT NULL,
ttl INT DEFAULT NULL,
prio INT DEFAULT NULL,
change_date INT DEFAULT NULL,
disabled TINYINT(1) DEFAULT 0,
ordername VARCHAR(255) BINARY DEFAULT NULL,
auth TINYINT(1) DEFAULT 1,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE INDEX nametype_index ON records(name,type);
CREATE INDEX domain_id ON records(domain_id);
CREATE INDEX recordorder ON records (domain_id, ordername);
CREATE TABLE supermasters (
ip VARCHAR(64) NOT NULL,
nameserver VARCHAR(255) NOT NULL,
account VARCHAR(40) NOT NULL,
PRIMARY KEY (ip, nameserver)
) Engine=InnoDB;
CREATE TABLE comments (
id INT AUTO_INCREMENT,
domain_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
type VARCHAR(10) NOT NULL,
modified_at INT NOT NULL,
account VARCHAR(40) NOT NULL,
comment VARCHAR(64000) NOT NULL,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE INDEX comments_domain_id_idx ON comments (domain_id);
CREATE INDEX comments_name_type_idx ON comments (name, type);
CREATE INDEX comments_order_idx ON comments (domain_id, modified_at);
CREATE TABLE domainmetadata (
id INT AUTO_INCREMENT,
domain_id INT NOT NULL,
kind VARCHAR(32),
content TEXT,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE INDEX domainmetadata_idx ON domainmetadata (domain_id, kind);
CREATE TABLE cryptokeys (
id INT AUTO_INCREMENT,
domain_id INT NOT NULL,
flags INT NOT NULL,
active BOOL,
content TEXT,
PRIMARY KEY(id)
) Engine=InnoDB;
CREATE INDEX domainidindex ON cryptokeys(domain_id);
CREATE TABLE tsigkeys (
id INT AUTO_INCREMENT,
name VARCHAR(255),
algorithm VARCHAR(50),
secret VARCHAR(255),
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE UNIQUE INDEX namealgoindex ON tsigkeys(name, algorithm);
MariaDB[powerdns]> flush privileges;
MariaDB[powerdns]> show databases;
MariaDB[powerdns]> show tables;
MariaDB[powerdns]> exit
$ cp /etc/pdns/pdns.conf /etc/pdns/pdns.conf.bak
$ vim /etc/pdns/pdns.conf
# backend
launch=gmysql
gmysql-host=localhost
gmysql-port=3306
gmysql-dbname=powerdns
gmysql-user=powerdns
gmysql-password=powerdns
# pdns API
webserver=yes
webserver-address=0.0.0.0
webserver-allow-from=0.0.0.0/0
webserver-port=8081
api=yes
api-key=wmqpdns
api-logfile=/var/log/pdns-api.log
$ systemctl enable pdns.service
$ systemctl start pdns.service
$ systemctl status pdns.service
查看8081、53两个端口
$ netstat -tulnp|grep pdns_server
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 9712/pdns_server
tcp 0 0 0.0.0.0:53 0.0.0.0:* LISTEN 9712/pdns_server
tcp6 0 0 :::53 :::* LISTEN 9712/pdns_server
udp 0 0 0.0.0.0:53 0.0.0.0:* 9712/pdns_server
udp6 0 0 :::53 :::* 9712/pdns_server
$ yum install -y epel-release
$ yum install -y https://repo.ius.io/ius-release-el7.rpm
$ yum install -y python36u python36u-devel python36u-pip
$ pip3.6 install -U pip
$ pip install -U virtualenv
$ rm -f /usr/bin/python3 && ln -s /usr/bin/python3.6 /usr/bin/python3
$ yum install -y gcc mariadb-devel openldap-devel xmlsec1-devel xmlsec1-openssl libtool-ltdl-devel
$ curl -sL https://rpm.nodesource.com/setup_10.x | bash -
$ curl -sL https://dl.yarnpkg.com/rpm/yarn.repo -o /etc/yum.repos.d/yarn.repo
$ yum install -y yarn
$ yum install -y git
$ git clone https://github.com/ngoduykhanh/PowerDNS-Admin.git /opt/web/powerdns-admin
$ cd /opt/web/powerdns-admin
$ virtualenv -p python3 flask
激活 python3 环境并安装python库(后续操作都是基于python3 环境下操作)
$ source ./flask/bin/activate
(flask)$ pip install python-dotenv
(flask)$ pip install -r requirements.txt
下载的包临时存放在 /root/.cache/pip/wheels 目录下。
(flask)$ mysql -u root -p
MariaDB[(none)]> CREATE DATABASE powerdnsadmin CHARACTER SET utf8 COLLATE utf8_general_ci;
MariaDB[(none)]> GRANT ALL PRIVILEGES ON powerdnsadmin.* TO 'pdnsadminuser'@'%' IDENTIFIED BY 'p4ssw0rd';
MariaDB[(none)]> FLUSH PRIVILEGES;
MariaDB[(none)]> exit
(flask)$ vim ./powerdnsadmin/default_config.py
#地址改成0.0.0.0
BIND_ADDRESS = '0.0.0.0'
# 配置数据库连接信息,库/用户/密码是之前手动创建的,不是pdns数据库
SQLA_DB_USER = 'pdnsadminuser'
SQLA_DB_PASSWORD = 'p4ssw0rd'
SQLA_DB_HOST = 'localhost'
SQLA_DB_NAME = 'powerdnsadmin'
# 开启MySQL
# DATABASE - MySQL
SQLALCHEMY_DATABASE_URI = 'mysql://'+SQLA_DB_USER+':'+SQLA_DB_PASSWORD+'@'+SQLA_DB_HOST+':'+str(SQLA_DB_PORT)+'/'+SQLA_DB_NAME
# 注释sqlite
# DATABASE - SQLite
# SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'pdns.db')
(flask)$ export FLASK_APP=./powerdnsadmin/__init__.py
(flask)$ flask db upgrade
创建资产文件
(flask)$ yarn install --pure-lockfile
(flask)$ flask assets build
(flask)$ ./run.py
访问PowerDNS-Admin Web界面:http://IP:9191
1、先注册用户,第一个用户将处于管理员角色。
2、第一次登录时,将被重定向到设置页面以配置PDNS API信息。
PDNS API URL:http://IP:8081
PDNS API KEY:wmqpdns
使用systemd管理PowerDNS-Admin
$ vim /root/pdns.sh
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
source /opt/web/powerdns-admin/flask/bin/activate; nohup /opt/web/powerdns-admin/run.py &
$ vim /usr/lib/systemd/system/powerdns-admin.service
[Unit]
Description=Pdns-admin Service
After=mariadb.service
[Service]
Type=forking
ExecStart=/root/pdns.sh
KillMode=process
[Install]
WantedBy=multi-user.target
启动Powerdns-Admin服务并将其设置为在启动时启动:
$ systemctl daemon-reload
$ systemctl start powerdns-admin.service
$ systemctl enable powerdns-admin.service
可以运行systemctl status powerdns-admin.service命令确认状态是否正在运行,没问题的话会返回相关的成功信息。
$ systemctl status powerdns-admin.service