跳至主要内容

在macOS下用dnsmasq搭建DNS服务器

原文链接
参考链接

一提起搭建DNS服务器,你可能会想到Bind这些Linux下的DNS工具,就像 Oracle Linux下内网DNS服务器的配置 里的那样,配置Bind,编写正解文件,再编写反解文件。步骤比较繁琐,尤其是正解、反解文件的格式很反人类,容易出错。
其实,除了Bind这种比较复杂的DNS服务之外,Linux系统中还提供了一种更轻量级的DNS服务,也就是本文的主角dnsmasq。它比较适合本地的小型网络,尤其是在虚拟、测试环境中为各个虚拟机提供DNS和DHCP服务。
在macOS中,通过homebrew安装,也可以使用dnsmasq。本文的目的就是将macOS作为一个虚拟局域网(192.168.78.)的DNS服务器(本机的IP地址为192.168.78.1),为本机以及各个虚拟主机提供局域网的DNS服务。接下来就是简单的配置步骤。

安装dnsmasq

如果你的macOS中没有安装homebrew的话,用下面的命令1
1
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
之后就可以用homebrew进行安装了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ brew install dnsmasq
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
gnupg
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/dnsmasq-2.7
######################################################################## 100.0%
==> Pouring dnsmasq-2.76.sierra.bottle.tar.gz
==> Caveats
To configure dnsmasq, copy the example configuration to /usr/local/etc/dnsmasq.conf
and edit to taste.
cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf
To have launchd start dnsmasq now and restart at startup:
sudo brew services start dnsmasq
==> Summary
🍺 /usr/local/Cellar/dnsmasq/2.76: 7 files, 504.7KB

配置dnsmasq

上面的安装提示信息中说的已经很明白了,需要把一个示例用的配置文件复制到/usr/local/etc/dnsmasq.conf来进行配置。什么?没注意看上面的信息,好吧,用下面的命令再看一次:
1
2
3
4
5
6
7
8
9
10
11
12
$ brew info dnsmasq
dnsmasq: stable 2.76 (bottled)
Lightweight DNS forwarder and DHCP server
http://www.thekelleys.org.uk/dnsmasq/doc.html
...
To configure dnsmasq, copy the example configuration to /usr/local/etc/dnsmasq.conf
and edit to taste.
cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf
To have launchd start dnsmasq now and restart at startup:
sudo brew services start dnsmasq
照做就行:
1
$ cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf
修改配置文件/usr/local/etc/dnsmasq.conf。刚才拷贝过来的文件中,默认所有的选项都是注释掉的,只需要修改下面两个选项:
1
2
3
4
5
6
7
8
9
10
# By default, dnsmasq will send queries to any of the upstream
# servers it knows about and tries to favour servers to are known
# to be up. Uncommenting this forces dnsmasq to try each query
# with each server strictly in the order they appear in
# /etc/resolv.conf
strict-order
<此处省略好多字...>
# Or which to listen on by address (remember to include 127.0.0.1 if
# you use this.)
listen-address=192.168.78.1,127.0.0.1
解释一下:
  • strict-order 表示在解析一个域名时,dnsmasq会严格按照/etc/resolv.conf文件中定义的DNS服务器的顺序从上到下进行 DNS 解析,直到第一个解析成功为止。/etc/resolv.conf是macOS默认的DNS配置文件,会自动生成。
  • listen-address 表示DNS服务会绑定到哪个地址上。如果只是本机使用,那么只需要指定127.0.0.1就可。如果还需要让局域网中的其他主机也能使用这个DNS服务器,还需要加上本机在局域网中的地址,即192.168.78.1,127.0.0.1。换句话说,在192.168.78.这个网段的所有主机都可以指定192.168.78.1为自己的DNS服务器地址。
到此为止,dnsmasq的配置文件就编辑完了。
那么问题来了:局域网中主机地址与域名的对应关系在哪里配置呢?
答:在/etc/hosts文件中进行配置。
原来,在默认情况下,dnsmasq在解析一个域名时,会首先查找/etc/hosts文件中的定义,如果找不到的话,再去/etc/resolv.conf中去找。当然,这个动作也是可以配置的,在配置文件中说明如下:
1
2
3
4
5
6
# If you don't want dnsmasq to read /etc/hosts, uncomment the
# following line.
#no-hosts
# or if you want it to read another file, as well as /etc/hosts, use
# this.
#addn-hosts=/etc/banner_add_hosts

配置本地局域网中的域名和主机映射

这里要按照实际的需要来定义,本文环境中的/etc/hosts文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
$ sudo vim /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost, KMAC.local
255.255.255.255 broadcasthost
::1 localhost
192.168.78.122 ora12c, ora12c.example.com
192.168.78.73 rhel7, rhel7.example.com
192.168.78.113 sol11, sol11.example.com

启动dnsmasq服务

按照brew info dnsmasq的提示,输入下面命令就可以启动服务,并且会开机自动启动:
1
2
3
$ sudo brew services start dnsmasq
Password:
==> Successfully started `dnsmasq` (label: homebrew.mxcl.dnsmasq)
上面的命令会将/usr/local/opt/dnsmasq/homebrew.mxcl.dnsmasq.plist自动复制为/Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist,这就是dnsmasq服务描述文件。这里多说几句,macOS用launchd(类似于Linux的systemd,或者init)来启动后台服务,LaunchDaemon会在开机时自动启动。启动时读入的就是这些”.plist”文件(Property List File),要关闭和重启服务的话,可以使用下面的命令:
1
2
$ sudo launchctl stop homebrew.mxcl.dnsmasq
$ sudo launchctl start homebrew.mxcl.dnsmasq

测试

先看看外部域名能不能正常解析到:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ dig baidu.com
; <<>> DiG 9.8.3-P1 <<>> baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64468
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;baidu.com. IN A
;; ANSWER SECTION:
baidu.com. 30 IN A 123.125.114.144
baidu.com. 30 IN A 220.181.57.217
baidu.com. 30 IN A 180.149.132.47
baidu.com. 30 IN A 111.13.101.208
;; Query time: 9 msec
;; SERVER: 10.0.31.199#53(10.0.31.199)
;; WHEN: Tue May 16 11:40:52 2017
;; MSG SIZE rcvd: 91
没有问题,再看看本地局域网域名的解析情况如何(这一步最好到从其他主机上进行测试,本机测试意义不大,因为/etc/hosts总是会解析成功的):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ dig rhel7
; <<>> DiG 9.8.3-P1 <<>> rhel7
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 30445
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;rhel7. IN A
;; Query time: 1 msec
;; SERVER: 10.0.31.199#53(10.0.31.199)
;; WHEN: Tue May 16 11:42:21 2017
;; MSG SIZE rcvd: 23

总结

一共五条命令即可搞定:
1
2
3
4
5
6
7
8
9
10
11
12
$ brew install dnsmasq
$ cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf
$ cat >> /usr/local/etc/dnsmasq.conf << EOF
strict-order
listen-address=192.168.78.1,127.0.0.1
EOF
$ sudo cat >> /etc/hosts << EOF
192.168.78.122 ora12c, ora12c.example.com
192.168.78.73 rhel7, rhel7.example.com
192.168.78.113 sol11, sol11.example.com
EOF
$ sudo brew services start dnsmasq
其实,关于dnsmasq网上有一大堆的教程,本文只用了最简单的步骤,使用dnsmasq的默认配置,保证在最短的时间内让DNS服务器跑起来。如果你感兴趣的话,可以仔细研究一下dnsmasq的配置文件,注释写的很详细,可以尝试配置一下DHCP服务。

清除DNS缓存
修改本地hosts后,因为DNS有缓存策略,所以一般还需要再强制刷新下DNS缓存才行,这里要注意,随着OSX版本的不同,这个命令也是有所不同,这里只针对10.10.4及以后的版本而言:
sudo killall -HUP mDNSResponder  

评论

此博客中的热门博文

Resolving errSecInternalComponent errors during code signing

原文链接 One code signing issue I commonly see, both here on DevForums and in my Day Job™ with DTS, is that the codesign command fails with errSecInternalComponent. This issue crops up in a wide variety of circumstances and the correct fix depends on the specific problem. This post is my attempt to clarify the potential causes of this error and help folks resolve it. If you have any questions or comments about this, please start a new thread, tagging it with Code Signing so that I see it. Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com" Resolving errSecInternalComponent errors during code signing In some circumstances the codesign command might fail with the error errSecInternalComponent. For example: % codesign -s "Apple Development" "MyTrue" MyTrue: errSecInternalComponent This typically affects folks who are signing code in a nonstandard environm...

iOS:检测使用VPN或Proxy

参考链接: https://www.jianshu.com/p/c3b950dbf86a https://gist.github.com/PramodJoshi/4faad4c91f7dcb4eb9b06be8390c01db http://noodlecode.net/2018/04/check-if-ios-app-is-connected-to-vpn 第一种方法 需要导入框架CFNetwork 然后,这个方法是mrc的:需要添加-fno-objc-arc的flag 代码如下: + ( BOOL )getProxyStatus { NSDictionary *proxySettings = NSMakeCollectable ([( NSDictionary *) CFNetworkCopySystemProxySettings () autorelease]); NSArray *proxies = NSMakeCollectable ([( NSArray *) CFNetworkCopyProxiesForURL (( CFURLRef )[ NSURL URLWithString: @"http://www.google.com" ], ( CFDictionaryRef )proxySettings) autorelease]); NSDictionary *settings = [proxies objectAtIndex: 0 ]; NSLog ( @"host=%@" , [settings objectForKey:( NSString *)kCFProxyHostNameKey]); NSLog ( @"port=%@" , [settings objectForKey:( NSString *)kCFProxyPortNumberKey]); NSLog ( @"type=%@" , [settings objectForKey:( NSString *)kCFProxyTypeKey]); if ([[settings object...

去广告DNS设置,国内ADGuard DNS方案,手机电脑iOS去广告,保护隐私

 原文链接 之前分享过使用mac系统搭建adguard home,这几个月用下来零零散散基本上也被弃用了。主要原因是因为需要保持电脑一直开机。但是我的电脑是笔记本,存在移动各个地域的情况,也就是说只能够屏蔽电脑自身,对于手机而言不太现实。今天偶然发现dnspod推出了高级版的公共解析。dnspod背靠腾讯云,肯定是合法合规的公共解析服务,这个高级版用起来不错。 国内自己搭建解析服务是违法行为,所以这也是为什么使用dnspod的原因。 后台截图 开始使用 首先我们先进入dnspod的公共解析页面,点击开始使用。 专业版公共解析 dnspod会提供几种预设,我们选择「开发者」即可 开发者 然后你就成功的申请到自己个人使用的dns了! 更新拦截规则 我们可以将常见的广告过滤规则加入到dns中。我们在顶部选项卡中选择「拦截规则」。 拦截规则设置 打开adguard adguard 绑定iOS设备 推荐使用描述文件的方式,删除配置时删除描述文件即可。 描述文件 绑定macOS 推荐使用描述文件的方式,删除配置时删除描述文件即可。 描述文件 mac需要在「系统偏好设置」的「网络」中查看是否正在运行。 代理 如果没有运行需要点击「···」来启动服务。 启动服务 绑定路由器 找到自己路由器的DHCP设置,修改dns,然后记得绑定自己的ip。 修改dns 绑定ip 费用 目前有300万次/月的免费额度,但没有超出之后的价格。300万次一个人比较难用完,可以放心使用。 我个人使用iOS设备两台、智能家居、电脑两台,日均请求数大致2万/日。 判断是否搭建成功 可以通过查看日志的方式,日志大概有半小时到一小时的延迟,请耐心等待。