Linux下DNS服务器的安装与配置
安装bind
Linux下的dns功能是通过bind软件实现的。在CentOS下面安装bind很方便:
yum install bind
相关的配置文件
1./etc/hosts
定义了主机名和ip地址的对应,其中也有将要运行dns这台电脑的ip地址和主机名。初始内容如下:
- # Do not remove the following line, or various programs
- # that require network functionality will fail.
- 127.0.0.1 TestServer localhost.localdomain localhost
- ::1 localhost6.localdomain6 localhost6
2. /etc/host.conf
当系统中同时存在DNS域名解析和/etc/hosts主机表机制时,由该/etc/host.conf确定主机名解释顺序。示例:
- order hosts,bind #名称解释顺序
- multi on #允许主机拥有多个IP地址
- nospoof on #禁止IP地址欺骗
3. /etc/resolv.conf
该文件是DNS域名解析的配置文件,它的格式很简单,每行以一个关键字开头,后接配置参数。resolv.conf的关键字主要有四个,分别是:
nameserver #定义DNS服务器的IP地址
domain #定义本地域名
search #定义域名的搜索列表
sortlist #对返回的域名进行排序
/etc/resolv.conf的一个示例:
- domain ringkee.com
- search www.ringkee.com ringkee.com
- nameserver 202.96.128.86
- nameserver 202.96.128.166
4. /etc/named.conf
这个文件是bind的主配置文件。这个文件比较复杂,我们先看一个例子,然后再逐段解释。
需要注意的是对于在CentOS里面自带的bind因为启动脚本更改了根目录的原因,在这里以及后面提到的所有文件的存放路径都是相对于/var/named/chroot/目录来说的,比如这里的/etc/named.conf的真实路径为/var/named/chroot/ect/named.conf。
- /*
- * log option
- */
- logging {
- channel default_syslog { syslog local2; severity error; };
- channel audit_log { file "/var/log/named.log"; severity error; print-time yes; };
- category default { default_syslog; };
- 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 { audit_log; };
- category lame-servers { audit_log; };
- };
- options {
- directory "/var/named";
- };
- zone "." {
- type hint;
- file "named.ca";
- };
- zone "localhost" IN {
- type master;
- file "localhost.zone";
- };
- zone "0.0.127.in-addr.arpa" IN {
- type master;
- file "named.local";
- allow-update { none; };
- };
- zone "linuxidc.com" {
- type master;
- file "linuxidc.com.zone";
- };
- zone "1.168.192.in-addr.arpa" {
- type master;
- file "linuxidc.com.rev";
- };
下面部分将逐段进行解释。
4.1 日志设定
- /*
- * log option
- */
- logging {
- channel default_syslog { syslog local2; severity error; };
- channel audit_log { file "/var/log/named.log"; severity error; print-time yes; };
- category default { default_syslog; };
- 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 { audit_log; };
- category lame-servers { audit_log; };
- };
这一部分是日志的设置,其中最主要的是 file "/var/log/named.log" 这一句指定了日志文件的位置,要正常启动named,必须要保证这一文件是存在的,并且named 进程对它有读写权限。
4.2 options
- options {
- directory "/var/named";
- // listen-on-v6 { any; };
- /*
- * If you've got a DNS server around at your upstream provider, enter
- * its IP address here, and enable the line below. This will make you
- * benefit from its cache, thus reduce overall DNS traffic in the Internet.
- */
- //forwarders {
- // your.upper.DNS.address;
- //};
- /*
- * If there is a firewall between you and nameservers you want
- * to talk to, you might need to uncomment the query-source
- * directive below. Previous versions of BIND always asked
- * questions using port 53, but BIND 8.1 uses an unprivileged
- * port by default.
- */
- // query-source address * port 53;
- /*
- * If running in a sandbox, you may have to specify a different
- * location for the dumpfile.
- */
- //dump-file "/etc/named_dump.db";
- };
这一部分是一些基本的配置项:
directory "/etc/named"; 指定域名解析等文件的存放目录(须手动建立);
listen-on-v6 { any; }; 支持ipv6的请求;
forwarders {
your.upper.DNS.address;
}; 指定前向DNS,当本机无法解析的域名,就会被转发至前向DNS进行解析。
dump-file "/etc/named_dump.db"; 指定named_dump.db文件的位置。
4.3线索域和回环域
- zone "." {
- type hint;
- file "named.ca";
- };
- zone "localhost" IN {
- type master;
- file "localhost.zone"; //正向解析文件
- };
- zone "0.0.127.in-addr.arpa" IN {
- type master;
- file "named.local"; //反向解析文件
- allow-update { none; };
- };
指定线索域和本地回环域,这一部分使用一些标准的例子就可以。
在这里type类型有三种,它们分别是master,slave和hint它们的含义分别是:
master:表示定义的是主域名服务器
slave :表示定义的是辅助域名服务器
hint:表示是互联网中根域名服务器
文件“named.ca”; 指定该域的解析文件,其目录为options中directory "/var/named";指定的。在本例中为/var/namd。如果缺少named.ca,可以试试看从这里下载:ftp://ftp.rs.internic.net/domain/named.root
或者是使用如下命令产生一份named.ca:
dig -t NS . > named.ca
[注:如果根节点查询不全则可以使用一下命令]
dig -t NS . @a.root-servers.net > named.ca
文件“localhost.zone”定义了回环域的正向解析文件,其内容可以为:
- $TTL 86400 //全局ttl值.以下记录不指定,则使用全局值
- localhost. 600 IN SOA localhost. admin.localhost. (
- 2011081601 //序列号 版本号
- 1H //H小时 M分钟 D天 W星期 默认为秒
- 10M //重试时间间隔
- 7D //过期时间 7D找不到主服务器 则自杀
- 1D ) //否定回答ttl值
- IN NS localhost.
- localhost. IN A 172.0.0.1
文件“named.local”定义了回环域的反向解析文件,其内容可以为:
- $TTL 86400
- @ 600 IN SOA localhost. admin.localhost. (
- 2011081601
- 1H
- 10M
- 7D
- 1D )
- IN NS localhost.
- 1 IN PTR localhost.
关于正向解析文件与反向解析文件的内容会在后文中进行详细解释。
4.4 自定义域
- zone "linuxidc.com" {
- type master;
- file "linuxidc.com.zone";
- };
- zone "1.168.192.in-addr.arpa" {
- type master;
- file "linuxidc.com.rev";
- };
这一部分是配置文件中我们需要重点关心的部分:
type master;
type master 指明该域主要由本机解析;
zone "0.168.192.in-addr.arpa" {
type master;
}; 指定ipv4地址逆向解析
type master 指明该域主要由本机解析;
至此我们就初步建立了一个标准的named 的主配置文件,接下来建立对应的域名解析或逆向解析文件。