==> 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.
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
$ 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.
# 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配置文件,会自动生成。
# 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
上面的命令会将/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),要关闭和重启服务的话,可以使用下面的命令:
;; 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