神代綺凛

[DNSmasq] 安装&配置详解
DNSmasq 是 Linux 下的一个用于配置 DNS 和 DHCP 的工具,个人认为最主要的功能还是用来做 D...
扫描右侧二维码阅读全文
06
2017/11

[DNSmasq] 安装&配置详解

DNSmasq 是 Linux 下的一个用于配置 DNS 和 DHCP 的工具,个人认为最主要的功能还是用来做 DNS 服务器。一句话,好用的不行。

Head Pic: 「【PFⅡ】ヘッツェル」/「たはるコウスケ」のイラスト [pixiv]

DNSmasq

安装

Ubuntu/Debian 等

无需多说,直接

apt-get install dnsmasq

CentOS

由于 CentOS 的 yum 安装的 DNSmasq 版本比较旧,因此我们可以先下载最新版编译好,用 yum 安装完之后再替换掉程序。

先从 http://www.thekelleys.org.uk/dnsmasq/ 找到你想要的 DNSmasq 版本的源码压缩包的下载地址,例如我选择的是我写这篇文章时的最新版dnsmasq-2.78.tar.gz

下载对应的源码压缩包并解压

wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.78.tar.gz
tar xf dnsmasq-2.78.tar.gz

编译,源码 make 不带 etc 等

cd dnsmasq-2.78
make V=s

然后用 yum 安装 DNSmasq

yum install dnsmasq

yum 安装的是2.4.8版本,然后我们再用我们编译好的最新版本替换

yes | cp -f ./src/dnsmasq /usr/sbin/dnsmasq

这时可以查看 DNSmasq 的版本检验一下

dnsmasq -v

看一下版本号,应该就是我们编译好的版本了

配置

DNSmasq 的配置文件位于/etc/dnsmasq.conf,下面是 DNSmasq 中和 DNS 相关的配置项的说明。

此处的配置仅为说明,请勿照抄配置
# 用指定的端口代替默认的DNS 53端口,如果设置为0,则完全禁止DNS功能,只使用dhcp服务
port=5353

# 以下两个参数告诉Dnsmasq过滤一些查询:1.哪些公共DNS没有回答 2.哪些root根域不可达。
# 从不转发格式错误的域名
domain-needed
# 从不转发不在路由地址中的域名
bogus-priv

# resolv-file配置Dnsmasq额外的向流的DNS服务器,通过下面的选项指定其他文件。
# 如果不开启就使用linux主机默认的/etc/resolv.conf里的nameserver。
resolv-file=/etc/dnsmasq.d/upstream_dns.conf

# 默认情况下Dnsmasq会发送查询到它的任何上游DNS服务器上,如果取消注释,
# 则Dnsmasq则会严格按照/etc/resolv.conf中的DNS Server顺序进行查询。
strict-order

# 以下两个参数控制是否通过/etc/resolv.conf确定上游服务器,是否检测/etc/resolv.conf的变化。
# 如果你不想Dnsmasq读取/etc/resolv.conf文件或者其他文件,获得它的servers,则取消注释。
no-resolv
# 如果你不允许Dnsmasq通过轮询/etc/resolv.conf或者其他文件来获取配置的改变,则取消注释。
no-poll

# 增加一个name server,一般用于内网域名
server=/localnet/192.168.0.1

# 设置一个反向解析,所有192.168.3.0/24的地址都到10.1.2.3去解析
server=/3.168.192.in-addr.arpa/10.1.2.3

# 增加一个本地域名,会在/etc/hosts中进行查询
local=/localnet/

# 增加一个域名,强制解析到你指定的地址上
address=/double-click.net/127.0.0.1

# 同上,还支持ipv6
address=/www.thekelleys.org.uk/fe80::20d:60ff:fe36:f83

# 增加查询yahoo google和它们的子域名到vpn、search查找
ipset=/yahoo.com/google.com/vpn,search

# 你还可以控制Dnsmasq和Server之间的查询从哪个网卡出去
server=10.1.2.3@eth1

# 指定源地址携带10.1.2.3地址和192.168.1.1的55端口进行通讯
server=10.1.2.3@192.168.1.1#55

# 改变Dnsmasq默认的uid和gid
user=
group=

# 如果你想Dnsmasq监听某个端口为dhcp、dns提供服务
interface=

# 你还可以指定哪个端口你不想监听
except-interface=

# 设置想监听的地址,如果你本机要使用写上127.0.0.1。
listen-address=

# 如果你想在某个端口只提供dns服务,则可以进行配置禁止dhcp服务
no-dhcp-interface=

# On systems which support it, dnsmasq binds the wildcard address,
# even when it is listening on only some interfaces. It then discards
# requests that it shouldn't reply to. This has the advantage of
# working even when interfaces come and go and change address. If you
# want dnsmasq to really bind only the interfaces it is listening on,
# uncomment this option. About the only time you may need this is when
# running another nameserver on the same machine.
bind-interfaces

# 如果你不想使用/etc/hosts,则取消下面的注释
no-hosts

# 如果你项读取其他类似/etc/hosts文件,则进行配置
addn-hosts=/etc/banner_add_hosts

# 自动的给hosts中的name增加一个域名
expand-hosts

# 给dhcp服务赋予一个域名
domain=thekelleys.org.uk

# 给dhcp的一个子域赋予一个不同的域名
domain=wireless.thekelleys.org.uk,192.168.2.0/24

# 同上,不过子域是一个范围
domain=reserved.thekelleys.org.uk,192.68.3.100,192.168.3.200

# dhcp分发ip的范围,以及每个ip的租约时间
dhcp-range=192.168.0.50,192.168.0.150,12h

# 同上,不过给出了掩码
dhcp-range=192.168.0.50,192.168.0.150,255.255.255.0,12h

# 自动加载conf-dir目录下的配置文件
conf-dir=/etc/dnsmasq.d

# 设置dns缓存大小,默认为150条
cache-size=150

修改完配置之后需要重启 DNSmasq 以使配置生效

service dnsmasq start

配置过程中需要注意的几点

把你的服务器作为一个 DNS 服务器

  1. 配置中的listen-address必须为你服务器的公网 IP
    listen-address=你的公网IP
  2. 在防火墙中开放 53 端口
    iptables -A INPUT -p udp -m udp --dport 53 -j ACCEPT
    iptables -A INPUT -p tcp -m tcp --dport 53 -j ACCEPT
  3. 如果是 CentOS 的话需要保存并重启防火墙使以上改动生效
    service iptables save
    service iptables restart

如果想强制重定向本地的 DNS 请求给 DNSmasq

eth0为网卡名,请替换为你自己机器的网卡名

iptables -t nat -A PREROUTING -i eth0 -p upd --dport 53 -j REDIRECT --to-port 53

然后重启防火墙(如果是 CentOS)

如果想自定义上游 DNS

no-resolv
no-poll
server=x.x.x.x#端口

不写#端口默认是53

使用上的一些建议

如果需要自定义上游 DNS 服务器,以及设置特定域名的 Host 之类的,建议写到一个单独的.conf配置文件里,然后放到一个专门的文件夹,然后修改 DNSmasq 配置中的这条

conf-dir=配置文件夹

这样的话以后修改配置就不需要频繁修改主配置文件,而且也有利于配置的分块

dnsmasq-china-list

dnsmasq-china-list项目维护了一张国内常用但是通过国外DNS会解析错误的网站域名的列表,保证List中的国内域名全部走国内DNS服务器解析。

项目地址: https://github.com/felixonmars/dnsmasq-china-list

使用

取消配置文件中conf-dir=/etc/dnsmasq.d这一行的注释

如果此处你是用的是自己的配置文件夹,那么久在后续步骤中将配置文件放入你自己的配置文件夹即可

随便找一个地方将项目文件下载下来

git clone https://github.com/felixonmars/dnsmasq-china-list.git

然后将其中的accelerated-domains.china.confbogus-nxdomain.china.confgoogle.china.conf(可选)放到/etc/dnsmasq.d/目录中
最后重启 DNSmasq 即可

参考文章

CentOS下yum安装dnsmasq,并强制替换为最新版
利用Dnsmasq部署DNS服务

搬瓦工VPS优惠套餐,建站稳如狗,支持支付宝,循环出账94折优惠码BWH3HYATVBJW
年付$47CN2线路,1核/1G内存/20G硬盘/1T@1Gbps【点击购买
季付$47CN2 GIA线路,1核/1G内存/20G硬盘/1T@2.5Gbps【点击购买
Last modification:April 22nd, 2018 at 04:37 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment Cancel reply

2 comments

  1. 萌白酱  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 78.0.3904.108(Google Chrome 78.0.3904.108)
    第一:我想购买你的在本站推荐的VPS,在VPS上搭建blog和websocket学习test用。但是我发现每个地区不同的运营商访问VPS的speed都不一样。我想用京东云解析DNS、DNSPod、DNS.LA这三家支持分线路解析DNS,当发现是移动用户就走104.28.14.0/24 cloudfare CDN ,联通用户走新加坡 104.20.157.0/24 cloudfare CDN ,电信用户走日本172.64.0.0/24 cloudfare CDN,请问这想法能实现吗?
    如果想法能实现,移动用户走104.28.14.6 cloudfare CDN,如果104.28.14.6 节点高负荷,用什么tools可以在指定的104.28.14.0/24 IP段找一个低负荷的CDN节点加速?
  2. 陈某  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 74.0.3729.169(Google Chrome 74.0.3729.169)
    想dnsmasq去弄vps的流媒体分区域的流媒体解锁,然后就看到你这篇了