Linux下用dnsmasq做dns cache server的配置方法

2016-09-25服务器
95

目前最新版是2.7.1 ,可去其FTP下载:http://www.thekelleys.org.uk/dnsmasq/

安装过程比较简单

yum -y install dnsmasq* 
wget http://www.keepalived.org/software/keepalived-1.2.9.tar.gz
tar zxvf keepalived-1.2.9.tar.gz
cd keepalived-1.2.9
./configure --prefix=/usr/local/keepalived
make && make install
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
chkconfig --add keepalived
chkconfig --level 35 keepalived on

keepalived的配置很简单,只需要配置一个VIP可以在两台Server之间飘来飘去就可以实现主备了

! Configuration File for keepalived
 
global_defs {
  notification_email {
   xxx@xxx.com
  }
  notification_email_from xxx@xxx.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
 
vrrp_instance dnscache {
  state MASTER         // 另一台配置BACKUP
  interface eth1        // 在哪个接口上进行服务器状态检测
  virtual_router_id 51
  priority 180         // BACKUP机器上配置100
  advert_int 1         // 检查间隔,单位为秒
  authentication {
    auth_type PASS
    auth_pass 1234
  }
 
  virtual_ipaddress {       // VIP设置,指定到内网网卡 
    192.168.100.99/24 dev eth1
  }
}

dnsmasq的配置也很简单

resolv-file=/etc/resolv.dnsmasq.conf
cache-size=1000
conf-dir=/etc/dnsmasq.d


  将dns地址写入到/etc/resolv.dnsmasq.conf文件中

echo "nameserver 8.8.8.8" > /etc/resolv.dnsmasq.conf

  本机和局域网其它全部的服务器dns解析都用它

echo "nameserver 192.168.100.99" > /etc/resolv.conf

  最后找一台局域网Server验证一下,如果能解析就说明正常了.

nslookup www.google.cn 192.168.100.99

  此方案只适合小型企业,规模少的情况下使用,解析量大的时候还是用bind最好。

下面是其它网友的补充:


sudo pacman -S --needed dnsmasq
cd /etc

[admin@huangye etc]$ sudo cp -v dnsmasq.conf{,.orig}
`dnsmasq.conf' -> `dnsmasq.conf.orig'

[admin@huangye etc]$ sudo vim dnsmasq.conf


相比来说,dnsmasq的配置简单多了:

resolv-file=/etc/dnsmasq.resolv.conf
addn-hosts=/etc/dnsmasq.hosts
local=/localnet/
no-dhcp-interface=eth0
conf-dir=/etc/dnsmasq.d


[admin@huangye etc]$ sudo cp -v resolv.conf dnsmasq.resolv.conf
Password:
`resolv.conf' -> `dnsmasq.resolv.conf'
sudo mkdir /etc/dnsmasq.d
sudo touch /etc/dnsmasq.hosts

sudo /etc/rc.d/dnsmasq start

最后,别忘记加入rc.conf DAEMONS,注意要在network后面。

dnsmasq 可从额外的hosts文件读取条目,如可以这样添加正向解析:

echo "IP地址 域名" > /etc/dnsmasq.hosts

另外,重启dnsmasq可以用SIGHUP(可以在修改hosts文件后,让配置生效)

 killall -s SIGHUP dnsmasq

查看服务状态:

 netstat -tunl
tcp        0      0 0.0.0.0:53              0.0.0.0:*               LISTEN
udp        0      0 0.0.0.0:53              0.0.0.0:*

再dig 一下,发现稳定在4ms ,我晕,同样的配置,我在一台ubuntu server 10.04 上面dig 是1ms ,在我本地居然在4ms ,囧 

The End

相关推荐

Nginx强制http跳转https访问的几种方法
Nginx让http的链接自动跳转到https的链接,其实方法还是蛮多的,下面介绍两种常用方法。 方法一 可以把所有的HTTP请求通过rewrite重写到HTTPS上 server{ listen 80; server_name XXXXX.com; //你的域名 //rewrite ^(.*)$ https://XXXXXX.com permanent; rewri...
2023-06-19 服务器
372

教你突破基于HTTP_REFERER的防盗链的方法
其实防盗链这个技术并不复杂,目前基本就是在服务器端判断HTTP_REFERER的位置,如果不是来自本站自身,则拒绝输出详细。 那么如果考虑突破防盗链的措施,就需要考虑在HTTPREFERER上面做手脚了。PHP 脚本中对应的变量是$SERVER[HTTPREFERER] ,它存储了 HTTPRE...
2022-12-13 服务器
267

Nginx使用中文文件名中文目录404错误的解决办法
一:确定你的系统是UTF编码 [root@Tserver ~]# env|grep LANG LANG=en_US.UTF-8 二:NGINX配置文件里默认编码设置为utf-8 server { listen 80; server_name .inginx.com ; index index.html index.htm index.php; root /usr/local/nginx/html/inginx.com; ch...
2022-10-14 服务器
902

 如何增加CentOS(Linux)的字体
1、在网上找到你要的目标字体。 2、上传到CentOS 7系统的 /usr/share/fonts 目录中。 3、执行# sudo mkfontscale(如果命令不存在,请yum安装mkfontscale)。 4、执行# mkfontdir (如果命令不存在,请yum安装mkfontdir )。 5、执行# fc-cache -fv(如果命...
2018-01-24 服务器
135

本地wamp下htaccess出错提示Internal Server Error的解决方法
本地wamp下调试url重写,加入htaccess文件后提示:500 Internal Server Error ...,而删除这个文件网站又可以正常访问。 百度了一下,都讲了一大通废话,其实就是没有开启url重写的功能。开启一下就可以。 WAMP下htaccess出错的解决方法: 1. 打开wamp安装目...
2017-11-07 服务器
231

对"对DllRegisterServer的调用失败,错误代码为0x8007005"的解决办法
在手工注册DLL文件时,有时会出现对DllRegisterServer的调用失败,错误代码为0x8007005的提示,导致注册失败,该问题经常出现在Vista系统和Windows7系统下,解决办法如下: 方法一: 运行(或[win]+r): 然后输入regsvr32 wavdest.dll 回车进行注册, 出现...
2017-11-01 服务器
1107