之前使用 Dnsmasq 搭建本地 DNS 缓存服务器,有个缺点就是必须要设置一个上级 DNS 服务器。上级 DNS 服务器选 Google 的 8.8.8.8 时,会导致一些网站(如淘宝)解析成国外 IP,导致访问速度变慢。选用国内的公共 DNS,又无法起到防止 DNS 污染的功能。改用 Bind 搭建本地 DNS 缓存可以解决这个问题,因为 Bind 可以自己从根域解析。
安装 Bind
# Arch Linux
sudo pacman -S bind
# CentOS
sudo yum install bind
# Debian
sudo apt-get install bind9
启动服务
默认配置下,Bind 即是一个递归 DNS 缓存服务器,直接启动 Bind 就行了。
Arch Linux
# Arch Linux
sudo systemctl start named.service
# CentOS
sudo service named start
# Debian/Ubuntu
sudo service bind start
测试
用下面的命令测试 Bind 是否正常工作
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: 20218
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 4, ADDITIONAL: 5
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.google.com. IN A
;; ANSWER SECTION:
www.google.com. 300 IN A 74.125.68.103
www.google.com. 300 IN A 74.125.68.106
www.google.com. 300 IN A 74.125.68.105
www.google.com. 300 IN A 74.125.68.99
www.google.com. 300 IN A 74.125.68.147
www.google.com. 300 IN A 74.125.68.104
;; AUTHORITY SECTION:
google.com. 166510 IN NS ns4.google.com.
google.com. 166510 IN NS ns3.google.com.
google.com. 166510 IN NS ns1.google.com.
google.com. 166510 IN NS ns2.google.com.
;; ADDITIONAL SECTION:
ns1.google.com. 166510 IN A 216.239.32.10
ns2.google.com. 166510 IN A 216.239.34.10
ns3.google.com. 166510 IN A 216.239.36.10
ns4.google.com. 166510 IN A 216.239.38.10
;; Query time: 159 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sun Jun 22 15:37:05 2014
;; MSG SIZE rcvd: 275
修改系统默认 DNS 服务器
Arch Linux
编辑/etc/resolvconf.conf
name_servers=127.0.0.1
CentOS/Debian/Ubuntu
编辑/etc/resolv.conf
nameserver 127.0.0.1