在RHEL6中用BASH脚本搭建DNS域名解析服务器

#!/bin/bash
# Just need to reset variable "MYIP" and "MYDOMAIN" to yourself.

MYIP="192.168.80.6"
MYDOMAIN="example.com"
export MYIP MYDOMAIN
#--------------------------------------
# Don't change anything in the below, 
# unless you know what you are doing.


#--------------------------------------
# Install Packages.
#--------------------------------------
yum -y install bind bind-chroot


#--------------------------------------
# Modify "/etc/named.conf" 
#--------------------------------------
sed -i 's/127.0.0.1;/any;/g' /etc/named.conf
sed -i 's/localhost;/any;/g' /etc/named.conf


#--------------------------------------
# Modify "/etc/named.rfc1912.zones"
#--------------------------------------
grep "$MYDOMAIN" /etc/named.rfc1912.zones > /dev/null
if [ $? -ne 0 ]; then
cat >> /etc/named.rfc1912.zones << _EOF_
zone "$MYDOMAIN" IN {
        type master;
        file "$MYDOMAIN.zone";
        allow-update { none; };
};
zone "$(echo $MYIP | awk -F"." '{print $3"."$2"."$1}').in-addr.arpa" IN {
        type master;
        file "$(echo $MYIP | awk -F"." '{print $1"."$2"."$3}').zone";
        allow-update { none; };
};
_EOF_
fi


#--------------------------------------
# Create forward and backward resolve file
#--------------------------------------
/bin/cp -p /var/named/named.localhost /var/named/"$MYDOMAIN".zone
/bin/cp -p /var/named/named.localhost /var/named/"$(echo $MYIP | awk -F"." '{print $1"."$2"."$3}')".zone

cat > /var/named/"$MYDOMAIN".zone << _EOF_
\$TTL 1D
@   IN SOA  $MYDOMAIN. rname.invalid. (
                                        0   ; serial
                                        1D  ; refresh
                                        1H  ; retry
                                        1W  ; expire
                                        3H )    ; minimum
                NS      $MYDOMAIN.
$MYDOMAIN.      A       $MYIP
www             A       $MYIP
ftp             A       $MYIP
$MYDOMAIN.      MX 5    mail.$MYDOMAIN.
mail            A       $MYIP
_EOF_

cat > /var/named/"$(echo $MYIP | awk -F"." '{print $1"."$2"."$3}')".zone << _EOF_
\$TTL 1D
@ IN SOA  $MYDOMAIN. rname.invalid. (
                                        0   ; serial
                                        1D  ; refresh
                                        1H  ; retry
                                        1W  ; expire
                                        3H )    ; minimum
        NS      $MYDOMAIN.
$(echo $MYIP | awk -F"." '{print $4}')  PTR     $MYDOMAIN.
$(echo $MYIP | awk -F"." '{print $4}')  PTR     www.$MYDOMAIN.
$(echo $MYIP | awk -F"." '{print $4}')  PTR     ftp.$MYDOMAIN.
$(echo $MYIP | awk -F"." '{print $4}')  PTR     mail.$MYDOMAIN.
_EOF_


#--------------------------------------
# Modify "/etc/resolv.conf"
#--------------------------------------
cat > /etc/resolv.conf << _EOF_
search $MYDOMAIN
nameserver $MYIP
_EOF_


#--------------------------------------
# Restart DNS service
#--------------------------------------
/etc/init.d/named restart

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值