dnsmasq 使用上比bind要簡便得多,可以做正向、反向dns解析,支持DHCP服務。也可以做內部dns服務器用.
本初僅使用DNS功能,給本地局域網服務器提供:hosts主機記錄,自定義域名,以及公網域名DNS轉發解析。
實現功能:利用dnsmasq做dns解析服務,內網的域名解析為本機的ip地址,外網的域名通過dns解析為外網對應的地址.
配置文件解析:
dnsmasq安裝完成后,默認配置文件為 /etc/dnsmasq.conf,本配置文件只列舉出常用的幾個配置項的使用方法.
Dnsmasq配置文件dnsmasq.conf詳解: # 緩存條數,默認為150條。cache-size=0 禁用緩存 cache-size=10000 ##指定 resolv-file 文件路徑(上游DNS服務器),默認/etc/resolv.dnsmasq。 resolv-file=/etc/dnsmasq-resolv.conf ###添加讀取額外的 hosts 文件路徑,可以多次指定。如果指定為目錄,則讀取目錄中的所有文件。 addn-hosts=/etc/dnsmasq.hosts #表示嚴格按照resolv.conf中的順序進行查找 strict-order #用此主機為局域網提供默認 DNS,寫本機的局域網IP listen-address=192.168.10.30,127.0.0.1 #設置日志記錄器 log-queries log-facility=/var/log/dnsmasq.log local-ttl=600 # 該目錄下的所有.conf文件都是要做解析的 conf-dir=/etc/dnsmasq.d # 指定域名解析到特定的ip上,后面的ip地址為你的主機ip: address=/saneri.com/192.168.10.30 # 智能DNS加快解析速度,后面ip地址為dns server=/cn/114.114.114.114 server=/taobao.com/114.114.114.114
開始搭建一台dnsmasq域名解析服務器:
本機的ip地址是:192.168.10.30,其中192.168.10.2為dns和網關的ip地址,可以正常訪問互聯網.
# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.10.2
1.安裝:
# yum install dnsmasq -y
2.啟動服務:
# systemctl enable dnsmasq.service
# systemctl start dnsmasq.service
3.查看版本:
# dnsmasq -v
4.修改配置文件:
dnsmasq配置選項比較多,我們可以根據實際需求來進行配置。下面為我的配置文件:
# cat /etc/dnsmasq.conf cache-size=10000 resolv-file=/etc/dnsmasq-resolv.conf addn-hosts=/etc/dnsmasq.hosts log-queries log-facility=/var/log/dnsmasq.log local-ttl=600 conf-dir=/etc/dnsmasq.d strict-order listen-address=192.168.10.30,127.0.0.1
指定 resolv-file 文件路徑(上游DNS服務器),默認/etc/resolv.dnsmasq,此處為/etc/dnsmasq-resolv.conf文件,和/etc/resolv.conf文件內容保持一致.
resolv-file=/etc/dnsmasq-resolv.conf
解析是有順序的,所以一定要將本機的ip作為dns放在前面,如果順序不對,解析也是會出問題的.
[root@localhost etc]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 192.168.10.30 nameserver 192.168.10.2 [root@localhost etc]# cat /etc/dnsmasq-resolv.conf nameserver 192.168.10.30 nameserver 192.168.10.2 [root@localhost etc]#
#添加讀取額外的 hosts 文件路徑,可以多次指定。如果指定為目錄,則讀取目錄中的所有文件,這里指定的域名和ip都是一對一的關系.
addn-hosts=/etc/dnsmasq.hosts
[root@localhost etc]# cat /etc/dnsmasq.hosts 192.168.10.30 www.saneri.com.io 192.168.10.30 ftp.saneri.com.io 192.168.10.30 momo.com.io [root@localhost etc]#
# 該目錄下的所有.conf文件都是要做解析的,我在這個目錄下創建了一個address.conf的配置文件,用來將域名解析到特定的ip上面,這種定義方式支持泛域名解析和反向解析等方式.
conf-dir=/etc/dnsmasq.d
[root@localhost etc]# cat /etc/dnsmasq.d/address.conf address=/www.taobao.com/127.0.0.1 ptr-record=127.0.0.1.in-addr.arpa,www.taobao.com address=/baidu.com/127.0.0.1 address=/saneri.com/192.168.10.30 [root@localhost etc]#
這些就是我的配置項,內網的域名解析到指定的主機ip上面,外網的域名解析正常.
5.解析測試
可以在本機使用nslookup進行測試,也可以在開一個客戶機,將客戶機的dns指向為192.168.10.30進行解析測試;
# nslookup > www.saneri.com.io Server: 192.168.10.30 Address: 192.168.10.30#53 Name: www.saneri.com.io Address: 192.168.10.30 > ftp.saneri.com.io Server: 192.168.10.30 Address: 192.168.10.30#53 Name: ftp.saneri.com.io Address: 192.168.10.30 > momo.com.io Server: 192.168.10.30 Address: 192.168.10.30#53 Name: momo.com.io Address: 192.168.10.30 > www.taobao.com Server: 192.168.10.30 Address: 192.168.10.30#53 Name: www.taobao.com Address: 127.0.0.1 > saneri.com Server: 192.168.10.30 Address: 192.168.10.30#53 Name: saneri.com Address: 192.168.10.30 > baidu.com Server: 192.168.10.30 Address: 192.168.10.30#53 Name: baidu.com Address: 127.0.0.1 > www.baidu.com Server: 192.168.10.30 Address: 192.168.10.30#53 Name: www.baidu.com Address: 127.0.0.1 > www.sina.cn Server: 192.168.10.30 Address: 192.168.10.30#53 Non-authoritative answer: www.sina.cn canonical name = sina.cn. Name: sina.cn Address: 49.7.36.58 > exit [root@localhost ~]#
參考文檔:https://blog.51cto.com/longlei/2065967