LoginSignup
44
50

More than 5 years have passed since last update.

Macにdnsmasqをインストールして開発環境でワイルドカードDNSを使う

Posted at

手順

dnsmasqインストール

brewでインストール

$ brew update
$ brew install dnsmasq
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/dnsmasq-2.72.mavericks.bottle.tar.gz
Already downloaded: /Library/Caches/Homebrew/dnsmasq-2.72.mavericks.bottle.tar.gz
==> Pouring dnsmasq-2.72.mavericks.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 at startup:
    sudo cp -fv /usr/local/opt/dnsmasq/*.plist /Library/LaunchDaemons
    sudo chown root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
Then to load dnsmasq now:
    sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

WARNING: launchctl will fail when run under tmux.
==> Summary
?  /usr/local/Cellar/dnsmasq/2.72: 7 files, 496K

dnsmasq.confをコピー。以下、パスはHomebrewの設定で変わるのかもしれないので、上記Cavertsに従うこと。

$ cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf

launchdに登録するためにスタートアップ設定をシステムにコピー

sudo cp -fv /usr/local/opt/dnsmasq/*.plist /Library/LaunchDaemons
sudo chown root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

LaunchDaemonにロード(サービスを登録)します。

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

dnsmasqの設定

/usr/local/etc/dnsmasq.confに以下の行を追加。*.devを全てローカルホストとして解決する。

address=/dev/127.0.0.1

Macの設定

Macが名前解決にローカルのdnsmasqを使うように設定する。Macには/etc/resolv.confがあるが、このファイルは使われない。代わりに、/etc/resolver/配下に解決したいドメイン名の設定ファイルを記述する。*.devの場合、以下のように設定。

$ vi /etc/resolver/dev
nameserver 127.0.0.1

dnsmasqを起動する

sudo launchctl start homebrew.mxcl.dnsmasq

dnsmasqを終了する

sudo launchctl start homebrew.mxcl.dnsmasq

dnsmasqの自動起動設定を解除する

Homebrewでインストールされるdnsmasqは /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist ファイル内で RunAtLoadtrueに指定されているので、次回起動時から自動的にdnsmasqがstartします。嫌な場合はfalseに書き換えましょう。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>homebrew.mxcl.dnsmasq</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/opt/dnsmasq/sbin/dnsmasq</string>
      <string>--keep-in-foreground</string>
    </array>
    <key>RunAtLoad</key>
    <true/>                  <!--- ← falseに更新 -->
    <key>KeepAlive</key>
    <true/>
  </dict>
</plist>

参考文献

44
50
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
44
50