侧边栏壁纸
博主头像
xuesheng博主等级

分享web知识,学习就是取悦自己!

  • 累计撰写 118 篇文章
  • 累计创建 14 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

macOS 下用 dnsmasq 搭建 DNS 服务器

xuesheng
2023-11-01 / 0 评论 / 0 点赞 / 160 阅读 / 1,671 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-11-01,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

我们在有些场景需要自己的 DNS 服务,比如:移动端设备开发过程中,如果需要解析自定义域名,由于无法修改 hosts 文件,所以可以采用自建简单的 DNS 服务来实现域名解析,比较方便。

一、安装

使用 Homebrew 进行安装:

brew install dnsmasq

安装成功后会提示使用信息。也可以用如下命令重新查看提示:

brew info dnsmasq

可以根据提示中的信息来查看启动服务的方式和配置文件的位置。

参考:《Dnsmasq 介绍》

本文以 dnsmasq: stable 2.86 为例进行说明。

重启服务:

sudo brew services restart dnsmasq

如果不需要后台服务,可以这样运行:

sudo /usr/local/opt/dnsmasq/sbin/dnsmasq \
    -k \
    -C /usr/local/etc/dnsmasq.conf \
    -7 /usr/local/etc/dnsmasq.d,*.conf

参数说明:

-C, --conf-file=<path> ........... 指定配置文件(默认为 /usr/local/etc/dnsmasq.conf)。
-k, --keep-in-foreground ......... 不进入后台,不在调试模式下运行。
-7, --conf-dir=<path> ............ 从此目录中的所有文件中读取配置。

二、配置文件

/usr/local/etc/dnsmasq.conf

用指定的端口代替默认的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
# If you don't want dnsmasq to read /etc/resolv.conf or any other
# file, getting its servers from this file instead (see below), then
# uncomment this.
#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查找
# Add the IPs of all queries to yahoo.com, google.com, and their
# subdomains to the vpn and search ipsets:
#ipset=/yahoo.com/google.com/vpn,search

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

指定源地址携带10.1.2.3地址和192.168.1.1的55端口进行通讯
# and this sets the source (ie local) address used to talk to
# 10.1.2.3 to 192.168.1.1 port 55 (there must be a interface with that
# IP on the machine, obviously).
# 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

使用上面的配置,按需调整如下:

listen-address=0.0.0.0
address=/hello.com/192.168.1.10

启动服务:

sudo brew services start dnsmasq

三、Docker 方式安装

有人制作了 dnsmasqDocker 镜像,可以在docker-hub中搜索dnsmasq 查看。

amd:https://hub.docker.com/r/jpillora/dnsmasq

arm:https://hub.docker.com/r/sirscythe/dnsmasq-arm

1、优势

相比起来 Docker 方式有一些优势:

更简洁干净,即用即抛
对于简单使用的场景,无需考虑各种命令行参数,启停服务都更方便
dnsmasq 不支持 Windows,通过 Docker 方式可以实现对 Windows 的支持
这个镜像还提供了Web配置界面

2、使用

启动服务:

$ docker run \
    --name dnsmasq \
    -d \
    -p 53:53/udp \
    -p 5380:8080 \
    -v /your/config/file:/etc/dnsmasq.conf \
    --log-opt "max-size=100m" \
    -e "HTTP_USER=foo" \
    -e "HTTP_PASS=bar" \
    --restart always \
    jpillora/dnsmasq

说明:

  • 使用 /your/config/file 作为配置文件
  • UDP53 端口提供 DNS 服务
  • 5380 端口提供 Web 管理端界面
  • 用户名 foo,密码 bar

3、Web 管理端

访问 http://<docker-host>:5380, 使用 foo/bar 进行身份验证,会看到:

4、测试

host <需要解析的域名> <提供DNS服务的主机>

四、参考资料

https://www.jianshu.com/p/64466fa557b3

https://www.hi-linux.com/posts/30947.html

0
博主关闭了所有页面的评论