Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

25.4. dnsmasq

25.4.1. Install

CentOS / Redhat
yum -y install dnsmasq
			
Debian / Ubuntu
apt-get install dnsmasq
			
Firewall 设置
iptables -A INPUT -p udp -m udp –dport 53 -j ACCEPT
			

25.4.2. /etc/dnsmasq.conf

一般配置下面三处即可

# vim /etc/dnsmasq.conf

resolv-file=/etc/dnsmasq.resolv.conf
addn-hosts=/etc/dnsmasq.hosts
conf-dir=/etc/dnsmasq.d

/etc/init.d/dnsmasq restart
		

25.4.3. dnsmasq.resolv.conf

让dnsmasq 接管DNS解析

# vim /etc/dnsmasq.conf

resolv-file=/etc/dnsmasq.resolv.conf
resolv-file
		
		
sudo cp /etc/resolv.conf /etc/dnsmasq.resolv.conf

cat > /etc/dnsmasq.resolv.conf <<EOF
nameserver 208.67.222.222
nameserver 208.67.220.220
EOF
		
		

或者

nameserver 8.8.8.8
nameserver 4.4.4.4
		

/etc/resolv.conf 设置用本机做解析

echo "nameserver 127.0.0.1″ > /etc/resolv.conf
or
sudo vim /etc/resolv.conf
nameserver 127.0.0.1
		

reload

/etc/init.d/dnsmasq reload
or
sudo killall -s SIGHUP dnsmasq
		

25.4.4. dnsmasq.hosts

dnsmasq 默认会读取 /etc/hosts 如果你不想让它解析/etc/hosts文件,可以自己定义一个文件。

# vim /etc/dnsmasq.conf
no-hosts
addn-hosts=/etc/dnsmasq.hosts
		
echo "172.16.0.1 test.example.com" > /etc/dnsmasq.hosts
		

重新起动

/etc/init.d/dnsmasq restart
		

查看日志

cat /var/log/message

Sep 15 18:17:24 J10-51-MemCache dnsmasq[13799]: read /etc/hosts - 2 addresses
Sep 15 18:17:24 J10-51-MemCache dnsmasq[13799]: read /etc/dnsmasq.hosts - 40 addresses
		

使用nslookup测试

nslookup test.example.com 172.16.3.51
		
[提示]提示

注释no-hosts选项,可以实现 /etc/hosts 与 /etc/dnsmasq.hosts 共用

25.4.5. /etc/dnsmasq.d/dnsmasq.server.conf

配置域名使用那些DNS解析

vim /etc/dnsmasq.d/dnsmasq.server.conf

server=/google.com/8.8.8.8
server=/yahoo.com/4.4.4.4
server=/qq.com/202.96.134.133
server=/com.cn/202.96.128.68
server=/us/208.67.222.222
server=/uk/208.67.220.220
		

反向解析

# Add other name servers here, with domain specs if they are for
# non-public domains.
#server=/localnet/192.168.0.1

# Example of routing PTR queries to nameservers: this will send all
# address->name queries for 192.168.3/24 to nameserver 10.1.2.3
#server=/3.168.192.in-addr.arpa/10.1.2.3
		

25.4.6. /etc/dnsmasq.d/dnsmasq.address.conf

vim /etc/dnsmasq.d/dnsmasq.address.conf

address=/www.mydomain.com/172.16.0.254
		

deny domain

address=/www.facebook.com/127.0.0.1
address=/www.google.com/127.0.0.1
		
域名劫持

将域名解析到错误的地址,这样可以屏蔽一些网站。

address=/www.facebook.com/127.0.0.1
address=/www.google.com/127.0.0.1
			

例如:在企业网络中不想让员下载安装软件,可以将下载网站解析到错误的地址上去,做到网址屏蔽

address=/www.download.com/127.0.0.1
			

25.4.7. FAQ

dnsdomainname: Unknown host

# hostname -i
hostname: Unknown host

echo "127.0.0.1    `hostname`" >> /etc/hosts

# hostname -i
127.0.0.1
		

什么时候使用 reload / restart

开启或禁用选项必须使用restart, 更新配置可以使用reload