搭建dnsmasq服務器,局域網內部解析


系統:centos6.5

 

公司內部需求一台dns server,解析內部域名(該域名不需要在公網上解析)

 

安裝了"bind bind-utils"包,配置里設置轉發到外部電信dns,dhcp服務設置分發dns服務為該服務器。然后解析內部很通暢,但是到外部(如百度,163,新浪)等網站很慢。

 

不知道是不是配置有問題,找不出。 

所以就使用dnsmasq試試。

下面是安裝操作步驟 

===================

1,yum 安裝

1
yum  install  dnsmasq -y

 

或者源碼安裝

1
2
3
cd  /tmp  && wget http: //www .thekelleys.org.uk /dnsmasq/dnsmasq-2 .71. tar .gz
tar  -zxvf dnsmasq-2.71. tar .gz &&  cd  dnsmasq-2.71
make  install
1
2
cp  dnsmasq.conf.example  /etc/dnsmasq .conf
mkdir  -p  /etc/dnsmasq .d  #這個目錄備用

2,dnsmasq配置

1
2
3
4
5
6
#主要有三個文件:
#/etc/dnsmasq.conf
#/etc/dnsmasq.d/resolv.dnsmasq.conf
#/etc/dnsmasq.d/dnsmasq.hosts 
 
#第一個是系統默認必須的,后面兩個可以自行建立,放置的路徑也可以根據自己需要定義。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vi  /etc/dnsmasq .conf
#ITGeeker每次開啟都提示錯誤,目的是讓dnsmasq讀取目錄內所有配置文件
#conf-dir=/etc/dnsmasq.d
#讓dnsmasq讀取你設定的resolv-file
#no-resolv
resolv- file = /etc/dnsmasq .d /resolv .dnsmasq.conf
no-poll
strict-order
#不讀取系統hosts,讀取你設定的
no-hosts
addn-hosts= /etc/dnsmasq .d /dnsmasq .hosts
#dnsmasq日志設置
log-queries
log-facility= /var/log/dnsmasq .log
#dnsmasq緩存設置
cache-size=1024
#單設置127只為本機使用,加入本機IP為內部全網使用
listen-address=127.0.0.1,10.19.21.249

#在/etc/dnsmasq.d目錄下新建2個文件

1
2
3
4
5
6
7
8
9
vi  /etc/dnsmasq .d /resolv .dnsmasq.conf
#nameserver 127.0.0.1 不應該添加
nameserver 202.96.209.5
nameserver 202.96.209.133
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 114.114.114.114
nameserver 8.8.4.4
#nameserver 8.8.8.8
1
2
vi  /etc/dnsmasq .d /dnsmasq .hosts
10.19.21.249 aop.baim.com

3,啟動服務

1
2
3
4
5
6
7
/etc/init .d /dnsmasq  start
chkconfig dnsmasq on
#如果是源碼編譯安裝的
啟動:  /usr/local/sbin/dnsmasq
驗證: netstat  -tunlp| grep  53
關閉:killall -KILL dnsmasq
重啟: pkill -9 dnsmasp &&  /usr/local/sbin/dnsmasq  -h
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
#服務啟動腳本
vi  /etc/init .d /dnsmasq
#!/bin/sh
#
# Startup script for the <span class='wp_keywordlink_affiliate'><a href="http://itgeeker.net/tag/dns/" title="View all posts in DNS" target="_blank">DNS</a></span> caching server
#
# chkconfig: - 49 50
# description: This script starts your DNS caching server
# processname: dnsmasq
# pidfile: /var/run/dnsmasq
# Source function library.
/etc/rc .d /init .d /functions
# Source networking configuration.
/etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} =  "no"  ] &&  exit  0
dnsmasq= /usr/local/sbin/dnsmasq
[ -f $dnsmasq ] ||  exit  0
RETVAL=0
# See how we were called.
case  "$1"  in
start)
if  [ $UID - ne  0 ] ;  then
echo  "User has insufficient privilege."
exit  4
fi
echo  -n  "Starting dnsmasq: "
daemon $dnsmasq $OPTIONS
RETVAL=$?
echo
[ $RETVAL - eq  0 ] &&  touch  /var/lock/subsys/dnsmasq
;;
stop)
if  test  "x`pidof dnsmasq`"  != x;  then
echo  -n  "Shutting down dnsmasq: "
killproc dnsmasq
fi
RETVAL=$?
echo
[ $RETVAL - eq  0 ] &&  rm  -f  /var/lock/subsys/dnsmasq  /var/run/dnsmasq .pid
;;
status)
status dnsmasq
RETVAL=$?
;;
reload)
echo  -n  "Reloading dnsmasq: "
killproc dnsmasq -HUP
RETVAL=$?
echo
;;
force-reload)
# new configuration takes effect only after restart
$0 stop
$0 start
RETVAL=$?
;;
restart)
$0 stop
$0 start
RETVAL=$?
;;
condrestart)
if  test  "x`/sbin/pidof dnsmasq`"  != x;  then
$0 stop
$0 start
RETVAL=$?
fi
;;
*)
echo  "Usage: $0 {start|stop|restart|reload|condrestart|status}"
exit  2
esac
exit  $RETVAL
1
2
3
4
5
6
#如果你是本地編輯上傳的,提示找不到文件記得
set  ff=unix
#再賦予執行的權限
chmod  +x  /etc/init .d /dnsmasq
/etc/init .d /dnsmasq  start
chkconfig dnsmasq on

 

注:完成后需要在DHCP服務端設置DNS服務器地址,然后客戶端會自動獲取到該DNS地址。需要重客戶端本地網絡。

 

wKiom1WciESDZGcPAAHuug7ROqU306.jpg

查看本地dns,是否能夠獲取地址。

 

 

然后訪問域名,是否能夠解析。

wKiom1WciVDg-jI_AAPCHKmpBqw177.jpg

 

 

本文出自 “charlie_cen” 博客,請務必保留此出處http://charlie928.blog.51cto.com/3741218/1671914

 

參考文章:http://charlie928.blog.51cto.com/3741218/1671914


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM