centos下dnsmasq安裝與配置

說明
一、dnsmasq是一款小巧且方便地用於配置DNS服務器和DHCP服務器的工具,適用於小型網絡,它提供了DNS解析功能和可選擇的DHCP功能。
二、dnsmasq能夠解決小範圍的dns查詢問題,若是業務是跨機房、跨地區的話不建議使用dnsmasq作爲dns解析服務器。 vim

安裝
一、下載緩存

wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.75.tar.gz

二、環境準備bash

yum -y install gcc

三、解壓服務器

tar -xf dnsmasq-2.75.tar.gz

四、編譯安裝網絡

cd dnsmasq-2.75
make install

五、版本查看dom

dnsmasq -v

六、編輯啓動腳本
#服務啓動腳本
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

賦予執行的權限post

chmod +x /etc/init.d/dnsmasq

七、啓動測試

/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

八、其餘方式安裝
Centos安裝

yum -y install dnsmasq

Ubuntu 安裝

sudo apt-get -y install dnsmasq

配置
1、三個關鍵配置文件
一、主配置文件,安裝後自動生成
/etc/dnsmasq.conf
二、添加內部須要解析的地址和域名
/etc/dnsmasq.hosts
三、dnsmasq的上游DNS服務器,能夠將reslove.conf配置文件複製爲resolv.dnsmasq.conf,進行添加nameserver
/etc/resolv.dnsmasq.conf

2、編輯配置文件
一、編輯配置/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

二、在/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 

三、在/etc/resolv.dnsmasq.conf添加上游dns服務地址,也就是本地dns10.77.3.8沒法解析的時候自動切換到上游dns服務

nameserver 114.114.114.114
nameserver 115.115.115.115

3、其餘配置以及說明
一、配置日誌輪轉,編輯/etc/logrotate.d/dnsmasq

/var/log/dnsmasq.log {
daily
copytruncate
missingok
rotate 30
compress
notifempty
dateext
size 200M
}

二、安裝dig&nslookup
Ubuntu:

sudo apt-get install dnsutils

Centos:

yum install bind-utils

客戶端測試域名是否生效:nslookup www.freeoa.net檢查解析的IP便可,或使用dig指令。

三、關閉防火牆,或放行dns的端口53

四、客戶端配置上本身搭建的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
相關文章
相關標籤/搜索