1. 程式人生 > >利用Dnsmasq部署DNS服務

利用Dnsmasq部署DNS服務

Dnsmasq提供DNS快取和DHCP服務、Tftp服務功能。作為域名解析伺服器(DNS),Dnsmasq可以通過快取DNS請求來提高對訪問過的網址的連線速度。作為DHCP伺服器,Dnsmasq可以為區域網電腦提供內網ip地址和路由。DNS和DHCP兩個功能可以同時或分別單獨實現。Dnsmasq輕量且易配置,適用於個人使用者或少於50臺主機的網路。此外它還自帶了一個PXE伺服器。

Dnsmasq工作原理

當接受到一個DNS請求時,Dnsmasq首先會查詢/etc/hosts這個檔案,然後查詢/etc/resolv.conf中定義的外部DNS。所以說Dnsmasq是一個很不錯的外部DNS中繼。

配置Dnsmasq為DNS快取伺服器,同時在/etc/hosts

檔案中加入本地內網解析,這樣一來每當內網機器查詢時就會優先查詢hosts檔案,這就等於將/etc/hosts共享給全內網機器使用,從而解決內網機器互相識別的問題。相比逐臺機器編輯hosts檔案或者新增Bind DNS記錄,僅編輯一個hosts檔案,這簡直太容易了。

安裝Dnsmasq

  • Ubuntu/Debian
$ apt-get install dnsmasq
  • Centos/RHEL
$ yum install dnsmasq

配置Dnsmasq

Dnsmasq處理DNS設定與BIND等其他DNS服務有所不同。所有的配置都在一個檔案中完成/etc/dnsmasq.conf。預設情況下dnsmasq.conf中只開啟了最後include項,可以在/etc/dnsmasq.d

中自己寫任意名字的配置檔案。

配置檔案說明

Dnsmasq配置檔案是/etc/dnsmasq.conf,下面對Dnsmasq中和DNS相關的配置項進行說明。

用指定的埠代替預設的DNS 53埠,如果設定為0,則完全禁止DNS功能,只使用dhcp服務
port=5353

以下兩個引數告訴Dnsmasq過濾一些查詢:1.哪些公共DNS沒有回答 2.哪些root根域不可達。

從不轉發格式錯誤的域名
#domain-needed

從不轉發不在路由地址中的域名
#bogus-priv

resolv-file配置Dnsmasq額外的向流的DNS伺服器,如果不開啟就使用linux主機預設的/etc/resolv.conf裡的nameserver,通過下面的選項指定其他檔案。
resolv-file=/etc/dnsmasq.d/upstream_dns.conf

預設情況下Dnsmasq會發送查詢到它的任何上游DNS伺服器上,如果取消註釋,則Dnsmasq則會嚴格按照/etc/resolv.conf中的DNS Server順序進行查詢。
#strict-order

以下兩個引數控制是否通過/etc/resolv.conf確定上游伺服器,是否檢測/etc/resolv.conf的變化,則取消註釋。 

如果你不想Dnsmasq讀取/etc/resolv.conf檔案或者其他檔案,獲得它的servers
# If you don't want dnsmasq to read /etc/resolv.conf or any other
# file, getting its servers from this file instead (see below), then
# uncomment this.
#no-resolv

如果你不允許Dnsmasq通過輪詢/etc/resolv.conf或者其他檔案來獲取配置的改變,則取消註釋。 
#no-poll

增加一個name server,一般用於內網域名
#server=/localnet/192.168.0.1

設定一個反向解析,所有192.168.3.0/24的地址都到10.1.2.3去解析
#server=/3.168.192.in-addr.arpa/10.1.2.3

增加一個本地域名,會在/etc/hosts中進行查詢
#local=/localnet/

增加一個域名,強制解析到你指定的地址上
#address=/double-click.net/127.0.0.1

同上,還支援ipv6
#address=/www.thekelleys.org.uk/fe80::20d:60ff:fe36:f83

增加查詢yahoo google和它們的子域名到vpn、search查詢
# Add the IPs of all queries to yahoo.com, google.com, and their
# subdomains to the vpn and search ipsets:
#ipset=/yahoo.com/google.com/vpn,search

你還可以控制Dnsmasq和Server之間的查詢從哪個網絡卡出去
# 
[email protected]
指定源地址攜帶10.1.2.3地址和192.168.1.1的55埠進行通訊 # and this sets the source (ie local) address used to talk to # 10.1.2.3 to 192.168.1.1 port 55 (there must be a interface with that # IP on the machine, obviously). # [email protected]#55 改變Dnsmasq預設的uid和gid #user= #group= 如果你想Dnsmasq監聽某個埠為dhcp、dns提供服務 #interface= 你還可以指定哪個埠你不想監聽 #except-interface= 設定想監聽的地址,如果你本機要使用寫上127.0.0.1。 #listen-address= 如果你想在某個埠只提供dns服務,則可以進行配置禁止dhcp服務 #no-dhcp-interface= # On systems which support it, dnsmasq binds the wildcard address, # even when it is listening on only some interfaces. It then discards # requests that it shouldn't reply to. This has the advantage of # working even when interfaces come and go and change address. If you # want dnsmasq to really bind only the interfaces it is listening on, # uncomment this option. About the only time you may need this is when # running another nameserver on the same machine. #bind-interfaces 如果你不想使用/etc/hosts,則取消下面的註釋 #no-hosts 如果你項讀取其他類似/etc/hosts檔案,則進行配置 addn-hosts=/etc/banner_add_hosts 自動的給hosts中的name增加一個域名 #expand-hosts 給dhcp服務賦予一個域名 #domain=thekelleys.org.uk 給dhcp的一個子域賦予一個不同的域名 #domain=wireless.thekelleys.org.uk,192.168.2.0/24 同上,不過子域是一個範圍 #domain=reserved.thekelleys.org.uk,192.68.3.100,192.168.3.200 dhcp分發ip的範圍,以及每個ip的租約時間 #dhcp-range=192.168.0.50,192.168.0.150,12h 同上,不過給出了掩碼 #dhcp-range=192.168.0.50,192.168.0.150,255.255.255.0,12h 自動載入conf-dir目錄下的配置檔案 conf-dir=/etc/dnsmasq.d 設定dns快取大小,預設為150條 cache-size=150

配置例項

配置上游伺服器地址

resolv-file配置Dnsmasq額外的上游的DNS伺服器,如果不開啟就使用Linux主機預設的/etc/resolv.conf裡的nameserver。

  • 通過下面的選項指定其他檔案來管理上游的DNS伺服器
$ vi /etc/dnsmasq.conf

resolv-file=/etc/resolv.dnsmasq.conf
  • 在指定檔案中增加轉發DNS的地址
$ vi /etc/resolv.dnsmasq.conf

nameserver 8.8.8.8
nameserver 8.8.4.4

本地啟用Dnsmasq解析

$ vi /etc/resolv.conf

nameserver 127.0.0.1

新增解析記錄

  • 使用系統預設hosts

編輯hosts檔案,簡單列舉一下格式

$ vi /etc/hosts

127.0.0.1  localhost 
192.168.101.107   web01.mike.com web01
192.168.101.107   web02.mike.com web02

hosts檔案的強大之處還在於能夠劫持解析,譬如mirror.centos.org是CentOS倉庫所在,幾乎是機器正常必訪問一個域名,我將它解析成一個內網地址,搭建一個內網映象站,不僅內網機器也可以及時得到安全更新,每月還可以節省很多流量。

  • 使用自定義hosts檔案

修改配置,增加自定義hosts檔案位置。

$ vi /etc/dnsmasq.conf

addn-hosts=/etc/dnsmasq.hosts

在/etc/dnsmasq.hosts檔案中新增DNS記錄

$ vi /etc/dnsmasq.hosts

192.168.101.107   web01.mike.com    web01 
192.168.101.107   web02.mike.com    web02
  • 使用自定義conf
$ vi /etc/dnsmasq.d/address.conf

# 指定dnsmasq預設查詢的上游伺服器,此處以Google Public DNS為例。
server=8.8.8.8
server=8.8.4.4

# 把所有.cn的域名全部通過114.114.114.114這臺國內DNS伺服器來解析
server=/cn/114.114.114.114

# 給*.apple.com和taobao.com使用專用的DNS
server=/taobao.com/223.5.5.5
server=/.apple.com/223.5.5.5

# 把www.hi-linux.com解析到特定的IP
address=/www.hi-linux.com/192.168.101.107

在這裡hi-linux.com相當於*.mike.com泛解析
address=/hi-linux.com/192.168.101.107

注:也可以直接新增到/etc/dnsmasq.conf中,不過/etc/dnsmasq.d/*.conf的優先順序大於/etc/dnsmasq.conf。

修改iptables配置

  • 允許本機的53埠可對外訪問
$ iptables -A INPUT -p udp -m udp --dport 53 -j ACCEPT
$ iptables -A INPUT -p tcp -m tcp --dport 53 -j ACCEPT
  • 轉發DNS請求
# 開啟流量轉發功能
$ echo '1' > /proc/sys/net/ipv4/ip_forward
$ echo '1' > /proc/sys/net/ipv6/ip_forward   # IPv6 使用者選用

# 新增流量轉發規則,將外部到53的埠的請求對映到Dnsmasq伺服器的53埠
$ iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 53
$ iptables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 53

# 如果要限制只允許內網的請求,方法如下
$ iptables -t nat -A PREROUTING -i eth1 -p upd --dport 53 -j REDIRECT --to-port 53
  • 儲存規則並重啟
$ service iptables save
$ service iptables restart

測試Dnsmasq

  • 啟動Dnsmasq
$ service dnsmasq start
  • 測試Dnsmasq

將其他機器的DNS換成dnsmasq所在的IP即可,就這麼容易。

$ dig @192.168.101.104 www.hi-linux.com

一些Dnsmasq技巧

Dnsmasq效能優化

我們都知道Bind不配合資料庫的情況下,經常需要重新載入並讀取配置檔案,這是造成效能低下的原因。根據這點教訓,我們可以考慮不讀取/etc/hosts檔案。而是另外指定一個在共享記憶體裡的檔案,比如/dev/shm/dnsrecord.txt ,這樣就不費勁了,又由於記憶體的非永續性,重啟就消失,可以定期同步硬碟上的某個內容到記憶體檔案中。

具體實現步驟

  • 配置dnsmasq
$ vim /etc/dnsmasq.conf

no-hosts 
addn-hosts=/dev/shm/dnsrecord.txt
  • 解決同步問題
# 開機啟動
$ echo "cat /etc/hosts > /dev/shm/dnsrecord.txt" >>/etc/rc.local 

# 定時同步內容
$ crontab -e 
*/10 * * * * cat /etc/hosts > /dev/shm/dnsrecord.txt

Dnsmasq選擇最快的上游DNS伺服器

經常會有這樣的情景,Dnsmasq伺服器配了一堆上游伺服器,轉發本地的dns請求,預設是Dnsmasq事實上是隻挑了一個上游dns伺服器來查詢並轉發結果,這樣如果選錯伺服器的話會導致DNS響應變慢。

解決方法

$ vi /etc/dnsmasq.conf

all-servers  
server=8.8.8.8  
server=219.141.136.10

all-servers表示對以下設定的所有server發起查詢,選擇迴應最快的一條作為查詢結果返回。
上面我們設定了兩個dns server,8.8.8.8(谷歌dns)和219.141.136.10(移動的dns),會同時查詢這兩個伺服器,詢問dns地址誰返回快就採用誰的結果。

dnsmasq-china-list專案

dnsmasq-china-list專案維護了一張國內常用但是通過國外DNS會解析錯誤的網站域名的列表,保證List中的國內域名全部走國內DNS伺服器解析。

dnsmasq-china-list使用

  • 取消dnsmasq.conf裡conf-dir=/etc/dnsmasq.d這一行的註釋
  • 獲取專案檔案
$ git clone https://github.com/felixonmars/dnsmasq-china-list.git
  • 將accelerated-domains.china.conf, bogus-nxdomain.china.conf,google.china.conf(可選)放到/etc/dnsmasq.d/目錄下(如目錄不存在則建立一個)。
  • dnsmasq-update-china-list放到/usr/bin/,這是一個批量修改DNS伺服器的工具(可選)。

參考文件

文/Mike

文章出處:運維之美