- 优先使用本地自定义dns和host,可以针对不同的网站使用不同的DNS,以提高网页打开速度。
- 提供dhcp服务,方便内网主机和移动设备管理。
1. YUM安装方法
非常方便,但版本比较低,目前只能安装dnsmasq x86_64 2.48-13.el6,但官方最新版已到2.71
yum install dnsmasq
2. 编译安装dnsmasq
cd /tmp && wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.71.tar.gz
tar -zxvf dnsmasq-2.71.tar.gz && cd dnsmasq-2.71
make install
#也很容易,安装完成
cp dnsmasq.conf.example /etc/dnsmasq.conf
mkdir -p /etc/dnsmasq.d #这个目录备用
3. dnsmasq配置
主要有三个文件:
/etc/dnsmasq.conf
/etc/dnsmasq.d/resolv.dnsmasq.conf
/etc/dnsmasq.d/dnsmasq.hosts
第一个是系统默认必须的,后面两个可以自行建立,放置的路径也可以根据自己需要定义。
vi /etc/dnsmasq.conf
自带的配置文件有很多说明,可以直接在上部加入以下内容,保留之前的可以当帮助文件用,也可直接删除原先的内容。
#ITGeeker每次开启都提示错误,目的是让dnsmasq读取目录内所有配置文件
#conf-dir=/etc/dnsmasq.d
#让dnsmasq读取你设定的resolv-file
#no-resolv
resolv-file=/etc/dnsmasq.d/resolv.dnsmasq.conf
no-poll
strict-order
#不读取系统hosts,读取你设定的
no-hosts
addn-hosts=/etc/dnsmasq.d/dnsmasq.hosts
#dnsmasq日志设置
log-queries
log-facility=/var/log/dnsmasq.log
#dnsmasq缓存设置
cache-size=1024
#单设置127只为本机使用,加入本机IP为内部全网使用
listen-address=127.0.0.1,192.168.188.199
在/etc/dnsmasq.d目录下新建2个文件
vi /etc/dnsmasq.d/resolv.dnsmasq.conf
国内快的DNS也就这些了,谷歌的8.8.8.8这两天有点慢,甚至ping不同,可能和香港和越南的光纤断裂有关,所以暂时注销了。
#nameserver 127.0.0.1 不应该添加
nameserver 202.96.209.5
nameserver 202.96.209.133
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 114.114.114.114
nameserver 8.8.4.4
#nameserver 8.8.8.8
vi /etc/dnsmasq.d/dnsmasq.hosts
给出以下样本,ITGeeker建议自己制作一个最新好用的。
173.194.120.88 0.docs.google.com
216.239.32.39 0.docs.google.com
173.194.120.88 0.drive.google.com
216.239.32.39 0.drive.google.com
......
dnsmasq启动脚本
编译安装比较麻烦的就是这件事了,当然也可直接手动操作:
启动: /usr/local/sbin/dnsmasq
验证:netstat -tunlp|grep 53
关闭:killall -KILL dnsmasq
重启: pkill -9 dnsmasp && /usr/local/sbin/dnsmasq -h
还是编辑一个启动脚本吧
vi /etc/init.d/dnsmasq
#!/bin/sh
#
# Startup script for the DNS caching server
#
# chkconfig: - 49 50
# description: This script starts your DNS caching server
# processname: dnsmasq
# pidfile: /var/run/dnsmasq
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
dnsmasq=/usr/local/sbin/dnsmasq
[ -f $dnsmasq ] || exit 0
RETVAL=0
# See how we were called.
case "$1" in
start)
if [ $UID -ne 0 ] ; then
echo "User has insufficient privilege."
exit 4
fi
echo -n "Starting dnsmasq: "
daemon $dnsmasq $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/dnsmasq
;;
stop)
if test "x`pidof dnsmasq`" != x; then
echo -n "Shutting down dnsmasq: "
killproc dnsmasq
fi
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/dnsmasq /var/run/dnsmasq.pid
;;
status)
status dnsmasq
RETVAL=$?
;;
reload)
echo -n "Reloading dnsmasq: "
killproc dnsmasq -HUP
RETVAL=$?
echo
;;
force-reload)
# new configuration takes effect only after restart
$0 stop
$0 start
RETVAL=$?
;;
restart)
$0 stop
$0 start
RETVAL=$?
;;
condrestart)
if test "x`/sbin/pidof dnsmasq`" != x; then
$0 stop
$0 start
RETVAL=$?
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload|condrestart|status}"
exit 2
esac
exit $RETVAL
Tips:如果你是本地编辑上传的,提示找不到文件记得
set ff=unix
再赋予执行的权限
chmod +x /etc/init.d/dnsmasq
现在可以启动和设置自动启动了
service dnsmasq restart
chkconfig dnsmasq on
验证是否正确启动
netstat -tunlp|grep 53
验证是否正确工作
需要命令dig和nslookup,如果没有,安装一下
yum install bind-utils
记得在iptables防火墙开放53端口,tcp和udp都要开
vi /etc/sysconfig/iptables
-A INPUT -p udp -m state --state NEW --dport 53 -j ACCEPT
-A INPUT -p tcp -m state --state NEW --dport 53 -j ACCEPT
将其他机器的DNS换成dnsmasq所在服务器的IP,这是关键,否则没人使用你的DNS服务。
利用dig或者nslookup参看DNS Server是否是
127.0.0.1或者192.168.188.199
大功告成!!!
/etc/dnsmasq.conf中dns和address配置范例
如果你想使用dnsmasq的泛解析功能,ITGeeker这里提供一些范例供你参考。
设定某些网址使用国内的DNS服务器,这里选用了上海最快的上海电信服务器,你也可以选择适合你的最快DNS服务器地址。
#Specify DNS server China
server=/www.freegeeker.com.cn/202.96.209.5
server=/itgeeker.net/202.96.209.5
server=/geekerconsulting.com/202.96.209.5
server=/baidu.com/202.96.209.5
server=/cn/202.96.209.5
server=/taobao.com/202.96.209.5
server=/weibo.com/202.96.209.5
server=/weibo.cn/202.96.209.5
server=/xunlei.com/202.96.209.5
#Specify DNS server out of china
server=/google.com/8.8.4.4
server=/twitter.com/8.8.4.4
server=/facebook.com/8.8.4.4
dnsmasq广受好评的泛解析,可以设定局域网内的特殊网址解析,禁止的ip解析等
#local itgeeker
address=/local.itgeeker.com/192.168.188.199
#forbid ip list
#1. video play website
address=/.youku.com/192.168.188.199
address=/.tudou.com/192.168.188.199
address=/video.sina.com.cn/192.168.188.199
address=/.ku6.com/192.168.188.199
address=/.funshion.com/192.168.188.199
address=/video.sina.com.cn/192.168.188.199
address=/video.sina.com.cn/192.168.188.199
address=/.youku.com/192.168.188.199
address=/.tudou.com/192.168.188.199