频繁的域名解析容易导致超时出错
在执行
opkg-cl update或yaourt -Syua命令更新系统软件信息时,容易因为频繁的域名解析而导致更新速度奇慢甚至是超时出错,通过将要访问的域名添加到/etc/hosts中,可以立即解决这个问题。考虑到服务器IP可能会换,使用/etc/hosts非长久之计。缓存域名解析结果
更好的办法是缓存域名解析结果,一般来说后台服务程序可以通过缓存域名解析结果加快后继请求的处理,但是对于像
opkg-cl、yaourt之类的工具程序,由于运行一次就退出了,要实现缓存域名解析结果就显得有点小题大作了,最好是在系统底层来实现,而不是每个应用程序都实现这个功能。Pdnsd 就是这样一款开源DNS代理服务程序,它安装在客户机上,对于客户端应用程序来说,它是DNS服务程序,对于真正的DNS服务来说它是DNS客户端程序。
Pdnsd 安装
yaourt -S pdnsd
Pdnsd 运行
sudo systemctl enable pdnsd sudo systemctl start pdnsd
客户机DNS设置
修改
/etc/resolv.conf为如下内容:nameserver 127.0.0.1
对于 Archlinux
/etc/resolv.conf是由resolvconf工具生成,直接修改后随时可能被覆盖,可以修改/etc/resolvconf.conf将以下配置行取消注释:# name_servers=127.0.0.1
然后重新生成
/etc/resolv.conf配置文件:sudo resolvconf -u
看看效果
多次执行下面的命令,可以感觉到后几次明显比第一次快,这就是DNS缓存在起作用。
nslookup www.google.com
适合国内环境的配置(仅供参考)
将
/etc/pdnsd.conf配置文件修改为以下内容:global { perm_cache=1024; cache_dir="/var/cache/pdnsd"; pid_file = /var/run/pdnsd.pid; run_as="pdnsd"; server_ip = 127.0.0.1; status_ctl = on; query_method=udp_tcp; min_ttl=15m; max_ttl=1d; timeout=10; neg_domain_pol=on; udpbufsize=1024; } server { label = "root-servers"; root_server = discover; randomize_servers = on; ip = 114.114.114.114, 223.5.5.5, 114.114.115.115, 223.6.6.6; timeout = 5; uptest = query; interval = 30m; ping_timeout = 50; purge_cache = off; exclude = .localdomain; policy = included; preset = off; } source { owner=localhost; serve_aliases=on; file="/etc/hosts"; } rr { name=localhost; reverse=on; a=127.0.0.1; owner=localhost; soa=localhost,root.localhost,42,86400,900,86400,86400; }重启 Pdnsd 生效配置:
sudo systemctl restart pdnsd
配置pdnsd使用dhcp分配的dns服务器
往往dhcp提供的dns服务器是最快的(它可能也做了缓存),用到了本地域名的情况下必须使用dhcp提供的dns服务器,如果将dns服务器写死在pdnsd.conf,切换网络(如从公司回到家里)就上不了网了,其实
resolvconf对pdnsd提供了支持。参考
man resolvconf将/etc/resolvconf.conf改为name_servers=127.0.0.1 pdnsd_conf=/etc/pdnsd.conf
删掉
/etc/pdnsd.conf中的所有server配置块。重启
NetworkManager生效配置sudo systemctl restart NetworkManager
现在
/etc/pdnsd.conf中的server配置块将由resolvconf来提供。参考