DNSMAQ 搭建 DNS 服務

DNSmasq是一個小巧且方便地用於配置DNS和DHCP的工具,適用於小型網絡,它提供了DNS功能和可選擇的DHCP功能。本身搭建公共DNS更加靈活,若是是在本地搭建,還能夠大幅提升解析速度。
相比較BIND那複雜的配置來講,dnsmasq輕量不少python

centos7安裝dnsmaq

yum -y install dnsmasq      #yum方式安裝

配置

1.配置文件在 /etc/dnsmasq.conf,咱們要讓它能用起來須要作以下配置:centos

#指定上游dns服務器    
resolv-file=/etc/resolv.dnsmasq.conf    
#表示嚴格按照 resolv-file 文件中的順序從上到下進行 DNS 解析, 直到第一個成功解析成功爲止    
strict-order    
# 開啓後會尋找本地的hosts文件在去尋找緩存的域名,最後到上游dns查找    #no-resolv    
listen-address=0.0.0.0      #0.0.0.0 設置爲公網IP    
conf-dir=/etc/dnsmasq.d     # 咱們的解析記錄都寫到這個目錄下

2.建立 /etc/resolv.dnsmasq.conf ,而後添加:緩存

# cat /etc/resolv.dnsmasq.conf    
nameserver 119.29.29.29    
nameserver 114.114.114.114    
nameserver 8.8.8.8    
nameserver 168.95.1.1

3.而後建立 /etc/dnsmasq.d/cloud.conf,添加:服務器

address=/baidu.com/127.0.0.1        # 將百度的域名解析到127.0.0.1       
address=/ad.youku.com/127.0.0.1     # 禁止優酷廣告    
address=/ad.iqiyi.com/127.0.0.1     # 禁止iqiyi廣告

格式是:網絡

address=/domain.com/dns

好比上面的百度,我就把它都解析到127.0.0.1。dom

實現DNS分流

server=/cn/114.114.114.114          # cn的域名都走114的dns    
server=/google.com/115.159.220.214  # 將谷歌的解析都走115.159.220.214

上面的是將全部cn結尾的域名都走114解析,下面是將google.com 走115.159.220.214解析。工具

開啓防火牆53端口後,本地測試下 53 端口是不是通的:測試

➜  www nc -vuz 121.42.18.6 53    
found 0 associations    
found 1 connections:         
    1:    flags=82<CONNECTED,PREFERRED>        
    outif (null)        
    src 192.168.2.32 port 49939        
    dst 121.42.18.6 port 53        
    rank info not available    
Connection to 121.42.18.6 port 53 [udp/domain] succeeded!
service dnsmasq start       #啓動dnsmaq:
systemctl enable dnsmasq    #設置爲開機自啓動。

測試

➜  www dig m.baidu.com @121.42.18.6    
; <<>> DiG 9.8.3-P1 <<>> m.baidu.com @121.42.18.6    
;; global options: +cmd    
;; Got answer:    
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41523    
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0    

;; QUESTION SECTION:    
;m.baidu.com.            IN  A    

;; ANSWER SECTION:    
m.baidu.com.        0   IN  A   127.0.0.1    

;; Query time: 30 msec    
;; SERVER: 121.42.18.6#53(121.42.18.6)    
;; WHEN: Mon Aug 28 10:32:27 2017    
;; MSG SIZE  rcvd: 45
    
# 能夠看到,百度的子域名已經被解析到127.0.0.1了,此外還能夠配合dnscrypt-proxy 對查詢進行加密,這裏就不展開了。
相關文章
相關標籤/搜索