Linux 中的 DNS 缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11020027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DNS caching in linux
提问by agent.smith
I am confused about DNS caching. I am writing a small forward proxy server and want to use OS DNS cache on a Linux system.
我对 DNS 缓存感到困惑。我正在编写一个小型转发代理服务器,并希望在 Linux 系统上使用 OS DNS 缓存。
If I understand correctly, then there is DNS caching at the browser level. Then there is DNS caching at OS level (Windows has it. I am not sure if Linux distros have it by default).
如果我理解正确,那么浏览器级别有 DNS 缓存。然后是操作系统级别的 DNS 缓存(Windows 拥有它。我不确定 Linux 发行版是否默认拥有它)。
So, how does a browser/proxy_server use OS DNS caching? I am trying to find if I can rely on Linux for DNS caching instead of doing it on my own inside my proxy.
那么,浏览器/proxy_server 如何使用操作系统 DNS 缓存?我试图找出是否可以依靠 Linux 进行 DNS 缓存,而不是在我的代理中自行完成。
Thanks
谢谢
采纳答案by Celada
On Linux (and probably most Unix), there is no OS-level DNS caching unless nscdis installed and running. Even then, the DNS caching feature of nscd is disabled by default at least in Debian because it's broken. The practical upshot is that your linux system very very probably does not do any OS-level DNS caching.
在 Linux(可能还有大多数 Unix)上,除非安装并运行nscd,否则没有操作系统级别的 DNS 缓存。即便如此, nscd 的 DNS 缓存功能在默认情况下至少在 Debian 中是禁用的,因为它已损坏。实际的结果是您的 linux 系统很可能不进行任何操作系统级别的 DNS 缓存。
You could implement your own cache in your application (like they did for Squid, according to diegows's comment), but I would recommend against it. It's a lot of work, it's easy to get it wrong (nscd got it wrong!!!), it likely won't be as easily tunable as a dedicated DNS cache, and it duplicates functionality that already exists outside your application.
您可以在您的应用程序中实现您自己的缓存(就像他们为 Squid 所做的那样,根据diegows的评论),但我建议不要这样做。工作量很大,很容易出错(nscd 出错了!!!),它可能不像专用 DNS 缓存那样容易调整,并且它复制了应用程序之外已经存在的功能。
If an end user using your software needs to have DNS caching because the DNS query load is large enough to be a problem or the RTT to the external DNS server is long enough to be a problem, they can install a caching DNS server such as Unboundon the same machine as your application, configured to cache responses and forward misses to the regular DNS resolvers.
如果使用你的软件的最终用户需要有 DNS 缓存,因为 DNS 查询负载大到足以成为一个问题,或者到外部 DNS 服务器的 RTT 足够长到一个问题,他们可以安装一个缓存 DNS 服务器,例如Unbound在与您的应用程序相同的机器上,配置为缓存响应并将未命中转发到常规 DNS 解析器。
回答by Zan Lynx
Here are two other software packages which can be used for DNS caching on Linux:
以下是另外两个可用于 Linux 上 DNS 缓存的软件包:
- dnsmasq
- bind
- 域名
- 绑定
After configuring the software for DNS forwarding and caching, you then set the system's DNS resolver to 127.0.0.1 in /etc/resolv.conf.
配置好 DNS 转发和缓存软件后,在 /etc/resolv.conf 中将系统的 DNS 解析器设置为 127.0.0.1。
If your system is using NetworkManager you can either try using the dns=dnsmasq
option in /etc/NetworkManager/NetworkManager.conf
or you can change your connection settings to Automatic (Address Only) and then use a script in the /etc/NetworkManager/dispatcher.d
directory to get the DHCP nameserver, set it as the DNS forwarding server in your DNS cache software and then trigger a configuration reload.
如果您的系统正在使用 NetworkManager,您可以尝试使用中的dns=dnsmasq
选项,/etc/NetworkManager/NetworkManager.conf
或者您可以将连接设置更改为自动(仅地址),然后使用/etc/NetworkManager/dispatcher.d
目录中的脚本获取 DHCP 名称服务器,将其设置为您的 DNS 转发服务器DNS 缓存软件,然后触发配置重新加载。
回答by Focus Linux
Firefox contains a dns cache. To disable the DNS cache:
Firefox 包含一个 dns 缓存。要禁用 DNS 缓存:
- Open your browser
- Type in about:config in the address bar
- Right click on the list of Properties and select New > Integer in the Context menu
- Enter 'network.dnsCacheExpiration' as the preference name and 0 as the integer value
- 打开浏览器
- 在地址栏中输入 about:config
- 右键单击“属性”列表,然后在上下文菜单中选择“新建”>“整数”
- 输入 'network.dnsCacheExpiration' 作为首选项名称和 0 作为整数值
When disabled, Firefox will use the DNS cache provided by the OS.
禁用时,Firefox 将使用操作系统提供的 DNS 缓存。
回答by Tk421
You have here available an example of DNS Caching in Debianusing dnsmasq.
您在此处提供了使用 dnsmasq在 Debian 中进行DNS 缓存的示例。
Configuration summary:
配置摘要:
/etc/default/dnsmasq
/etc/default/dnsmasq
# Ensure you add this line
DNSMASQ_OPTS="-r /etc/resolv.dnsmasq"
/etc/resolv.dnsmasq
/etc/resolv.dnsmasq
# Your preferred servers
nameserver 1.1.1.1
nameserver 8.8.8.8
nameserver 2001:4860:4860::8888
/etc/resolv.conf
/etc/resolv.conf
nameserver 127.0.0.1
Then just restart dnsmasq.
然后只需重新启动 dnsmasq。
Benchmark test using DNS 1.1.1.1:
使用 DNS 1.1.1.1 的基准测试:
for i in {1..100}; do time dig slashdot.org @1.1.1.1; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += } END {print sum / NR}'
Benchmark test using you local cached DNS:
使用本地缓存的 DNS 进行基准测试:
for i in {1..100}; do time dig slashdot.org; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += } END {print sum / NR}'