一、浅谈DNS解析技巧
在搭建服务器之前,我们需要了解一下DNS域名解析原理,《DNS原理及其解析过程》,总体上,《DNS原理及其解析过程》对解析过程说的非常清楚。
在这里,补充和强调以下了三点:
1、DNS查询:分为递归查询和迭代查询两种
(1) 递归查询: 即客户端向本地DNS服务器请求查询域名,本地DNS服务器收到查询任务后如自身无法回答则向其他服务器查询,直到查到结果后返回结果给客户端。期间DNS服务器可能要查询很多其他DNS服务器。
(2)迭代查询:即客户端向本地服务器请求查询域名,本地DNS服务器无法回答,则给客户端返回另一个能查询域名的服务器地址,客户端再向另一服务器查询,期间可能客户端需查询多个DNS服务器,最终查到结果。
2、DNS服务器分类:
DNS服务器分为:
(1)master(主DNS服务器):拥有区域数据的文件,并对整个区域数据进行管理。
(2)slave(从服务器或叫辅助服务器):拥有主DNS服力器的区域文件的副 本,辅助主DNS服务器对客户端进行解析,当主DNS服务器坏了后,可以完全接替主服务器的工作。
(3)forward:将任何查询请求都转发给其他服务器。起到一个代理的作用。
(4)cache:缓存服务器。
(4)hint:根DNS internet服务器集。
3、DNS劫持
DNS 劫持是网络安全界常见的一个名词,意思是通过某些手段取得某一目标域名的解析记录控制权,进而修改此域名的解析结果,通过此修改将对此域名的访问由原先的 IP地址转入到自己指定的IP,从而实现窃取资料或者破坏原有正常服务的目的。
二、安装DNS BIND服务器
关于安装DNS服务器,我们这里使用Windows环境,需要下载Windows版本的Bind服务器
1.下载地址《DNS Bind》,我们下载相对稳定的版本即可,配置基本相同。
2.安装
将下载的BIND9.9.0.ZIP解压,进入到解压后的文件夹,运行 BINDInstall.exe,在弹出的安装窗口中输入一个密码,一直默认安装就行了,不需要更改什么设置。默认安装到 windows\system32\dns目录下。
由于 windows\system32\dns属于系统目录,因此,需要设置各种权限,因此,我们建议安装到非系统盘,比如(E:/DNSBindServer/),本篇博客的DNS安装位置在(E:/DNSBindServer/)下面
以上账号,我们省略,使用系统账号即可。
安装完成之后,我们打开Windows系统服务管理界面
3、环境配置
3.1首先,建立如下目录环境
3.2生成rndc.key文件
然后CMD窗口,进E:/DNSBindServer/bin目录,用rndc-config.exe程序生产rndc.key。执行如下命令,将在E:/DNSBindServer/etc目录下生成rndc.key
rndc-confgen -a
3.3创建named.conf
在E:/DNSBindServer/etc目录下创建named.conf,这个是Bind服务器默认加载的配置。
配置内容如下
include "E:/DNSBindServer/etc/rndc.key";
controls {
inet 127.0.0.1 port 953
allow { 127.0.0.1; } keys { "rndc-key"; };
};
include "E:/DNSBindServer/etc/named.conf.options";
include "E:/DNSBindServer/etc/named.conf.zones";
第一段引入rndc.key
第二段是控制点,用来远程控制,注意,这楼里的 port:953是控制点端口,不是DNS服务端口,DNS服务端口是53。
第三段是常规配置
在E:/DNSBindServer/etc/建立named.conf.options文件,在里面加入如下内容
logging {
channel warning
{
file "E:/DNSBindServer/log/warning.log" versions 3 size 2048k;
severity warning;
print-category yes;
print-severity yes;
print-time yes;
};
channel query
{
file "E:/DNSBindServer/log/query.log" versions 3 size 2048k;
severity info;
print-category yes;
print-severity yes;
print-time yes;
};
channel default_syslog {
file "E:/DNSBindServer/log/error.log";
severity error; print-category yes;
print-severity yes;
print-time yes;
};
channel audit_log { file "E:/DNSBindServer/log/zone_named.log"; severity error; print-time yes; };
category default { warning;};
category general { default_syslog; };
category security { audit_log; default_syslog; };
category config { default_syslog; };
category resolver { audit_log; };
category xfer-in { audit_log; };
category xfer-out { audit_log; };
category notify { audit_log; };
category client { audit_log; };
category network { audit_log; };
category update { audit_log; };
category queries { query; };
category lame-servers { audit_log; };
};
acl "trust-lan" { 127.0.0.0/8; 192.168.0.0/16; 10.1.0.0/16; };
options {
#域名文件存放的绝对路径
directory "E:/DNSBindServer/etc/";
#如果bind启动,自动会在{pid-file}目录生成一个named.pid文件,打开文件就是named进程的ID
pid-file "E:/DNSBindServer/log/dns_named.pid";
version "1.0.0";
allow-transfer { none; }; #允许trust-lan里的IP从主DNS上进行区域传输
allow-notify { "trust-lan"; }; #从服务器接收主服务器的更新通知
allow-query { "trust-lan"; }; #允许查询,只有trust-lan中的主机发来的DNS请求才会被处理
allow-recursion{ none;}; #打开BIND递归查询功能
notify yes;
forwarders {
61.234.254.6;
61.234.254.5;
59.51.78.211;
8.8.8.8;
};
auth-nxdomain no; # conform to RFC1035
#listen-on port 53 { 127.0.0.1;192.168.1.210;10.1.235.92; };
listen-on-v6 { any; };
};
logging模块是日志通道配置,这对于配置调试非常重要,因为BIND并不自动产生日志,因此,无比在配置阶段添加日志配置,防止配置失败问题产生。特别是如下通道。
category default { warning;};
接下来是
acl "trust-lan" { 127.0.0.0/8; 192.168.0.0/16; 10.1.0.0/16; };
acl(Access Control List)是定义一个常量的声明,用来限制哪些主机的请求被允许响应。
下来是非常重要的options配置
options {
#域名文件存放的绝对路径
directory "E:/DNSBindServer/etc/";
#如果bind启动,自动会在{pid-file}目录生成一个named.pid文件,打开文件就是named进程的ID
pid-file "E:/DNSBindServer/log/dns_named.pid";
version "1.0.0";
allow-transfer { none; }; #允许trust-lan里的IP从主DNS上进行区域传输
allow-notify { "trust-lan"; }; #从服务器接收主服务器的更新通知
allow-query { "trust-lan"; }; #允许普通查询
allow-recursion{ none;}; #打开BIND递归查询功能
notify yes;
forwarders { #如果本地域名无法解析到ip,那么自动前往如下地址
61.234.254.6;
61.234.254.5;
59.51.78.211;
8.8.8.8;
};
auth-nxdomain no; # conform to RFC1035
listen-on-v6 { any; };
};
*auth-nxdomain 是否做为权威服务器回答域不存在(Auth-nxdomain)
如果设置为'yes',则允许服务器以权威性(authoritatively)的方式返回NXDOMAIN(该域不存在)的回答,否则就不会作权威性的回答,缺省值为”是”,这里设置为no,让本地服务器到DNS分布式系统去查询。
最后创建named.conf.zones文件,输入如下内容
zone "." IN {
type hint; #根域名服务器
file "E:/DNSBindServer/etc/named.db.root";
};
zone "localhost" IN {
type master; #该域名服务器是主域名服务器,这个选项主要用在主备部署中
file "E:/DNSBindServer/etc/localhost.zone";
allow-update { none; };
};
zone "0.0.127.in-addr.arpa" IN { #这种属于反向解析
type master;
file "E:/DNSBindServer/etc/127.0.0.zone";
allow-update { none; }; #定义了允许向主zone文件发送动态更新的匹配列表
};
zone "xushjie.com" IN {
type master;
file "E:/DNSBindServer/etc/xushjie.com.zone";
allow-update { none; };
};
zone "0.168.192.in-addr.arpa" IN {
type master;
file "E:/DNSBindServer/etc/192.168.0.zone";
allow-update { none; };
};
zone "baidu.com" IN{
type master;
file "E:/DNSBindServer/etc/baidu.com.zone";
allow-update { none; };
};
我们看到,第一项是name.db.root,
下载地址时《ftp://ftp.internic.net/domain/named.root》
解析文件配置
反向解析
127.0.0.zone
$TTL 86400
@ IN SOA localhost. root.localhost. (
2005030122
28800
14400
3600000
86400 )
@ IN NS localhost.
1 IN PTR localhost. ;反解析,注意,1表示主机号,者此处是 127.0.0.1
2 IN PTR localhost. ;反解析,注意,2表示主机号,者此处是 127.0.0.2
192.168.0.zone
$TTL 86400
@ IN SOA ns.xushjie.com. root.xushjie.com. ( ;ns.xushjie.com.表示域名组, root.xushjie.com.表示邮箱root@xueshjie.com
2005030119
7200
3600
43200
86400 )
@ IN NS ns.xushjie.com. ;反向解析第一句表示类型(必须)
1 IN PTR www.xushjie.com. ;表示192.168.0.1
100 IN PTR www.xushjie.com. ;表示192.168.0.100
正向解析
localhost.zone
$TTL 86400
localhost. IN SOA localhost. root.localhost. (
2001062501 ;serial
21600 ;refresh
3600 ;retry
604800 ;expire
86400 ;mininum
)
@ IN NS localhost.
@ IN A 127.0.0.1
@ IN AAAA ::1
xushijie.com.zone
$TTL 86400
@ IN SOA ns.xushjie.com. root.xushjie.com. (
1053891168
21600
3600
604800
86400 )
@ IN NS ns.xushjie.com.
ns IN A 192.168.0.100
www IN A 192.168.0.100
其中一句如下:用来表示域名所对应的地址组(ns.xushjie.com),以及管理员邮箱(root.xushjie.com.,本应该是root@xushjie.com,但是@具有特殊意义)
@ IN SOA ns.xushjie.com. root.xushjie.com.
再来看下个例子
baidu.com.zone
$TTL 86400
@ IN SOA ns1.baidu.com. root.baidu.com.(
2001072501 ;serial
21600 ;refresh
3600 ;retry
604800 ;expire
86400 ;mininum
)
@ IN NS ns1.baidu.com. ;(定义ns1)
ns1 IN A 220.181.112.244 ;(让ns1指向对应的ip,否则以下地址无法解析)
www IN A 220.181.112.244
image IN A 180.149.131.70
关于配置,请参考如下博客
《DNS named.conf配置&DNS主从配置》
《DNS服务器bind的master 和 slave 搭建》
《DNS服务器之一:DNS简介及BIND安装与基本配置 系列文章》
《DNS扫盲系列之五:域名配置ZONE文件 系列文章》
4.接下来就是DNS 测试
首先要启动BindServer
然后执行如下命令
目前为止,我们的DNS服务器已经安装完成。
5.私有解析服务器设置
但是,这DNS服务器是独立运行的,如果我们直接执行如下命令
dig www.baidu.com
那么不会通过BINDServer解析,因此,为了进一步使用BINDServer,我们需要设置主机DNS服务地址