1.缘起
好吧,ubuntu下面打开oschina.net,打开linux.cn等等都很慢,根源在于dns解析很慢,那怎么解决呢?
2.网上常用解决办法及存在问题
dnsmasq
使用dnsmasq可以缓存一段时间的dns到本机,但是过上3、5分钟又得重新到互联网上取一遍dns,然后又慢得要死。
nscd
理论上装了nscd后,在/var/db/下有nscd相关的缓存文件,但不知道为啥,我安装了nscd之后,然后启动了nscd,但是/var/db/下不存在nscd相关的缓存文件,然后每次nslookup oschina.net都会去114.114.114.114查询,慢死了。
3.ubuntu14.04 解决办法
(1)卸载dnsmasq、nscd
正常ubuntu不会安装这两个东西,如果安装了就记得卸载掉,卸载命令
sudo apt-get purge dnsmasq nscd
(2)注销networkmanager的dnsmasq功能
打开 /etc/NetworkManager/NetworkManager.conf
删除dns=dnsmasq
(3)安装、配置pdnsd
1)安装
sudo apt-get install pdnsd
安装时选择resolv.conf,不要选择manual
2)配置
修改/etc/defaults/pdnsd
将其中的START_DAEMON=no改为yes
修改/etc/pdnsd.conf
修改global里面的min_ttl=1d;修改完成后的结果见下
global {
perm_cache=2048;
cache_dir="/var/cache/pdnsd";
run_as="pdnsd";
server_ip = 127.0.0.1; // Use eth0 here if you want to allow other
// machines on your network to query pdnsd.
status_ctl = on;
paranoid=on;
// query_method=tcp_udp; // pdnsd must be compiled with tcp
// query support for this to work.
min_ttl=1d; // Retain cached entries at least 15 minutes.
max_ttl=1w; // One week.
timeout=10; // Global timeout option (10 seconds).
// Don't enable if you don't recurse yourself, can lead to problems
// delegation_only="com","net";
}
修改server的ip,修改完成后的结果见下
server {
label = "root-servers";
root_server=on;
ip = 114.114.114.114
, 8.8.8.8
;
timeout = 5;
uptest = query;
interval = 30m; // Test every half hour.
ping_timeout = 300; // 30 seconds.
purge_cache = off;
exclude = .localdomain;
policy = included;
preset = off;
}
修改/etc/resolv.conf
修改完成后的结果见下
nameserver 127.0.0.1
nameserver 114.114.114.114
nameserver 8.8.8.8
设置/etc/resolv.conf的不可更改属性
sudo chattr +i resolv.conf
上述命令如果报错:chattr: Operation not supported while reading flags on /etc/resolv.conf
则执行下述命令并重启,然后重新建立resolv.conf
sudo apt-get purge resolvconf
4.ubuntu16.04解决办法
不知道为啥,16.04安装了pdnsd之后无法cache,du -h /var/cache/pdnsd 一直返回8k,然后,发现16.04如果修改了某些东西后解析dns好像还是蛮快的
(1)卸载resolvconf和avahi-daemon(可能有的没有安装,就不用卸载了)
sudo apt-get purge resolvconf avahi-daemon
(2)修改/etc/NetworkManager/NetworkManager.conf
原始值:dns=dnsmasq
修改后:dns=none
(3)/etc/resolv.conf里写上阿里dns、114dns、googledns
sudo rm -rfv /etc/resolv.conf
sudo touch /etc/resolv.conf
修改后的/etc/resolv.conf
nameserver 223.5.5.5
nameserver 114.114.114.114
nameserver 8.8.8.8