摘要:本文通过部署DNS服务器来提供自定义的dns服务。 每次更新DNS时,登录10.20.31.198,进入root,然后运行./updateDnsmasqFrom******.sh, 当然了,此处星号要换成具体的hosts来源网址才行
工具
- ubuntu;其他平台略!
- dnsmasq
- 找一个提供hosts更新的网站
安装
在Ubuntu下用apt安装,或者源代码编译安装
配置操作
我们在10.20.31.198上部署了dnsmasq,配置文件有两个,分别是dnsmasq.conf.withoutaddress和updateDnsmasqFrom*.sh,
bash
root@server198:/home/ubuntu# ls |grep masq
dnsmasq.conf.withoutaddress
updateDnsmasqFrom*******.sh
每次更新时,首先手动进入root, 然后运行./updateDnsmasqFrom*******.sh
,运行时打印信息如下,
```bash
root@server198:/home/ubuntu# ./updateDnsmasqFrom*.sh
only can run as root!
--2016-07-01 14:58:37-- https://*.//hosts
Resolving * (*)... 120.*
Connecting to * (*)|120.**|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
Saving to: ‘hosts’
[ <=> ] 148,043 --.-K/s in 0.1s
2016-07-01 14:58:38 (1.32 MB/s) - ‘hosts’ saved [148043]
Version: 2016-06-30
make sure dnsmasq.conf.withoutaddress exists
awk: cmd. line:1: warning: escape sequence \/' treated as plain
/'
* Restarting DNS forwarder and DHCP server dnsmasq [ OK ]
update OK!
```
当然也可以后台运行,例如screen ./updateDnsmasqFrom******.sh
配置文件详解
下面介绍上面两个配置文件,前者代码中有效的代码如下,
bash
ubuntu@server198:~$ cat dnsmasq.conf.withoutaddress |grep -Evn "^$|#"
34:resolv-file=/etc/resolv.dnsmasq.conf
41:strict-order
55:server=8.8.8.8
95:interface=eth0
100:listen-address=127.0.0.1
104:no-dhcp-interface=eth0
可以看出这就是dnsmasq安装后默认的配置文件;
后者主要用于更新dnsmasp的配置文件,包括从coding.net下载hosts文件,写入dns服务配置文件,重启dnsmasq服务3个过程,代码如下,
bash
ubuntu@server198:~$ grep -Evn "^$" updateDnsmasqFrom*******.sh
1:#!/bin/bash
3:echo 'only can run as root!'
4:mv hosts hosts.bak
5:wget https://coding.net/u/scaffrey/p/hosts/git/raw/master/hosts
7:grep "Last updated" hosts |awk '{print "Version: " $4}'
9:echo 'make sure dnsmasq.conf.withoutaddress exists'
11:cat dnsmasq.conf.withoutaddress > /etc/dnsmasq.conf
13:cat hosts |grep -v "#"|grep '\.' |awk '{print "address=\/"$2"\/" $1}' >>/etc/dnsmasq.conf
15:/etc/init.d/dnsmasq restart
17:if [ $? -eq 0 ]; then
18: echo "update OK!"
19: rm hosts.bak
20:else
21: echo "update Failed, try later!"
22: mv hosts.bak hosts
23: cat dnsmasq.conf.withoutaddress > /etc/dnsmasq.conf
24: cat hosts |grep -v "#"|grep '\.' |awk '{print "address=\/"$2"\/" $1}' >>/etc/dnsmasq.conf
25: /etc/init.d/dnsmasq restart
26:fi
28:sleep 1h
29:./updateDnsmasqFrom*******.sh
ubuntu@server198:~$