Setting Up Dnsmasq For Local Web Development Testing On Any Device

2018-08-27 Linux工具

Please note, these instructions are for OS X Lion.

First, you need to get the IP address of your machine on your local network. In OS X, the easiest place to find this is in System Preferences > Network. If you’re using DHCP on your local network, you will want to make sure your computer requests the same IP address when it renews it’s IP address lease. I recommend configuring the DCHP Reservation settings on your router to accomplish this. Otherwise, you can specify a manual address in your network settings:

  1. Go to System Preferences > Network
  2. Click Advanced…
  3. Click the TCP/IP tab
  4. Change the Configure IPv4 dropdown to Using DHCP with manual address
  5. Enter your current IP address in the textbox that appears
  6. Click Ok then Apply

For the purpose of these instructions, let’s assume your local network IP address is 192.168.x.x.

Install dnsmasq

  1. Go to Downloads for Apple Developers and download the most recent release of Command Line Tools for Xcode and install it.
  2. Go to the MacPorts install page, then download and install the Mac OS X Package (.pkg) Installer for Lion.
  3. In Terminal, run
    sudo port -v selfupdate
  4. Install dnsmasq
    sudo port install dnsmasq

Configure dnsmasq

  1. Add the following line to /opt/local/etc/dnsmasq.conf. (Don’t forget to replace the IP address.)
    This will point all hostnames ending with .dev at 192.168.x.x. If you’d prefer a hostname extension other than .dev, you can always edit this later and add additional lines.
  2. sudo cp /etc/resolv.conf /opt/local/etc/

Start dnsmasq automatically on boot

  1. sudo mkdir -p /System/Library/StartupItems/DNSMASQ
  2. Create /System/Library/StartupItems/DNSMASQ/DNSMASQ and fill it with the following:
    . /etc/rc.common
    if [ "${DNSMASQ}" = "-YES-" ]; then
        ConsoleMessage "Starting DNSMASQ"
  1. Create /System/Library/StartupItems/DNSMASQ/Startup\ Parameters.plist and fill it with the following:
    Description = "Local DNSMASQ Server";
    Provides = ("DNS Masq");
    OrderPreference = "None";
    Messages =
        start = "Starting DNSMASQ";
        stop = "Stopping DNSMASQ";
  1. Add the following line to /etc/hostconfig
  2. Make it executable
    sudo chmod +x /System/Library/StartupItems/DNSMASQ/DNSMASQ

Start dnsmasq for the first time

  1. Open a separate Terminal window and run tail -f /var/log/system.log
  2. sudo /System/Library/StartupItems/DNSMASQ/DNSMASQ

You should see dnsmasq starting up in the system.log. Make sure there are no errors.

Add your IP to your DNS settings

  1. Go to System Preferences > Network
  2. Click Advanced…
  3. Click the DNS tab
  4. Add 192.168.x.x (your IP address) to the list and drag it to the top of the list if you can
  5. Click Ok then Apply

You will need to do this on any device you would like to access your .dev hostnames, including VMWare instances and iOS devices.


  1. Clear Cache: dscacheutil -flushcache

  2. Dig a domain: dig
    You should see something like this near the bottom:
    ;; SERVER: 192.168.x.x#53(192.168.x.x)

  3. ping
    You should see something like this:

PING (192.168.x.x): 56 data bytes
64 bytes from 192.168.x.x: icmp_seq=0 ttl=64 time=0.035 ms
64 bytes from 192.168.x.x: icmp_seq=1 ttl=64 time=0.046 ms