Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setup dnsmasq on OS X

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

@hackzilla

This comment has been minimized.

hackzilla commented Aug 19, 2013

isn't the step "Create config directory" irrelevant?

surely brew creates this as part of the install.

@ogrrd

This comment has been minimized.

Owner

ogrrd commented Aug 19, 2013

@hackzilla Let me know if it does and I can update the gist. It works with the step in so I haven't tried it without, but I get what you're saying.

@hackzilla

This comment has been minimized.

hackzilla commented Aug 19, 2013

apparently it doesn't

@tejasmanohar

This comment has been minimized.

tejasmanohar commented May 22, 2014

Would you translate/convert sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons for MacPorts users? Is this a one-time run or per reboot?

@PacoH

This comment has been minimized.

PacoH commented May 25, 2016

Hi. Although this MAY be useful, it isn't very helpful if you don't explain how each command works explicitly. Just blindly copying and pasting commands like this, without knowing what each one will do, is never a good idea.

BTW on my machine, /usr/local/etc/ already exists.

@zl3ag

This comment has been minimized.

zl3ag commented Sep 19, 2016

Also, be aware that .dev exists as a "real" TLD in the ICANN root, so you should probably come up with a different TLD

https://newgtlds.icann.org/en/program-status/delegated-strings

@m-miller

This comment has been minimized.

m-miller commented Jun 16, 2017

Will this override any hosts entries already in place or VM defined hosts?

@tobybot

This comment has been minimized.

tobybot commented Jun 28, 2017

to follow up the comment from September: .dev does exist, and anyone reading this far down should change to use .test, which is reserved by icann for just such purposes.

also i don't believe this will override host entries in place but i could be wrong about that!

@jianping-roth

This comment has been minimized.

jianping-roth commented Jul 24, 2017

I use macOS Sierra, I have to remove strict-order from my configuration in order for srv-host to work.

@jntme

This comment has been minimized.

jntme commented Jul 28, 2017

Fell too for the .dev TLD trap. Plase, change this in your gist.

@c835722

This comment has been minimized.

c835722 commented Aug 19, 2017

Having .dev in TLD also preventev my docker-machine env dev from functioning.

@eugeneware

This comment has been minimized.

eugeneware commented Aug 26, 2017

Remember to NOT delete your old DNS servers from MacOS network preferences. Add 127.0.0.1 to the list and move it to the top. When you hit the + button it deletes the DNS entries by default from the list, so note them down, and add them back in, but add 127.0.0.1 to the top.

@eugeneware

This comment has been minimized.

eugeneware commented Aug 26, 2017

Also, if the DNS entries you're trying to create don't actually exist, then you don't need to add 127.0.0.1 to your network preferences DNS order. If your domain suffix is blah, make sure that you add the nameserver 127.0.0.1 to /etc/resolver/blah for the OS X resolver fallback to work.

See this for more info.

@matteocng

This comment has been minimized.

matteocng commented Sep 25, 2017

Thanks for this. As previously suggested, it is not recommended to use .dev, see dont use dev for development for additional info and recommendations (tl:dr .localhost, .invalid, .test, .example are discussed).

@johnny77221

This comment has been minimized.

johnny77221 commented Sep 26, 2017

I couldn't get it work when adding it to LaunchDaemons, even add an execution as user setting
so I'll have to manually run sudo /usr/local/opt/dnsmasq/sbin/dnsmasq --keep-in-foreground after login and it works well
(I am using mac Sierra 10.12.6)

@Bahir

This comment has been minimized.

Bahir commented Oct 6, 2017

I followed the setup doing my local dnsmasq network listening on Ethernet interface (10.0.0.11).
But after reboot dnsmasq is on the list of running provesses but I have to restart it manually each time I boot up the system.
After that it works.
I have no idea why it is but I have an idea that dnsmasq is runned before Ethernet going up.
That resolves why after restart the service helps.

@kinjal

This comment has been minimized.

kinjal commented Dec 13, 2017

https://stackoverflow.com/a/47744259/6629

from chrome 63+, domain names ending in .DEV get forcibly redirected to https://. It is recommended to use .TEST as the TLD for local domains.

@mcurren

This comment has been minimized.

mcurren commented Dec 14, 2017

^^ correct

@roelal

This comment has been minimized.

roelal commented Dec 19, 2017

Instead of altering the LaunchDaemons manually, you can use the convenient brew services.

@joneslee85

This comment has been minimized.

joneslee85 commented Jan 4, 2018

FYI Google has claimed the .dev domain, I now use .test instead

@tishma

This comment has been minimized.

tishma commented Apr 18, 2018

I've had this successfully working a few years ago on yosemite, but recently I realized it fails on sierra.

manually running the dnsmasq binary (instead of loading plist daemon file) helped as it did show the reason why it failed.

brew installation doesn't seem to create /usr/local/var/run/dnsmasq/ dir required for the daemon to run. after creating this dir - it works again.

@g4macgregor

This comment has been minimized.

g4macgregor commented Aug 28, 2018

I have set everything up accordingly, and have the /usr/local/var/run/dnsmasq/dnsmasq.pid with nobody:nobody. Should this be the same user:group as httpd is running?

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