最近研究DNS主从服务,也是参考了很多文章,这里记录一下,方便大家少走弯路,DNS服务可以算是Linux服务中比较难的一个了,尤其是配置文件书写,少一个字符都有可能造成错误。
那什么是DNS呢?简单的说就是完成域名到IP的解析过程。简洁的域名能让人们更方便记忆,不需要记那么长的IP访问某一个网站。
DNS解析过程到底是怎样的呢?
第一步:客户机访问某个网站,请求域名解析,首先查找本地HOST文件,如果有对应域名、IP记录,直接返回给客户机。如果没有则将该请求发送给本地的域名服务器:
第二步:本地DNS服务器能够解析客户端发来的请求,服务器直接将答案返回给客户机。
第三步:本地DNS服务器不能解析客户端发来的请求,分为两种解析方法
1、采用递归解析:本地DNS服务器向根域名服务器发出请求,根域名服务器对本地域名服务的请求进行解析,得到记录再给本地DNS服务器,本地DNS服务器将记录缓存,并将记录返给客户机。
2、采用迭代解析:本地DNS服务器向根域名服务器发出请求,根域名服务器返回给本地域名服务器一个能够解析请求的根的下一级域名服务器的地址,本地域名服务器在向根返回的IP地址发出请求,最终得到域名解析记录。
如上只是简单介绍了一下DNS相关知识,言归正传,如下通过脚本自动安装并添加域名解析,脚本可以根据自己的需求修改:(脚本适用于CentOS x86_64 5.8系列)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
#!/bin/sh
#auto install config bind server
#wugk 2013-08-28
#定义变量
BND_ETC
=
/
var
/
named
/
chroot
/
etc
BND_VAR
=
/
var
/
named
/
chroot
/
var
/
named
BAK_DIR
=
/
data
/
backup
/
dns_`date
+
%
Y
%
m
%
d
-
%
H
%
M`
##Backup named server
if
[ !
-
d $BAK_DIR ];then
echo
"Please waiting Backup Named Config ............"
mkdir
-
p $BAK_DIR
cp
-
a
/
var
/
named
/
chroot
/
{etc,var} $BAK_DIR
cp
-
a
/
etc
/
named.
*
$BAK_DIR
fi
##Define Shell Install Function
Install ()
{
if
[ !
-
e
/
etc
/
init.d
/
named ];then
rpm
-
e
-
-
nodeps bind
-
utils
rpm
-
e
-
-
nodeps bind
-
libs
rpm
-
e
-
-
nodeps bind
rpm
-
e bind
-
chroot
rpm
-
e caching
-
nameserver
rpm
-
ivh
-
-
nodeps bind
-
9.3
.
6
-
20.P1
.el5_8.
6.x86_64
.rpm bind
-
chroot
-
9.3
.
6
-
20.P1
.el5_8.
6.x86_64
.rpm bind
-
libs
-
9.3
.
6
-
20.P1
.el5_8.
6.x86_64
.rpm bind
-
utils
-
9.3
.
6
-
20.P1
.el5_8.
6.x86_64
.rpm caching
-
nameserver
-
9.3
.
6
-
20.P1
.el5_8.
6.x86_64
.rpm
else
echo
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
echo
"The Named Server is exists ,Please exit ........."
sleep
1
fi
}
##Define Shell Init Function
Init_Config ()
{
cd $BND_ETC ;ls .
/
*
cp
-
p named.caching
-
nameserver.conf named.conf
sed
-
i
-
e
's/localhost;/any;/g'
-
e
'/port/s/127.0.0.1/any/g'
named.conf
echo
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
sleep
2
echo
"The named.conf config Init success !"
}
##Define Shell Add Name Function
Add_named ()
{
##DNS name
read
-
p
"Please Insert Into Your Add Name ,Example 51cto.com :"
NAME
echo $NAME |grep
-
E
"com|cn|net|org"
while
[
"$?"
-
ne
0
]
do
read
-
p
"Please reInsert Into Your Add Name ,Example 51cto.com :"
NAME
echo $NAME |grep
-
E
"com|cn|net|org"
done
## IP address
read
-
p
"Please Insert Into Your Name Server IP ADDress:"
IP
echo $IP |egrep
-
o
"([0-9]{1,3}\.){3}[0-9]{1,3}"
while
[
"$?"
-
ne
"0"
]
do
read
-
p
"Please reInsert Into Your Name Server IP ADDress:"
IP
echo $IP |egrep
-
o
"([0-9]{1,3}\.){3}[0-9]{1,3}"
done
ARPA_IP
=
`echo $IP|awk
-
F.
'{print $3"."$2"."$1}'
`
ARPA_IP1
=
`echo $IP|awk
-
F.
'{print $4}'
`
cd $BND_ETC
grep
"$NAME"
named.rfc1912.zones
if
[ $?
-
eq
0
];then
echo
"The $NAME IS exist named.rfc1912.zones conf ,please exit ..."
exit
else
read
-
p
"Please Insert Into SLAVE Name Server IP ADDress:"
SLAVE
echo $SLAVE |egrep
-
o
"([0-9]{1,3}\.){3}[0-9]{1,3}"
while
[
"$?"
-
ne
"0"
]
do
read
-
p
"Please Insert Into SLAVE Name Server IP ADDress:"
SLAVE
echo $SLAVE |egrep
-
o
"([0-9]{1,3}\.){3}[0-9]{1,3}"
done
grep
"rev"
named.rfc1912.zones
if
[ $?
-
ne
0
];then
cat >>named.rfc1912.zones <<EOF
#`date +%Y-%m-%d` Add $NAME CONFIG
zone
"$NAME"
IN {
type
master;
file
"$NAME.zone"
;
allow
-
transfer { $SLAVE; };
also
-
notify { $SLAVE; };
allow
-
update { none; };
};
zone
"$ARPA_IP.in-addr.arpa"
IN {
type
master;
file
"$ARPA_IP.rev"
;
allow
-
transfer { $SLAVE; };
also
-
notify { $SLAVE; };
allow
-
update { none; };
};
EOF
else
cat >>named.rfc1912.zones <<EOF
#`date +%Y-%m-%d` Add $NAME CONFIG
zone
"$NAME"
IN {
type
master;
file
"$NAME.zone"
;
allow
-
transfer { $SLAVE; };
also
-
notify { $SLAVE; };
allow
-
update { none; };
};
EOF
fi
fi
[ $?
-
eq
0
]&& echo
"The $NAME config name.rfc1912.zones success !"
sleep
3
;echo
"Please waiting config $NAME zone File ............."
cd $BND_VAR
read
-
p
"Please insert Name DNS A HOST ,EXample www or mail :"
HOST
read
-
p
"Please insert Name DNS A NS IP ADDR ,EXample 192.168.111.130 :"
IP_HOST
echo $IP_HOST |egrep
-
o
"([0-9]{1,3}\.){3}[0-9]{1,3}"
ARPA_IP2
=
`echo $IP_HOST|awk
-
F.
'{print $3"."$2"."$1}'
`
ARPA_IP3
=
`echo $IP_HOST|awk
-
F.
'{print $4}'
`
while
[
"$?"
-
ne
"0"
]
do
read
-
p
"Please Reinsert Name DNS A IPADDRESS ,EXample 192.168.111.130 :"
IP_HOST
echo $IP_HOST |egrep
-
o
"([0-9]{1,3}\.){3}[0-9]{1,3}"
done
cat >$NAME.zone <<EOF
\$TTL
86400
@ IN SOA localhost. root.localhost. (
43
; serial (d. adams)
1H
; refresh
15M
; retry
1W
; expiry
1D
) ; minimum
IN NS $NAME.
EOF
REV
=
`ls
*
.rev`
ls
*
.rev >>
/
dev
/
null
if
[ $?
-
ne
0
];then
cat >>$ARPA_IP.rev <<EOF
\$TTL
86400
@ IN SOA localhost. root.localhost. (
1997022703
; Serial
28800
; Refresh
14400
; Retry
3600000
; Expire
86400
) ; Minimum
IN NS $NAME.
EOF
echo
"$HOST IN A $IP_HOST"
>>$NAME.zone
echo
"$ARPA_IP3 IN PTR $HOST.$NAME."
>>$ARPA_IP.rev
[ $?
-
eq
0
]&& echo
-
e
"The $NAME config success:\n$HOST IN A $IP_HOST\n$ARPA_IP3 IN PTR $HOST.$NAME."
else
sed
-
i
"9a IN NS $NAME."
$REV
echo
"$HOST IN A $IP_HOST"
>>$NAME.zone
echo
"$ARPA_IP3 IN PTR $HOST.$NAME."
>>$REV
[ $?
-
eq
0
]&& echo
-
e
"The $NAME config success1:\n$HOST IN A $IP_HOST\n$ARPA_IP3 IN PTR $HOST.$NAME."
fi
}
##Define Shell List A Function
Add_A_List ()
{
if
cd $BND_VAR
REV
=
`ls
*
.rev`
read
-
p
"Please Insert Into Your Add Name ,Example 51cto.com :"
NAME
[ !
-
e
"$NAME.zone"
];then
echo
"The $NAME.zone File is not exist ,Please ADD $NAME.zone File :"
Add_named ;
else
read
-
p
"Please Enter List Name A NS File ,Example /tmp/name_list.txt: "
FILE
if
[
-
e $
FILE
];then
for
i
in
`cat $
FILE
|awk
'{print $2}'
|sed
"s/$NAME//g"
|sed
's/\.$//g'
`
#for i in `cat $FILE|awk '{print $1}'|sed "s/$NAME//g"|sed 's/\.$//g'`
do
j
=
`awk
-
v I
=
"$i.$NAME"
'{if(I==$2)print $1}'
$
FILE
`
echo
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
echo
"The $NAME.zone File is exist ,Please Enter insert NAME HOST ...."
sleep
1
ARPA_IP
=
`echo $j|awk
-
F.
'{print $3"."$2"."$1}'
`
ARPA_IP2
=
`echo $j|awk
-
F.
'{print $4}'
`
echo
"$i IN A $j"
>>$NAME.zone
echo
"$ARPA_IP2 IN PTR $i.$NAME."
>>$REV
[ $?
-
eq
0
]&& echo
-
e
"The $NAME config success:\n$i IN A $j\n$ARPA_IP2 IN PTR $i.$NAME."
done
else
echo
"The $FILE List File IS Not Exist .......,Please exit ..."
fi
fi
}
##Define Shell Select Menu
PS3
=
"Please select Menu Name Config: "
select i
in
"自动安装Bind服务"
"自动初始化Bind配置"
"添加解析域名"
"批量添加A记录"
do
case $i
in
"自动安装Bind服务"
)
Install
;;
"自动初始化Bind配置"
)
Init_Config
;;
"添加解析域名"
)
Add_named
;;
"批量添加A记录"
)
Add_A_List
;;
*
)
echo
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
sleep
1
echo
"Please exec: sh $0 { Install(1) or Init_Config(2) or Add_named(3) or Add_config_A(4) }"
;;
esac
done
|
本文参考如下文章,非常感谢,欢迎交流、转载!
http://blog.csdn.net/crazw/article/details/8986504
http://www.xiaoxiaozi.com/2013/04/23/2409/
http://1567045.blog.51cto.com/1557045/724332
本文转自 wgkgood 51CTO博客,原文链接:http://blog.51cto.com/wgkgood/1284480