說明
1、dnsmasq是一款小巧且方便地用於配置DNS服務器和DHCP服務器的工具,適用於小型網絡,它提供了DNS解析功能和可選擇的DHCP功能。
2、dnsmasq可以解決小范圍的dns查詢問題,如果業務是跨機房、跨地區的話不建議使用dnsmasq做為dns解析服務器。
安裝
1、下載
wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.75.tar.gz
2、環境准備
yum -y install gcc
3、解壓
tar -xf dnsmasq-2.75.tar.gz
4、編譯安裝
cd dnsmasq-2.75 make install
5、版本查看
dnsmasq -v
6、編輯啟動腳本
#服務啟動腳本
vim /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
賦予執行的權限
chmod +x /etc/init.d/dnsmasq
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
8、其他方式安裝
Centos安裝
yum -y install dnsmasq
Ubuntu 安裝
sudo apt-get -y install dnsmasq
配置
一、三個關鍵配置文件
1、主配置文件,安裝后自動生成
/etc/dnsmasq.conf
2、添加內部需要解析的地址和域名
/etc/dnsmasq.hosts
3、dnsmasq的上游DNS服務器,可以將reslove.conf配置文件復制為resolv.dnsmasq.conf,進行添加nameserver
/etc/resolv.dnsmasq.conf
二、編輯配置文件
1、編輯配置/etc/dnsmasq.conf
vim /etc/dnsmasq.conf
#定義dnsmasq從哪里獲取上游DNS服務器的地址,默認是從/etc/resolv.conf獲取 resolv-file=/etc/resolv.dnsmasq.conf #嚴格按照resolv-file文件中的順序從上到下進行DNS解析,直到第一個解析成功為止 strict-order #啟用泛域名解析,即自定義解析a記錄 #訪問baidu.com時的所有域名都會被解析成10.77.3.8 address=/baidu.com/10.77.3.8 address=/blog.csdn.net/10.77.3.8 #定義dnsmasq監聽的地址,默認是監控本機的所有網卡上。局域網內主機若要使用dnsmasq服務時,指定本機的IP地址 listen-address=10.77.3.8,127.0.0.1 #本地域名配置文件(不支持泛域名),添加內部需要解析的地址和域名(重新加載即可生效) addn-hosts=/etc/dnsmasq.hosts #緩存條數 cache-size=100 #為防止DNS污染,使用參數定義的DNS解析的服務器。注意:如果是阿里雲服務器上配置dnsmasq要啟用此項。 bogus-nxdomain=114.114.114.114 #可以通過server對不通的網站使用不通的DNS服務器進行解析。如下表示對於google的服務,使用谷歌的DNS解析 server=/google.com/8.8.8.8 #記錄dns查詢日志服務器 log-queries #設置日志記錄器 log-facility=/var/log/dnsmasq.log
2、在/etc/dnsmasq.hosts中編輯添加需要解析的內部地址
vim /etc/dnsmasq.hosts
#(testhjs.com為自定義的域名與10.77.3.8進行關聯,其他pc或服務器等client配置了10.77.3.8為dns,即可解析此域名) 10.77.3.8 testhjs.com #(testhjs2.com為自定義的域名與10.77.3.119進行關聯,其他pc或服務器等client配置了10.77.3.8為dns,即可解析此域名) 10.77.3.119 testhjs2.com
3、在/etc/resolv.dnsmasq.conf添加上游dns服務地址,也就是本地dns10.77.3.8無法解析的時候自動切換到上游dns服務
nameserver 114.114.114.114 nameserver 115.115.115.115
三、其他配置以及說明
1、配置日志輪轉,編輯/etc/logrotate.d/dnsmasq
/var/log/dnsmasq.log {
daily
copytruncate
missingok
rotate 30
compress
notifempty
dateext
size 200M
}
2、安裝dig&nslookup
Ubuntu:
sudo apt-get install dnsutils
Centos:
yum install bind-utils
客戶端測試域名是否生效:nslookup www.freeoa.net檢查解析的IP即可,或使用dig指令。
3、關閉防火牆,或放行dns的端口53
4、客戶端配置上自己搭建的dns地址進行測試
由於dns服務端配置了/etc/dnsmasq.conf中配置了address=/baidu.com/10.77.3.8,所以下面的dns解析地址為10.77.3.8
root@test ~]# dig baidu.com ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> baidu.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53090 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;baidu.com. IN A ;; ANSWER SECTION: baidu.com. 0 IN A 10.77.3.8 ;; Query time: 0 msec ;; SERVER: 127.0.0.1#53(127.0.0.1) ;; WHEN: Thu Apr 12 10:38:28 2018 ;; MSG SIZE rcvd: 43 [root@test ~]# nslookup baidu.com Server: 10.77.3.8 Address: 10.77.3.8#53 Name: baidu.com Address: 10.77.3.8 自定義域名解析 [root@test ~]# dig testhjs.com ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.el6_9.5 <<>> testhjs.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55740 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;testhjs.com. IN A ;; ANSWER SECTION: testhjs.com. 0 IN A 10.77.3.8 ;; Query time: 0 msec ;; SERVER: 10.77.3.8#53(10.77.3.8) ;; WHEN: Thu Apr 12 10:41:13 2018 ;; MSG SIZE rcvd: 45 [root@test ~]# nslookup testhjs.com Server: 10.77.3.8 Address: 10.77.3.8#53 Name: testhjs.com Address: 10.77.3.8
