前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

dnsmasq

原创
作者头像
zero000
修改2019-06-15 12:01:39
3.2K0
修改2019-06-15 12:01:39
举报
文章被收录于专栏:程序员菜谱程序员菜谱

1. dnsmasq

1.1. dnsmasq简介

dnsmasq支持解决DNS,DHCP和router等多方面问题,一个常见的认知dnsmasq是一个本地dns(/etc/resolv.conf)的加强版

官方地址http://www.thekelleys.org.uk/dnsmasq/doc.html

1.2. dnsmasq使用场景

1.2.1. 域名解析

dnsmasq通过多个对Name Server发起访问,获取最快的DNS解析结果,加快了域名解析的速度

传统的域名解析有2个主要过程:

  1. /etc/hosts,该文件记录本地配置的域名和IP的映射,是域名解析(如ping,http访问)时域名的第一解析方式
  2. /etc/resolv.conf,该文件主要记录DNS的Name Server,对于没在/etc/hosts记录的域名,系统向Name Server发起DNS请求,从而解析域名

resolv.conf方式有些缺点:

  • 如果文件配置多个NS,每次都使用第一个NS优先解析
  • 如果第一个NS没有响应,顺序往下访问,2个NS访问间隔默认为5s
  • 每个DNS请求默认尝试2次
  • 当配置多于3个NS时,后面多出的NS不会被使用

通过man resolv.conf可以查看到相关默认值

代码语言:txt
复制
timeout:n
        sets the amount of time the resolver will wait for  a  response
        from  a remote name server before retrying the query via a dif‐
        ferent name  server.   Measured  in  seconds,  the  default  is
        RES_TIMEOUT  (currently 5, see <resolv.h>).  The value for this
        option is silently capped to 30.
代码语言:txt
复制
attempts:n
       sets the number of times the resolver will send a query to  its
       name  servers  before  giving  up and returning an error to the
       calling application.  The default is RES_DFLRETRY (currently 2,
       see  <resolv.h>).  The value for this option is silently capped
       to 5.

resolv.conf提供可选参数,可以使用rotate随机访问NS,2个NS间隔最短可改为1s,尝试次数最少可改为1次

优化详细方式查看这里

代码语言:txt
复制
options timeout:1 attempts:1 rotate
nameserver 10.0.0.1
nameserver 10.0.0.2
nameserver 10.0.0.3

但显然,优化后的resolv也并非最优方案,这时候可以选用dnsmasq

1.3. dnsmasq优化

1.3.1. 配置优化

开启no-negcache,不缓存故障

he following directive controls whether negative caching should be enabled or not. Negative caching allows dnsmasq to remember “no such domain” answers from the parent nameservers, so it does not query for the same non-existent hostnames again and again. This is probably useful for spam filters or MTA services. By default, negative caching is enabled. To disable, un-comment the following directive.

1.3.2. 监控优化

一般情况下,进程会配置watchdog对进程状态进行监控,通过与crontab结合,定时发现进程不存在的情况并自动拉起进程。

但更深层次考虑的话,dnsmasq有可能一直都不能拉起,这时候就需要考虑降级操作,降级至本地/etc/resolv.conf作DNS解析

dnsmasq_check流程.png
dnsmasq_check流程.png

1.4. FAQ

A1: 对于某些特殊的域名,想使用指定的Name Server,该如何配置

Q1: dnsmasq支持按域名指定NS,可通过以下方式实现

在dnsmasq.conf中添加如下配置:

代码语言:txt
复制
server=/.google.com/8.8.8.8
server=/.google.com/4.4.4.4

或者将配置写入单独的文件,include至dnsmasq.conf

代码语言:txt
复制
# 创建google专用的dnsmasq配置文件
echo -e "server=/.google.com/8.8.8.8\nserver=/.google.com/4.4.4.4" > /etc/dnsmasq.resolv_google.conf

# 在dnsmasq启用该配置
echo "conf-file=/etc/dnsmasq.resolv_google.conf" >> /etc/dnsmasq.conf

而且,使用本地resolv.conf是无法实现不同域名使用不同DNS解析,详看这里

A2: 针对A1的问题,假如我有多个域名需要“劫持”,有什么方式?

Q2: 首先考虑就是使用正则表达式匹配域名,然后指定NS;但官方dnsmasq是不支持正则表达式的方式(2.63版本好像曾经支持),所以如果想使用该功能,需要使用第三方支持正则表达式的dnsmasq

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. dnsmasq
    • 1.1. dnsmasq简介
      • 1.2. dnsmasq使用场景
        • 1.2.1. 域名解析
      • 1.3. dnsmasq优化
        • 1.3.1. 配置优化
        • 1.3.2. 监控优化
      • 1.4. FAQ
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档