自動Yum安裝DNS服務器
阿新 • • 發佈:2018-06-13
DNS Shell 腳本 yum bind #!/bin/bash
####################################################################
# Auto install bind
# Create Date : 2018-05-31
# Written by :風花
# Organization: hbgslz.com
####################################################################
DOMAIN_NAME=`hostname| awk -F. '{print $2"."$3}'`
HOSTNAME=`hostname|awk -F. '{print $1}'`
IN_Face=`route -n |awk '{if($4~/UG/){print $8}}'|head -n 1`
Local_IP=`nmcli device show "$IN_Face" | grep IP4.ADDRESS | awk '{print $2}' | awk -F/ '{print $1}'`
IP_Arp_01=`echo $Local_IP | awk -F. '{print $3}'`
IP_Arp_02=`echo $Local_IP | awk -F. '{print $2}'`
IP_Arp_03=`echo $Local_IP | awk -F. '{print $1}'`
IP_Arp_04=`echo $Local_IP | awk -F. '{print $4}'`
cd /tmp/
yum -y install bind-utils bind >>/tmp/init_sn.log -y || exit 1
# ***config /etc/named.conf***
cat << named_conf > /etc/named.conf
options {
//listen-on port 53 { 192.168.100.27; }; #指定監聽IP和端口,可以指定多個IP
//listen-on-v6 port 53 { none; };
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";
allow-query { any; }; #允許查詢的客戶端列表
//allow-recursion { localhost;192.168.100.0/24;192.168.200.0/24; }; #運行通過本服務器遞歸查詢的客戶端列表
//forward first; #轉發模式,first選項代表首先查詢forwarders中的DNS服務器,如果查詢失敗,則從根服務器開始遞歸查詢(需要定義zone "."區域),only選項代表只查詢forwarders中的服務>器,如果查詢失敗也不會繼續從根服務器進行遞歸查詢
forwarders { 114.114.114.114;8.8.8.8; };
/*
- 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 will
cause your server to become part of large scale DNS amplification
attacks. Implementing BCP38 within your network would greatly
reduce such attack surface
*/
recursion yes; #允許遞歸查詢,如果刪除該行,即為叠代查詢
dnssec-enable yes; #DNSSEC相關選項,國內的DNS服務器基本沒有配置DNSSEC,因此關閉,保持默認開啟也沒影響
dnssec-validation yes;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
managed-keys-directory "/var/named/dynamic";
pid-file "/run/named/named.pid";
session-keyfile "/run/named/session.key";
};
logging {
channel default_debug {
file "/var/log/named/named.log" versions 55 size 10m;
severity dynamic;
print-time yes;
print-severity yes;
print-category yes;
};
category queries { default_debug; };
};
zone "." IN {
type hint;
file "named.ca";
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
named_conf
# ***config /etc/named.rfc1912.zones***
cat << named_rfc1912_zones > /etc/named.rfc1912.zones
zone "localhost.localdomain" IN {
type master;
file "named.localhost";
allow-update { none; };
};
zone "localhost" IN {
type master;
file "named.localhost";
allow-update { none; };
};
zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" IN {
type master;
file "named.loopback";
allow-update { none; };
};
zone "1.0.0.127.in-addr.arpa" IN {
type master;
file "named.loopback";
allow-update { none; };
};
zone "0.in-addr.arpa" IN {
type master;
file "named.empty";
allow-update { none; };
};
//正向解析
zone "$DOMAIN_NAME" IN {
type master; #主服務器
file "$DOMAIN_NAME.zone"; #區域解析文件,位於CHROOT-PATH/var/named/
notify yes; #定時通知從服務器刷新區域信息,時間間隔為區域解析文件中的refresh值
};
//反向解析
zone "$IP_Arp_01.$IP_Arp_02.$IP_Arp_03.in-addr.arpa" IN {
type master;
file "$IP_Arp_01.$IP_Arp_02.$IP_Arp_03.in-addr.arpa.zone";
notify yes;
};
named_rfc1912_zones
# ***config /var/named/hbgsyl.com.zone***
cat << domain_com_zone > /var/named/hbgsyl.com.zone
\$TTL 1D
@ IN SOA @ $DOMAIN_NAME. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A $Local_IP
domain_com_zone
# ***config /var/named/$IP_Arp_01.$IP_Arp_02.$IP_Arp_03.in-addr.arpa.zone***
cat << arpa_zone > /var/named/$IP_Arp_01.$IP_Arp_02.$IP_Arp_03.in-addr.arpa.zone
\$TTL 1D
@ IN SOA @ $DOMAIN_NAME. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A $Local_IP
PTR $HOSTNAME.
$IP_Arp_04 PTR $HOSTNAME.$DOMAIN_NAME.
arpa_zone
if [ ! -d "/var/log/named" ];
then
mkdir /var/log/named
else
break
fi
chown -R named.named /var/log/named
chown -R named.named /var/named
systemctl enable named.service
systemctl start named.service
#check install status.
check_cmd=`nslookup "$DOMAIN_NAME" | echo $?`
if [ "${check_cmd}" == "0" ];
then
echo "<OK!> install bind successful!"
/etc/init.d/network restart
exit 5
else
echo "<ERROR!> Please install bind again!"
fi
自動Yum安裝DNS服務器