常碰到一些朋友抱怨說 linux 的 dns 很難設。
但經我個人觀察,100% 都是打錯字或格式疏忽而已~~~~ ^_^
這裡我寫了隻 shell script ,可以幫助建立簡單的正解及反解檔,
然後將剩下的"簡單"的部份,則自己來修改了...
download:
http://www.study-area.org/linux/src/sample_dns.sh.tgz
初版原始碼:
[code:1:212a63ac08]#!/bin/bash
set -u
# purpose: make a sample dns for RedHat like system
# author: netman<netman@study-area.org>
# date: 2003-12-05
# version: v.0.1
#-- CAVEATE --#
# 1) script has been tested only on RedHat 8.x/9.0 platform.
# 2) needs root privilege to run.
# 3) only one forward zone and one reverse zone will be created.
# if no specified zone names are given, script will assume to
# use the current DNS domain(using text.cxm instead if not found) for
# forward zone's name, and the Class-C ipv4 subnet of current IP for
# reverse zone's name.
# 4) the script can NOT determine whether you have the proper delegation,
# you should manually delete ANY non-authorized zone settings.
# 5) only followint Resource Record will be create by this script:
# * SOA (both forward & reverse)
# * NS (both forward & reverse)
# * MX (forward only)
# * A (forward only)
# * CNAME (www & ftp, forward only)
# * PTR (reverse only)
# 6) a backup for named.conf and db directory will be made to /root/backup.
# backup will be performed once during the first running only,
# unless -b options is given.
# 7) absolutely NO WARRANTY while running this script.
#-- CHANGE LOG --#
# 1) 2003-12-05 v0.01 by netman
# * first version.
options=hrstbf
named_conf=/etc/named.conf
db_dir=/var/named
bak_dir=/root/backup
wtty=$(ps | grep $$ | tail -n 1 | awk '{print $2}')
to_file=
backup=
update=
#-- get zone info --#
fwd_zone=$(hostname -d)
fwd_zone=${fwd_zone:-test.cxm}
host_if=eth0
host_ip=$(ifconfig | grep -A1 $host_if | awk '/inet/{print $2}' | sed 's/^.*://')
host_ip=${host_ip:-192.168.1.1}
rev_zone="$(echo ${host_ip%.*} | awk -F. '{print $3"."$2"."$1}').in-addr.arpa"
ns_ptr=${host_ip##*.}
serial_nu=$(date +%Y%m%d)01
opt_ttl=86400
function print_usage {
echo "-------------------------------------------------------------"
echo "Script Name:"
echo -e "\t${0##*/}"
echo "Options:"
echo -e "\t-h\tprint this help"
echo -e "\t-r\trun the script with default values"
echo -e "\t-s\tspecify your own zone names"
echo -e "\t-t\ttest only"
echo -e "\t-b\tforce backup"
echo -e "\t-f\tforce update db(s)"
echo "Example:"
echo -e "\troot_shell# $0 -tu"
echo "-------------------------------------------------------------"
exit 0
}
function print_choice {
echo
echo "Please select one:"
echo "h): to print HELP."
echo "t): to TEST the script only."
echo "q): to QUIT."
echo -n 'Your choice? '
read action
case $action in
h|H) print_usage ;;
t|T) exec $0 -t ;;
q|Q) echo; exit 0 ;;
*) print_choice ;;
esac
}
echo $@ | grep -q "[^$options-]" || [ $# -eq 0 ] && {
echo
echo "${0##*/}: missing argument or invalid options."
print_choice
}
function get_zone {
echo
echo "Which name you would like to assign to the $1 zone? "
echo "(or press Enter for none): "
read z_name
echo $z_name | grep -q ' ' && {
echo "Error: no space allowed in zone name."
echo " Press ctrl-c to abort or type again:"
get_zone $1
}
}
function run_spec {
get_zone forward
fwd_zone=${z_name%.}
get_zone reverse
rev_zone=${z_name%.}
echo
echo "Give the FQDN of your name-server"
echo "(or press Enter for system defaults): "
read ns_host
}
#-- prepare backup dir --#
test -d $bak_dir || {
mkdir $bak_dir || {
echo "${0##*/}: Error: Can't create backup dir: $bak_dir "
exit 1
}
}
#-- test permission --#
for target in $named_conf $db_dir $bak_dir; do
[ -e $target ] || continue
[ -w $target ] || {
echo "${0##*/}: Error: you have no write perssion to $target"
exit 2
}
done
#-- make backup --#
function run_bak {
cp -a $source $bak_dir || {
echo "${0##*/}: Error:can't make backup for $source "
exit 3
}
}
function do_backup {
for source in $named_conf $db_dir; do
[ "$backup" = true ] && op=';' || op='||'
eval test -e $bak_dir/${source##*/} $op run_bak
done
}
#-- create default settings if missing --#
function create_raw {
test -e $named_conf || {
echo "${0##*/}: WARNING: $named_conf seems missing!"
echo -n "Do you want me to create it for you? (y/N): "
read YN
echo $YN | grep -Eq 'Y|y' || return 0
cat > $named_conf <<END
// generated by ${0##*/} on $(date)
options {
directory "$db_dir";
};
zone "." IN {
type hint;
file "named.ca";
};
zone "localhost" IN {
type master;
file "localhost.zone";
allow-update { none; };
};
zone "0.0.127.in-addr.arpa" IN {
type master;
file "named.local";
allow-update { none; };
};
END
test -e $db_dir/named.ca || {
cat > $db_dir/named.ca <<END
; This file holds the information on root name servers needed to
; initialize cache of Internet domain name servers
; (e.g. reference this file in the "cache . <file>"
; configuration file of BIND domain name servers).
;
; This file is made available by InterNIC
; under anonymous FTP as
; file /domain/named.cache
; on server FTP.INTERNIC.NET
;
; last update: Nov 5, 2002
; related version of root zone: 2002110501
;
;
. 3600000 IN NS A.ROOT-SERVERS.NET.
A.ROOT-SERVERS.NET. 3600000 A 198.41.0.4
. 3600000 NS B.ROOT-SERVERS.NET.
B.ROOT-SERVERS.NET. 3600000 A 128.9.0.107
. 3600000 NS C.ROOT-SERVERS.NET.
C.ROOT-SERVERS.NET. 3600000 A 192.33.4.12
. 3600000 NS D.ROOT-SERVERS.NET.
D.ROOT-SERVERS.NET. 3600000 A 128.8.10.90
. 3600000 NS E.ROOT-SERVERS.NET.
E.ROOT-SERVERS.NET. 3600000 A 192.203.230.10
. 3600000 NS F.ROOT-SERVERS.NET.
F.ROOT-SERVERS.NET. 3600000 A 192.5.5.241
. 3600000 NS G.ROOT-SERVERS.NET.
G.ROOT-SERVERS.NET. 3600000 A 192.112.36.4
. 3600000 NS H.ROOT-SERVERS.NET.
H.ROOT-SERVERS.NET. 3600000 A 128.63.2.53
. 3600000 NS I.ROOT-SERVERS.NET.
I.ROOT-SERVERS.NET. 3600000 A 192.36.148.17
. 3600000 NS J.ROOT-SERVERS.NET.
J.ROOT-SERVERS.NET. 3600000 A 192.58.128.30
. 3600000 NS K.ROOT-SERVERS.NET.
K.ROOT-SERVERS.NET. 3600000 A 193.0.14.129
. 3600000 NS L.ROOT-SERVERS.NET.
L.ROOT-SERVERS.NET. 3600000 A 198.32.64.12
. 3600000 NS M.ROOT-SERVERS.NET.
M.ROOT-SERVERS.NET. 3600000 A 202.12.27.33
; End of File
END
}
test -e $db_dir/localhost.zone || {
cat > $db_dir/localhost.zone <<END
\$TTL 86400
\$ORIGIN localhost.
@ 1D IN SOA @ root (
42 ; serial (d. adams)
3H ; refresh
15M ; retry
1W ; expiry
1D ) ; minimum
1D IN NS @
1D IN A 127.0.0.1
END
}
test -e $db_dir/named.local || {
cat > $db_dir/named.local <<END
\$TTL 86400
@ IN SOA localhost. root.localhost. (
1997022700 ; Serial
28800 ; Refresh
14400 ; Retry
3600000 ; Expire
86400 ) ; Minimum
IN NS localhost.
1 IN PTR localhost.
END
}
} # end of first test
} # end of function
#-- check named.conf --#
function check_conf {
for zone in $@; do
war_msg="${0##*/}: WARNING: \n\t$named_conf seems up-to-date. Nothing will be done. "
adv_msg="If you really want to continue, you can:"
[ -e $named_conf ] && grep -Eq "$zone" $named_conf && {
echo -e "$war_msg"
echo "$adv_msg"
echo "1) delete '$zone' sections from $named_conf."
echo " OR:"
echo "2) run '${0##*/} -f' to override db files in $db_dir."
echo -e " Note: this option will not modify $named_conf."
exit 4
}
done
}
#-- modify named.conf --#
function mod_conf {
for zone in $@; do
[ "$to_file" ] && {
echo "----> Content will be written to $named_conf :"
}
cat >> ${to_file:-$named_conf} <<END
zone "$zone" IN {
type master;
file "$zone";
};
END
done
}
#-- modify rr db --#
function mod_rr {
ns_host=${ns_host:-$(hostname -s).$fwd_zone}
ns_host=${ns_host%.}
[ "$fwd_zone" ] && {
[ "$to_file" ] && {
echo "----> Content will be written to $db_dir/$fwd_zone :"
}
cat > ${to_file:-$db_dir/$fwd_zone} <<END
\$TTL $opt_ttl
@ IN SOA $ns_host. root.$ns_host. (
$serial_nu ; Serial
28800 ; Refresh
14400 ; Retry
604800 ; Expire
86400 ) ; Minimum
@ IN NS $ns_host.
${ns_host%%.*} IN A $host_ip
www IN CNAME $ns_host.
ftp IN CNAME $ns_host.
END
}
[ "$rev_zone" ] && {
[ "$to_file" ] && {
echo "----> Content will be written to $db_dir/$rev_zone :"
}
cat > ${to_file:-$db_dir/$rev_zone} <<END
\$TTL $opt_ttl
@ IN SOA $ns_host. root.$ns_host. (
$serial_nu ; Serial
28800 ; Refresh
14400 ; Retry
604800 ; Expire
86400 ) ; Minimum
@ IN NS $ns_host.
$ns_ptr IN PTR $ns_host.
END
} # end of test
} # end of function
#-- main script --#
while getopts ":$options" opt; do
case "$opt" in
h) print_usage ;;
r) : ;;
s) run_spec ;;
t) to_file=/dev/$wtty ;;
b) backup=true ;;
f) update=true ;;
\?) print_usage ;;
esac
done
do_backup
create_raw
test "$update" = true || { check_conf $fwd_zone $rev_zone; mod_conf $fwd_zone $rev_zone; }
mod_rr
test "$to_file" || {
echo "${0##*/}: Okay, all done!"
echo "Don't forget to restart your named daemon and check log messages."
echo "Enjoy!"
}
exit 0[/code:1:212a63ac08]