搭建本地 DNS 缓存

日期: 05 月 24日, 2014
标签:

搭建本地 DNS 缓存的好处有很多:

  1. 加快 DNS 解析速度
  2. 防止 DNS 污染
  3. 屏蔽某些网站

安装 Dnsmasq

Arch Linux

sudo pacman -S dnsmasq

Debian

sudo apt-get install dnsmasq

配置 Dnsmasq

编辑/etc/dnsmasq.conf:

no-resolv
no-hosts
cache-size=2048
server=8.8.8.8
serve=114.114.114.114

启动服务

Arch Linux

sudo systemctl enable dnsmasq.service
sudo systemctl start dnsmasq.service

Debian

sudo service dnsmasq start

测试 Dnsmasq

用下面的命令测试 Dnsmasq 是否正常工作

dig @localhost www.google.com

正常的话应有类似下面的输出

; <<>> DiG 9.9.2-P2 <<>> @localhost www.google.com
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29369
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 4, ADDITIONAL: 5

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.google.com.			IN	A

;; ANSWER SECTION:
www.google.com.		51	IN	A	173.194.127.113
www.google.com.		51	IN	A	173.194.127.116
www.google.com.		51	IN	A	173.194.127.114
www.google.com.		51	IN	A	173.194.127.115
www.google.com.		51	IN	A	173.194.127.112

;; AUTHORITY SECTION:
google.com.		544	IN	NS	ns1.google.com.
google.com.		544	IN	NS	ns3.google.com.
google.com.		544	IN	NS	ns4.google.com.
google.com.		544	IN	NS	ns2.google.com.

;; ADDITIONAL SECTION:
ns1.google.com.		578	IN	A	216.239.32.10
ns2.google.com.		578	IN	A	216.239.34.10
ns3.google.com.		578	IN	A	216.239.36.10
ns4.google.com.		578	IN	A	216.239.38.10

;; Query time: 1 msec
;; SERVER: ::1#53(::1)
;; WHEN: Sat May 24 20:48:11 2014
;; MSG SIZE  rcvd: 259

修改系统默认 DNS 服务器

Arch Linux

编辑 /etc/resolvconf.conf

name_servers=127.0.0.1

Debian

编辑/etc/resolv.conf

nameserver 127.0.0.1