前言緩存
DNSmasq是一個小巧且方便地用於配置DNS和DHCP的工具,適用於小型網絡。它提供了DNS功能和可選擇的DHCP功能能夠取代dhcpd(DHCPD服務配置)和bind等服務,配置起來更簡單,更適用於虛擬化和大數據環境的部署。網絡
DNSmasq主要是在配置文件/etc/dnsmasq.conf ,利用好就能快捷部署好使的dhcp和dns服務。app
DHCP服務dom
# 服務監聽的網絡接口地址異步
#interface=eth0async
listen-address=192.168.1.132,127.0.0.1工具
# dhcp動態分配的地址範圍oop
dhcp-range=192.168.1.50,192.168.1.150,48h性能
# dhcp服務的靜態綁定測試
# dhcp-host=00:0C:29:5E:F2:6F,192.168.1.201,infinite 無限租期
dhcp-host=00:0C:29:5E:F2:6F,192.168.1.201,os02
dhcp-host=00:0C:29:15:63:CF,192.168.1.202,os03
# 設置默認租期
#dhcp-lease-max=150
# 租期保存在下面文件
#dhcp-leasefile=/var/lib/dnsmasq/dnsmasq.leases
# 經過/etc/hosts來分配對應的hostname
#dhcp-host=judge
# 忽略下面MAC地址的DHCP請求
#dhcp-host=11:22:33:44:55:66,ignore
# dhcp所在的domain
domain=debugo.com
# 設置默認路由出口
dhcp-option=3,192.168.0.1
# 設置NTP Server.這是使用option name而非選項名來進行設置
#dhcp-option=option:ntp-server,192.168.0.4,10.10.0.5
啓動dnsmasq服務
service dnsmasq start
下面在客戶端進行測試:
# 確保網絡接口配置使用dhcp方式
[root@localhost] cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE="eth1"
BOOTPROTO=dhcp
IPV6INIT=no
NM_CONTROLLED=no
ONBOOT="yes"
TYPE="Ethernet"
# 重啓網絡服務
[root@localhost] service network restart
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0:
Determining IP information for eth1... done. [ OK ]
# 檢查IP地址
[root@os03] ifconfig
eth1 Link encap:Ethernet HWaddr 00:0C:29:15:63:D9
inet addr:192.168.1.202 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe15:63d9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:251 errors:0 dropped:0 overruns:0 frame:0
TX packets:43 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:36077 (35.2 KiB) TX bytes:4598 (4.4 KiB)
......
# 檢查默認路由
[root@os03] route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth1
配置DNS服務
DNSmasq可以緩存外部DNS記錄,同時提供本地DNS解析或者做爲外部DNS的代理,即DNSmasq會首先查找/etc/hosts等本地解析文件,而後再查找/etc/resolv.conf等外部nameserver配置文件中定義的外部DNS。因此說dnsmasq是一個很不錯的DNS中繼。DNS配置一樣寫入dnsmasq.conf配置文件裏。
# 本地解析文件
#no-hosts
#addn-hosts=/etc/banner_add_hosts
# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.
# 例如,/etc/hosts中的os01將擴展成os01.debugo.com
expand-hosts
local=/debugo.com/
# 強制使用完整的解析名
domain-needed
# 添加額外的上級DNS主機(nameserver)配置文件
#resolv-file=
# 不使用上級DNS主機配置文件(/etc/resolv.conf和resolv-file)
no-resolv
# 相應的,能夠爲特定的域名指定解析它的nameserver。通常是其餘的內部DNS name server
# server=/myserver.com/192.168.0.1
# 設置DNS緩存大小(單位:DNS解析條數)
cache-size=500
# 關於log的幾個選項
log-queries
#log-dhcp
log-facility=/var/log/dnsmasq.log
# 異步log,緩解阻塞,提升性能。
log-async=20
# 指定domain的IP地址
address=/doubleclick.net/127.0.0.1
address=/.phobos.apple.com/202.175.5.114
配置完成後重啓dnsmasq,而後在客戶端測試:
[root@os03] nslookup os01.debugo.com
Server: 192.168.1.132
Address: 192.168.1.132#53
Name: os01.debugo.com
Address: 192.168.1.132
[root@os03] nslookup os02.debugo.com
Server: 192.168.1.132
Address: 192.168.1.132#53
Name: os02.debugo.com
Address: 192.168.1.201
[root@os03] nslookup doubleclick.net
Server: 192.168.1.132
Address: 192.168.1.132#53
Name: doubleclick.net
Address: 127.0.0.1
#注意,因爲address選項解析爲127.0.0.1,而非server的192.168.1.132地址。
[root@os03] nslookup a1.phobos.apple.com
Server: 192.168.1.132
Address: 192.168.1.132#53
Name: a1.phobos.apple.com
Address: 202.175.5.114
好了,說到這裏應該都會使用DNSmasq這個小工具了,簡單快捷,下一期,咱們再講講如何搭建智能dns。更多精彩》》