1、直接使用yum安装,并且设置开机自启,关闭SELinux[root@localhost ]# yum install dnsmasq* -y [root@localhost ]# chkconfig dnsmasq on
2、修改本地网络配置文件修改网卡参数[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=172.19.30.250 PREFIX=24 GATEWAY=172.19.30.254 DNS1=127.0.0.1 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME=”System eth0″修改hostname[root@localhost ~]# vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=ad.cloud.com GATEWAY=172.19.30.254
3、修改iptables(嫌麻烦的直接关掉就可以了)允许本机的53端口可对外访问$iptables-AINPUT-pudp-mudp–dport53-jACCEPT $iptables-AINPUT-ptcp-mtcp–dport53-jACCEPT转发DNS请求#开启流量转发功能$echo‘1’>/proc/sys/net/ipv4/ip_forward $echo‘1’>/proc/sys/net/ipv6/ip_forward #IPv6用户选用#添加流量转发规则,将外部到53的端口的请求映射到Dnsmasq服务器的53端口$iptables-tnat-APREROUTING-pudp–dport53-jREDIRECT–to-ports53 $iptables-tnat-APREROUTING-ptcp–dport53-jREDIRECT–to-ports53#如果要限制只允许内网的请求,方法如下,如果是一个网卡,直接把eth1替换成eth0即可$iptables-tnat-APREROUTING-ieth1-pupd–dport53-jREDIRECT–to-port53保存规则并重启$serviceiptablessave $serviceiptablesrestart
4、修改/etc/dnsmasq.conf[root@localhost dnsmasq.d]#vim /etc/dnsmasq.conf 填入以下内容 no-hosts#不加载本地的/etc/hosts文件local-ttl=3600#本地缓存时间,通常不要求缓存本地,这样更改hosts文件后即使生效cache-size=1000000#最大缓存条数dns-forward-max=1000000 listen-address=127.0.0.1,172.19.30.250#如果想要这台服务器做解析,本文所有工具都可以在《Linux就该这么学》这本教材中找到。就要填上自己的地址,并且填上127.0.0.1的地址resolv-file=/etc/resolv.dnsmasq.conf#这个文件是可以自定义的,我就跟随大流,直接复制了一份resolv.conf改了名称就用了all-servers#这条语句的意思就是如果本地没查询到,则想我们的resolv.dnsmasq.conf文件中所有的DNS查询,谁查到的快就用谁的log-queries#开启日志选项log-facility=/var/log/dnsmasq/dnsmasq.log log-async=100#异步log,缓解阻塞,提高性能。默认为5,最大为100conf-dir=/etc/dnsmasq.d#这条应该是最后一句,它的作用其实就是说明该目录下的所有.conf文件都是要做解析的提示:如果咱们内网中存在多个不同域名,我们可以加上上面的这一句,我们做解析时就好分类配置文件
5、添加resolv解析文件修改我们刚才复制的配置文件,指定我们正常需要的DNS,这样的话我们的内网就可以正常的使用网络,而不至于用了自己搭建的DNS,却访问不了互联网。[root@localhost dnsmasq.d]#vim /etc/resolv.dnsmasq.conf nameserver 202.106.0.20 nameserver 192.168.59.241 nameserver 114.114.114.114 nameserver 8.8.8.8 nameserver 168.95.1.1#最后一条为台湾的DNS,我们苹果的官方appstore下载东西会快一些,并且苹果电脑在线重装系统用这个DNS也会快一些
6、添加其他域名解析我们切换到这个目录下,添加我们不同内部域名的解析[root@localhost dnsmasq.d]#cd /etc/dnsmasq.d/添加一个解析文件[root@localhost dnsmasq.d]#vim cloud.conf [root@localhost dnsmasq.d]# ls cloud.conf seccloud.conf
7、配置文件语法规则正常下我们添加的解析内容如下,解析地址的语法规则为:address=/domain/ip_address [root@localhost dnsmasq.d]#cat cloud.conf address=/im.cloud.top/192.168.59.12 address=/git.cloud.top/192.168.59.20 address=/crm.cloud.top/192.168.59.11 address=/ftp.cloud.top/172.19.2.253 address=/note.cloud.top/172.19.30.250
8、启动dnsmasq服务[root@localhost ~]#service dnsmasq start
9、我们进行测试验证:自己的笔记本上域名访问进行测试:localhost:~ admin$ nslookup > git.cloud.top Server:172.19.30.250 Address:172.19.30.250#53 Name:git.cloud.top Address: 192.168.59.20 > www.baidu.comServer:172.19.30.250 Address:172.19.30.250#53 Non-authoritative answer:www.baidu.comcanonical name = www.a.shifen.com. Name:www.a.shifen.comAddress: 61.135.169.121 Name:www.a.shifen.comAddress: 61.135.169.125缓存验证:[root@ad ~]# dig www.baidu.com ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.el6_9.2 <<>> www.baidu.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21877 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 4, ADDITIONAL: 4 ;; QUESTION SECTION: ;www.baidu.com. IN A ;; ANSWER SECTION: www.baidu.com. 223 IN CNAME www.a.shifen.com. www.a.shifen.com. 549 IN A 119.75.213.61 www.a.shifen.com. 549 IN A 119.75.216.20 ;; AUTHORITY SECTION: shifen.com. 30440 IN NS ns4.baidu.com. shifen.com. 30440 IN NS dns.baidu.com. shifen.com. 30440 IN NS ns2.baidu.com. shifen.com. 30440 IN NS ns3.baidu.com. ;; ADDITIONAL SECTION: dns.baidu.com. 4938 IN A 202.108.22.220 ns2.baidu.com. 76100 IN A 61.135.165.235 ns3.baidu.com. 31611 IN A 220.181.37.10 ns4.baidu.com. 166964 IN A 220.181.38.10 ;; Query time: 59 msec;; SERVER: 127.0.0.1#53(127.0.0.1) ;; WHEN: Sat Jul 1 13:25:51 2017 ;; MSG SIZE rcvd: 226 [root@ad ~]# dig www.baidu.com ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.el6_9.2 <<>> www.baidu.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61460 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;www.baidu.com. IN A ;; ANSWER SECTION: www.baidu.com. 215 IN CNAME www.a.shifen.com. www.a.shifen.com. 541 IN A 119.75.216.20 www.a.shifen.com. 541 IN A 119.75.213.61 ;; Query time: 0 msec;; SERVER: 127.0.0.1#53(127.0.0.1) ;; WHEN: Sat Jul 1 13:25:59 2017 ;; MSG SIZE rcvd: 93我们可以看到,上次解析保留了缓存,这次解析直接读取了缓存文件。