首先说一下我的环境
centos 6.7
[root@oldboy ~]# uname -r
2.6.32-573.el6.x86_64
第一步:
在安装dnsmasq之前,请为本机配置静态ip
[root@oldboy ~]# cat/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.10.253
NETMASK=255.255.255.0
GATEWAY=192.168.10.2
[root@oldboy ~]# ifconfig eth0|sed -n '2p'
inet addr:192.168.10.253 Bcast:192.168.10.255 Mask:255.255.255.0
首先保证能够上网
[root@oldboy ~]# ping www.baidu.com -c 4
PING www.a.shifen.com (61.135.169.121) 56(84) bytesof data.
64 bytes from 61.135.169.121: icmp_seq=1 ttl=128time=4.14 ms
64 bytes from 61.135.169.121: icmp_seq=2 ttl=128time=16.6 ms
64 bytes from 61.135.169.121: icmp_seq=4 ttl=128time=20.5 ms
第二步:
[root@oldboy ~]# yum install dnsmasq -y
已加载插件:fastestmirror, security
设置安装进程
Loading mirror speeds from cached hostfile
* base:mirrors.aliyun.com
* extras:mirrors.aliyun.com
* updates:mirrors.aliyun.com
解决依赖关系
--> 执行事务检查
---> Package dnsmasq.x86_64 0:2.48-16.el6_7 willbe 安装
--> 完成依赖关系计算
依赖关系解决
=========================================================================
软件包 架构 版本 仓库 大小
=========================================================================
正在安装:
dnsmasq x86_64 2.48-16.el6_7 updates 149 k
事务概要
=========================================================================
Install 1 Package(s)
总下载量:149 k
Installed size: 293 k
下载软件包:
dnsmasq-2.48-16.el6_7.x86_64.rpm | 149 kB 00:06
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
正在安装 : dnsmasq-2.48-16.el6_7.x86_64 1/1
Verifying :dnsmasq-2.48-16.el6_7.x86_64 1/1
已安装:
dnsmasq.x86_64 0:2.48-16.el6_7
完毕!
第三步
编辑[root@oldboy ~]# vim /etc/dnsmasq.conf
只需修改两处
修改90行
listen-address=127.0.0.1,192.168.10.253
192.168.10.253为本机ip,客户机的dns地址
修改432行
cache-size=10000
设置dns缓存,缓存大小,按需设置
第四步:
[root@oldboy ~]# cat /etc/resolv.conf
; generated by /sbin/dhclient-script
search localdomain
nameserver 223.5.5.5
nameserver 202.106.46.151
223.5.5.5为阿里dns
202.106.46.151.为当地运营商的dns,这里自行添加,指定上游的dns即可
第五步
接下来,启动服务,并设置为开机自启动
[root@oldboy ~]# /etc/init.d/dnsmasq start
Starting dnsmasq: [确定]
[root@oldboy ~]# chkconfig --level 3 dnsmasq on
[root@oldboy ~]# chkconfig --list|grep dnsmasq
dnsmasq 0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:关闭 6:关闭
[root@oldboy ~]#
第六步,
设置客户端dns为我们的dnsmasq服务器地址,并测试
注意两次;; Query time: 20 msec两次时间间隔
[root@oldboy ~]# dig www.baidu.com
; <<>> DiG9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status:NOERROR, id: 16078
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY:0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.baidu.com. IN A
;; ANSWER SECTION:
www.baidu.com. 98 IN CNAME www.a.shifen.com.
www.a.shifen.com. 98 IN A 61.135.169.121
www.a.shifen.com. 98 IN A 61.135.169.125
;; Query time: 20 msec
;; SERVER: 223.5.5.5#53(223.5.5.5)
;; WHEN: Sat Apr 9 14:36:15 2016
;; MSG SIZE rcvd: 90
[root@oldboy ~]# dig www.baidu.com
; <<>> DiG9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status:NOERROR, id: 56741
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY:0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.baidu.com. IN A
;; ANSWER SECTION:
www.baidu.com. 196 IN CNAME www.a.shifen.com.
www.a.shifen.com. 196 IN A 61.135.169.125
www.a.shifen.com. 196 IN A 61.135.169.121
;; Query time: 0 msec
;; SERVER: 192.168.10.253#53(192.168.10.253)
;; WHEN: Mon Apr 11 22:37:56 2016
;; MSG SIZE rcvd: 93