Mac Dnsmasq搭建DNS服务
2016-03-24 18:50
453 查看
转载: https://passingcuriosity.com/2013/dnsmasq-dev-osx/
Most web developers will be familiar with the process of updating your
direct traffic for
Most will also be familiar with the problems of this approach:
it requires a configuration change every time you add or remove a project; and
it requires administration access to make the change.
Installing a local DNS server like Dnsmasq and configuring your system to use that server
can make these configuration changes a thing of the past. In this post, I’ll run through the process of:
Installing Dnsmasq on OS X.
Configuring Dnsmasq to respond to all
with
Configure OS X to send all
requests to Dnsmasq.
Before we get started, I should give you a warning: these instructions show you how to install new system software and change your system configuration. Like all such changes, you should not proceed unless you are confident you have understood
them and that you can reverse the changes if needed.
To quote the Dnsmasq project home page
Dnsmasq is a lightweight, easy to configure DNS forwarder and DHCP server […] is targeted at home networks[.]
There are plenty of ways to install Dnsmasq but my favourite (on OS X) is to use the Homebrew package manager. Installing Homebrew is fairly
simple but beyond my scope here.
Once you have Homebrew installed, using it to install Dnsmasq is easy:
The installation process will output several commands that you can use to start Dnsmasq automatically with a default configuration. I used the following commands but you should use whichever commands
you to:
Now that you have Dnsmasq installed and running, it’s time to configure it! The configuration file lives at
default, so open this file in your favourite editor.
One the many, many things that Dnsmasq can do is compare DNS requests against a database of patterns and use these to determine the correct response. I use this functionality to match any request which ends in
send
Insert this into your
just to keep them all together) and save the file.
You may need to restart Dnsmasq to get it to recognise this change. Restarting Dnsmasq is the same as any other service running under
You can test Dnsmasq by sending it a DNS query using the
in
You should get back a response something like:
Now that you have a working DNS server you can configure your operating system to use it. There are two approaches to this:
Send all DNS queries to Dnsmasq.
Send only
to Dnsmasq.
The first approach is easy – just change your DNS settings in System Preferences – but probably won’t work without additional changes to the Dnsmasq configuration.
The second is a bit more tricky, but not much. Most UNIX-like operating systems have a configuration file called
controls the way DNS queries are performed, including the default server to use for DNS queries (this is the setting that gets set automatically when you connect to a network or change your DNS server/s in System Preferences).
OS X also allows you to configure additional resolvers by creating configuration files in the
This directory probably won’t exist on your system, so your first step should be to create it:
Now you should create a new file in this directory for each resolver you want to configure. Each resolver corresponds – roughly and for our purposes – to a top-level domain like our
There a number of details you can configure for each resolver but I generally only bother with two:
the name of the resolver (which corresponds to the domain name to be resolved); and
the DNS server to be used.
For more information about these files, see the
Create a new file with the same name as your new top-level domain (I’m using
recall) in the
it by running the following commands:
Here
the IP address of the server to use.
Once you’ve created this file, OS X will automatically read it and you’re done!
Testing you new configuration is easy; just use
DNS names in your new top-level domain:
You should see results that mention the IP address in your Dnsmasq configuration like this:
You can now just make up new DNS names under
Maybe the next step on your journey should be learning how to do configure Apache virtual hosts automatically based on host names? If this sounds interesting, contact me using the details below and I’ll write that article too.
关键的步骤:
在DNS,dnsmasq.conf文件中添加:address=/test.com/127.0.0.1
在相应的目录下添加 /etc/resolver/test.com,对应的内容为nameserver 127.0.0.1
sudo cp -fv /usr/local/opt/dnsmasq/*.plist /Library/LaunchDaemons
sudo chown root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
sudo launchctl stop homebrew.mxcl.dnsmasq
sudo launchctl start homebrew.mxcl.dnsmasq
Most web developers will be familiar with the process of updating your
/etc/hostsfile to
direct traffic for
coolproject.devto
127.0.0.1.
Most will also be familiar with the problems of this approach:
it requires a configuration change every time you add or remove a project; and
it requires administration access to make the change.
Installing a local DNS server like Dnsmasq and configuring your system to use that server
can make these configuration changes a thing of the past. In this post, I’ll run through the process of:
Installing Dnsmasq on OS X.
Configuring Dnsmasq to respond to all
.devrequests
with
127.0.0.1.
Configure OS X to send all
.devrequests
requests to Dnsmasq.
Before we get started, I should give you a warning: these instructions show you how to install new system software and change your system configuration. Like all such changes, you should not proceed unless you are confident you have understood
them and that you can reverse the changes if needed.
Installing Dnsmasq
To quote the Dnsmasq project home pageDnsmasq is a lightweight, easy to configure DNS forwarder and DHCP server […] is targeted at home networks[.]
There are plenty of ways to install Dnsmasq but my favourite (on OS X) is to use the Homebrew package manager. Installing Homebrew is fairly
simple but beyond my scope here.
Once you have Homebrew installed, using it to install Dnsmasq is easy:
# Update your homebrew installation brew up # Install dnsmasq brew install dnsmasq
The installation process will output several commands that you can use to start Dnsmasq automatically with a default configuration. I used the following commands but you should use whichever commands
brewtells
you to:
# Copy the default configuration file. cp $(brew list dnsmasq | grep /dnsmasq.conf.example$) /usr/local/etc/dnsmasq.conf # Copy the daemon configuration file into place. sudo cp $(brew list dnsmasq | grep /homebrew.mxcl.dnsmasq.plist$) /Library/LaunchDaemons/ # Start Dnsmasq automatically. sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
Configuring Dnsmasq
Now that you have Dnsmasq installed and running, it’s time to configure it! The configuration file lives at /usr/local/etc/dnsmasq.confby
default, so open this file in your favourite editor.
One the many, many things that Dnsmasq can do is compare DNS requests against a database of patterns and use these to determine the correct response. I use this functionality to match any request which ends in
.devand
send
127.0.0.1in response. The Dnsmasq configuration directive to do this is very simple:
address=/dev/127.0.0.1
Insert this into your
/usr/local/etc/dnsmasq.conffile (I put it near the example
address=/double-click.net/127.0.0.1entry
just to keep them all together) and save the file.
You may need to restart Dnsmasq to get it to recognise this change. Restarting Dnsmasq is the same as any other service running under
launchd:
sudo launchctl stop homebrew.mxcl.dnsmasq sudo launchctl start homebrew.mxcl.dnsmasq
You can test Dnsmasq by sending it a DNS query using the
digutility. Pick a name ending
in
devand use dig to query your new DNS server:
dig testing.testing.one.two.three.dev @127.0.0.1
You should get back a response something like:
;; ANSWER SECTION: testing.testing.one.two.three.dev. 0 IN A 127.0.0.1
Configuring OS X
Now that you have a working DNS server you can configure your operating system to use it. There are two approaches to this:Send all DNS queries to Dnsmasq.
Send only
.devqueries
to Dnsmasq.
The first approach is easy – just change your DNS settings in System Preferences – but probably won’t work without additional changes to the Dnsmasq configuration.
The second is a bit more tricky, but not much. Most UNIX-like operating systems have a configuration file called
/etc/resolv.confwhich
controls the way DNS queries are performed, including the default server to use for DNS queries (this is the setting that gets set automatically when you connect to a network or change your DNS server/s in System Preferences).
OS X also allows you to configure additional resolvers by creating configuration files in the
/etc/resolver/directory.
This directory probably won’t exist on your system, so your first step should be to create it:
sudo mkdir -p /etc/resolver
Now you should create a new file in this directory for each resolver you want to configure. Each resolver corresponds – roughly and for our purposes – to a top-level domain like our
dev.
There a number of details you can configure for each resolver but I generally only bother with two:
the name of the resolver (which corresponds to the domain name to be resolved); and
the DNS server to be used.
For more information about these files, see the
resolver(5)manual page:
man 5 resolver
Create a new file with the same name as your new top-level domain (I’m using
dev,
recall) in the
/etc/resolver/directory and add a
nameserverto
it by running the following commands:
sudo tee /etc/resolver/dev >/dev/null <<EOF nameserver 127.0.0.1 EOF
Here
devis the top-level domain name that I’ve configured Dnsmasq to respond to and
127.0.0.1is
the IP address of the server to use.
Once you’ve created this file, OS X will automatically read it and you’re done!
Testing
Testing you new configuration is easy; just use pingcheck that you can now resolve some
DNS names in your new top-level domain:
# Make sure you haven't broken your DNS. ping -c 1 www.google.com # Check that .dev names work ping -c 1 this.is.a.test.dev ping -c 1 iam.the.walrus.dev
You should see results that mention the IP address in your Dnsmasq configuration like this:
PING iam.the.walrus.dev (127.0.0.1): 56 data bytes
You can now just make up new DNS names under
.devwhenever you please. Congratulations!
Maybe the next step on your journey should be learning how to do configure Apache virtual hosts automatically based on host names? If this sounds interesting, contact me using the details below and I’ll write that article too.
关键的步骤:
在DNS,dnsmasq.conf文件中添加:address=/test.com/127.0.0.1
在相应的目录下添加 /etc/resolver/test.com,对应的内容为nameserver 127.0.0.1
sudo cp -fv /usr/local/opt/dnsmasq/*.plist /Library/LaunchDaemons
sudo chown root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
sudo launchctl stop homebrew.mxcl.dnsmasq
sudo launchctl start homebrew.mxcl.dnsmasq
相关文章推荐
- CGLib与JDK的动态代理
- LeetCode: Longest Consecutive Sequence [128]
- 两个viewport的故事(第二部分)
- log4j的配置
- JS去掉文字中的html代码段
- 两个viewport的故事(第一部分)
- jQuery easyui 和 ueditor结合的问题
- 不太完美的四则运算
- 再谈网游同步技术:实时动作游戏同步方式和传输协议选择
- linux系统目录介绍
- KMP算法学习笔记(二)
- Windows下FFmpeg快速入门
- android https协议遇到的问题
- CSS 元素垂直居中的 6种方法
- 【手游】梦幻西游手游 美术资源加密分析
- C++动态内存分配
- c++作业2
- linux wait 和waitpid
- Android系统手机端抓包方法
- 保存视频文件到相册视频的时长为0