利用 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
1
$ apt-get install dnsmasq
  • Centos/RHEL
1
$ yum install dnsmasq

配置Dnsmasq

Dnsmasq處理DNS設置與BIND等其他DNS服務有所不同。所有的配置都在一個文件中完成/etc/dnsmasq.conf。默認情況下dnsmasq.conf中只開啟了最后include項,可以在/etc/dnsmasq.d中自己寫任意名字的配置文件。

配置文件說明

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
用指定的端口代替默認的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之間的查詢從哪個網卡出去
# server=10.1.2.3@eth1

指定源地址攜帶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).
# server=10.1.2.3@192.168.1.1#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服務器
1
2
3
$ vi /etc/dnsmasq.conf

resolv-file=/etc/resolv.dnsmasq.conf
  • 在指定文件中增加轉發DNS的地址
1
2
3
4
$ vi /etc/resolv.dnsmasq.conf

nameserver 8.8.8.8
nameserver 8.8.4.4
本地啟用Dnsmasq解析
1
2
3
$ vi /etc/resolv.conf

nameserver 127.0.0.1
添加解析記錄
  • 使用系統默認hosts

編輯hosts文件,簡單列舉一下格式

1
2
3
4
5
$ 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文件位置。

1
2
3
$ vi /etc/dnsmasq.conf

addn-hosts=/etc/dnsmasq.hosts

在/etc/dnsmasq.hosts文件中添加DNS記錄

1
2
3
4
$ vi /etc/dnsmasq.hosts

192.168.101.107 web01.mike.com web01
192.168.101.107 web02.mike.com web02
  • 使用自定義conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ 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端口可對外訪問
1
2
$ iptables -A INPUT -p udp -m udp --dport 53 -j ACCEPT
$ iptables -A INPUT -p tcp -m tcp --dport 53 -j ACCEPT
  • 轉發DNS請求
1
2
3
4
5
6
7
8
9
10
# 開啟流量轉發功能
$ 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
  • 保存規則並重啟
1
2
$ service iptables save
$ service iptables restart

測試Dnsmasq

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

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

1
$ dig @192.168.101.104 www.hi-linux.com

一些Dnsmasq技巧

Dnsmasq性能優化

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

具體實現步驟

  • 配置dnsmasq
1
2
3
4
$ vim /etc/dnsmasq.conf

no-hosts
addn-hosts=/dev/shm/dnsrecord.txt
  • 解決同步問題
1
2
3
4
5
6
# 開機啟動
$ 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響應變慢。

解決方法

1
2
3
4
5
$ 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服務器解析。

項目地址: https://github.com/felixonmars/dnsmasq-china-list

dnsmasq-china-list使用

  • 取消dnsmasq.conf里conf-dir=/etc/dnsmasq.d這一行的注釋
  • 獲取項目文件
1
$ 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服務器的工具(可選)。

參考文檔

http://www.google.com
http://purplegrape.blog.51cto.com/1330104/1083354
https://i-meto.com/archives/iptables_PREROUTING.html
http://blog.itphp.org/archives/225


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM