sh DNS服务器 - 用于为家庭/实验室使用生成基本绑定配置的简单脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh DNS服务器 - 用于为家庭/实验室使用生成基本绑定配置的简单脚本相关的知识,希望对你有一定的参考价值。

#!/bin/bash
#
# Simple script to generate a basic bind configuration for home/lab use
#

# Local config - adjust as required
OWNIP=192.168.111.3
NETWORK=192.168.111.0
NETMASK=/24
DNS1=192.168.111.1
DNS2=

# Advanced - should not be changed
DOMAIN=lab.local

# Internal - must not be changed
CONFDIR=/etc/bind

# Let's go - make sure we're in the right path
if [[ ! -d "${CONFDIR}" ]]
  then
        echo "ERROR: configuration path ${CONFDIR} does not exist, exiting"
        exit 1
  else
        echo "Configuration path ${CONFDIR}"
        cd $CONFDIR || exit 1
  fi

# Stop bind
echo "Stopping bind9 daemon..."
service bind9 stop

# Remove the root zone servers, we don't want to query these directly
[[ ! -f db.root.original ]] && mv db.root db.root.original
cat > db.root <<- EOF
\$TTL   30d
@       IN      SOA     localhost. root.localhost. (
                          1     ; Serial
                        30d     ; Refresh
                         1d     ; Retry
                        30d     ; Expire
                        30d     ; Negative Cache TTL
                        )
;
@       IN      NS      localhost.
EOF
echo "Created db.root"

# Set bind options and upstream DNS servers
[[ ! -f named.conf.options.original ]] && mv named.conf.options named.conf.options.original
cat > named.conf.options <<- EOF
options {
        directory "/var/cache/bind";
        auth-nxdomain no;
        listen-on { any; };
        listen-on-v6 { any; };
        allow-recursion { 127.0.0.1; ${NETWORK}${NETMASK}; };
EOF
printf "\tforwarders { ${DNS1}" >> named.conf.options
[[ -n "${DNS2}" ]] && printf "; ${DNS2}" >> named.conf.options
printf "; };\n};\n" >> named.conf.options
echo "Created named.conf.options"

# Configure the local domain
[[ ! -f named.conf.local.original ]] && mv named.conf.local named.conf.local.original
REVADDR=$(for FIELD in 3 2 1; do printf "$(echo ${NETWORK} | cut -d '.' -f $FIELD)."; done)
cat > named.conf.local <<- EOF
zone "${DOMAIN}" {
        type master;
        notify no;
        file "${CONFDIR}/db.${DOMAIN}";
};
zone "${REVADDR}in-addr.arpa" {
        type master;
        notify no;
        file "${CONFDIR}/db.${REVADDR}in-addr.arpa";
};
include "${CONFDIR}/zones.rfc1918";
EOF
echo "Created named.conf.local"

# Populate the forward zone
SERIAL="$(date '+%Y%m%d')01"
NET="$(echo ${NETWORK} | cut -d '.' -f 1-3)"
cat > db.${DOMAIN} <<- EOF
\$ORIGIN ${DOMAIN}.
\$TTL   1d
@       IN      SOA     localhost. root.localhost. (
                        ${SERIAL}       ; Serial
                        1d              ; Refresh
                        2h              ; Retry
                        1w              ; Expire
                        2d              ; Negative Cache TTL
                        )
        IN      NS      dns.${DOMAIN}.
;
dns             IN      A       ${OWNIP}
ntp             IN      CNAME   dns.${DOMAIN}.
esxi01          IN      A       ${NET}.11
esxi02          IN      A       ${NET}.12
esxi03          IN      A       ${NET}.13
esxi04          IN      A       ${NET}.14
;
vcenter         IN      A       ${NET}.20
vma             IN      A       ${NET}.21
EOF
echo "Populated forward zone file db.${DOMAIN} for ${DOMAIN}"

# Populate the reverse zone
OWNH="$(echo ${OWNIP} | cut -d '.' -f 4)"
cat > db.${REVADDR}in-addr.arpa <<- EOF
\$ORIGIN ${REVADDR}in-addr.arpa.
\$TTL   1d
@       IN      SOA     localhost. root.localhost. (
                        ${SERIAL}       ; Serial
                        1d              ; Refresh
                        2h              ; Retry
                        1w              ; Expire
                        2d              ; Negative Cache TTL
                        )
        IN      NS      dns.${DOMAIN}.
;
${OWNH} IN      PTR     dns.${DOMAIN}.
;
11      IN      PTR     esxi01.${DOMAIN}.
12      IN      PTR     esxi02.${DOMAIN}.
13      IN      PTR     esxi03.${DOMAIN}.
14      IN      PTR     esxi04.${DOMAIN}.
;
20      IN      PTR     vcenter.${DOMAIN}.
21      IN      PTR     vma.${DOMAIN}.
EOF
echo "Populated reverse zone file db.${REVADDR}in-addr.arpa for ${NET}"

# Enable local DNS server
[[ ! -f /etc/resolv.conf.original ]] && mv /etc/resolv.conf /etc/resolv.conf.original
cat > /etc/resolv.conf <<- EOF
domain ${DOMAIN}
search ${DOMAIN}
nameserver ${OWNIP}
EOF
echo "Enabled local DNS server in /etc/resolv.conf"

# Start bind
echo "Starting bind9 daemon..."
service bind9 start

# Done
echo "Done."

以上是关于sh DNS服务器 - 用于为家庭/实验室使用生成基本绑定配置的简单脚本的主要内容,如果未能解决你的问题,请参考以下文章

THMDNS in detail(DNS基础详解)-学习

sh 实验!用于为Alce配置envirorment的Shell脚本

配置DNS主从实验

DNS正向解析实验

DNS快速配置实验

dns搭建及实验