Skip to content

Instantly share code, notes, and snippets.

@xhkyyy
Forked from ogrrd/dnsmasq OS X.md
Last active January 14, 2020 07:24
Show Gist options
  • Save xhkyyy/f86682aecba5a8abe611e1562377f1ca to your computer and use it in GitHub Desktop.
Save xhkyyy/f86682aecba5a8abe611e1562377f1ca to your computer and use it in GitHub Desktop.
mac 安装 dnsmasq

详细问题见文档末尾

Never touch your local /etc/hosts file in OS X again

NOTE this has been done properly by a guy here: https://www.stevenrombauts.be/2018/01/use-dnsmasq-instead-of-etc-hosts/

You should go and do that now, ignore all of this.

To setup your computer to work with *.dev domains, e.g. project.dev, awesome.dev and so on, without having to add to your hosts file each time.

Requirements

Install

brew install dnsmasq

Setup

Create config directory

mkdir -pv $(brew --prefix)/etc/

Setup *.dev

echo 'address=/.dev/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf

You should probably add strict-order to dnsmasq.conf to keep nameserver order of resolv.conf (see here).

Autostart

Work after reboot

sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons

Get it going right now

sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

Add to resolvers

Create resolver directory

sudo mkdir -v /etc/resolver

Add your nameserver to resolvers

sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'

Add local DNS to search order in System Preferences

System Preferences > Network > Wi-Fi (or whatever you use) > Advanced... > DNS > add 127.0.0.1 to top of the list.

Finished

That's it! You can run scutil --dns to show all of your current resolvers, and you should see that all requests for a domain ending in .dev will go to the DNS server at 127.0.0.1

防止 service 启动失败,创建 dnsmasq.d 文件夹

mkdir -p $(brew --prefix)/etc/dnsmasq.d

调试

使用 mac 的 console.app 查看调试日志

重启 dns 服务

sudo brew services restart dnsmasq

列出所有的 brew service brew services list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment