DNSmasq是一个小巧且方便地用于配置DNS和DHCP的工具,适用于小型网络。它提供了DNS功能和可选择的DHCP功能可以取代dhcpd(DHCPD服务配置)和bind等服务,配置起来更简单,更适用于虚拟化和大数据环境的部署。

dhcp服务

其中一些关键的配置如下,配置文件/etc/dnsmasq.conf 中的注释已经给出了非常详细的解释。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 服务监听的网络接口地址
# If you want dnsmasq to listen for DHCP and DNS requests only on
# specified interfaces (and the loopback) give the name of the
# interface (eg eth0) here.
# Repeat the line for more than one interface.
#interface=
# Or you can specify which interface _not_ to listen on
#except-interface=
# Or which to listen on by address (remember to include 127.0.0.1 if
# you use this.)
listen-address=192.168.1.132,127.0.0.1
 
# dhcp动态分配的地址范围
# Uncomment this to enable the integrated DHCP server, you need
# to supply the range of addresses available for lease and optionally a lease time
dhcp-range=192.168.1.50,192.168.1.150,48h
 
# dhcp服务的静态绑定
# Always set the name and ipaddr of the host with hardware address
# dhcp-host=00:0C:29:5E:F2:6F,192.168.1.201
# 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
 
# 设置默认租期
# Set the limit on DHCP leases, the default is 150
#dhcp-lease-max=150
 
# 租期保存在下面文件
# The DHCP server needs somewhere on disk to keep its lease database.
# This defaults to a sane location, but if you want to change it, use
# the line below.
#dhcp-leasefile=/var/lib/dnsmasq/dnsmasq.leases
 
# 通过/etc/hosts来分配对应的hostname
# Enable the address given for "judge" in /etc/hosts
# to be given to a machine presenting the name "judge" when
# it asks for a DHCP lease.
#dhcp-host=judge
 
# 忽略下面MAC地址的DHCP请求
# Never offer DHCP service to a machine whose ethernet
# address is 11:22:33:44:55:66
#dhcp-host=11:22:33:44:55:66,ignore
 
# dhcp所在的domain
# Set the domain for dnsmasq. this is optional, but if it is set, it
# does the following things.
# 1) Allows DHCP hosts to have fully qualified domain names, as long
#     as the domain part matches this setting.
# 2) Sets the "domain" DHCP option thereby potentially setting the
#    domain of all systems configured by DHCP
# 3) Provides the domain part for "expand-hosts"
domain=debugo.com
 
# 设置默认路由出口
# dhcp-option遵循RFC 2132(Options and BOOTP Vendor Extensions),可以通过dnsmasq --help dhcp来查看具体的配置
# 很多高级的配置,如iSCSI连接配置等同样可以由RFC 2132定义的dhcp-option中给出。
# option 3为default route
# Override the default route supplied by dnsmasq, which assumes the
# router is the same machine as the one running dnsmasq.
dhcp-option=3,192.168.0.1
 
# 设置NTP Server.这是使用option name而非选项名来进行设置
# Set the NTP time server addresses to 192.168.0.4 and 10.10.0.5
#dhcp-option=option:ntp-server,192.168.0.4,10.10.0.5

注意:当为某一MAC地址同时静态分配主机名和IP时,如果写到两条dhcp-host选项里(如下所示),则只会生效后面的一条。正确的选项写法如上配置。

1
2
dhcp-host=00:0C:29:5E:F2:6F,192.168.1.201
dhcp-host=00:0C:29:5E:F2:6F,os02

重新启动客户端网卡。由于之前测试中客户端网卡已经申请了DHCP租期。所以这里需要修改租期文件,让客户端重新获得IP和hostname。

1
2
3
[root@server] vim /var/lib/dnsmasq/dnsmasq.leases
1400240493 00:0c:29:5e:f2:6f 192.168.1.143 os02 *
1400240498 00:0c:29:15:63:cf 192.168.1.52 os01 *

启动dnsmasq服务(server的IP为192.168.1.132)

1
[root@server]dnsmasq

下面在客户端进行测试:
# 确保网络接口配置使用dhcp方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[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.
# 检查IP地址                                                           [  OK  ]
[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配置文件里。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#
本地解析文件
#
If you don't want dnsmasq to read /etc/hosts, uncomment the following line.
#no-hosts
#
or if you want it to read another file, as well as /etc/hosts, use this.
#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
#
Add local-only domains here, queries in these domains are answered
#
from /etc/hosts or DHCP only.
local=/debugo.com/
#
强制使用完整的解析名
#
Never forward plain names (without a dot or domain part)
domain-needed
#
添加额外的上级DNS主机(nameserver)配置文件
#
Change this line if you want dns to get its upstream servers from
#
somewhere other that /etc/resolv.conf
#resolv-file=
#
不使用上级DNS主机配置文件(/etc/resolv.conf和resolv-file)
#
If you don't want dnsmasq to read /etc/resolv.conf or any other
#
file, getting its servers from this file instead (see below), then
#
uncomment this.
no-resolv
#
相应的,可以为特定的域名指定解析它的nameserver。一般是其他的内部DNS name server
#
Add other name servers here, with domain specs if they are for
#
non-public domains.
#
server=/myserver.com/192.168.0.1
#
设置DNS缓存大小(单位:DNS解析条数)
#Set
the size of dnsmasq's cache. The default is 150 names. Setting the cache size to zero disables caching.
cache-size=500
#
关于log的几个选项
#
For debugging purposes, log each DNS query as it passes through
#
dnsmasq.
log-queries
#
Log lots of extra information about DHCP transactions.
#log-dhcp
#
Log to this syslog facility or file. (defaults to DAEMON)
log-facility=/var/log/dnsmasq.log
#
异步log,缓解阻塞,提高性能。
#
Enable asynchronous logging and optionally set the limit on the number of lines which will be queued by dnsmasq
#
when writing to the syslog is slow.
#
Dnsmasq can log asynchronously: this allows it to continue functioning without being blocked by syslog,
#
and allows syslog to use dnsmasq for DNS queries without risking deadlock. If the queue of log-lines becomes
#
full, dnsmasq will log the overflow, and the number of messages lost.
#
The default queue length is 5, a sane value would be 5-25, and a maximum limit of 100 is imposed.
log-async=20
#
指定domain的IP地址
#
Add domains which you want to force to an IP address here.
#
The example below send any host in doubleclick.net to a local
#
webserver.
address=/doubleclick.net/127.0.0.1
address=/.phobos.apple.com/202.175.5.114

配置完成后重启dnsmasq,然后在客户端测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[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和DHCP的更多相关文章

  1. centos DNS服务搭建 DNS原理 使用bind搭建DNS服务器 配置DNS转发 配置主从 安装dig工具 DHCP dhclient 各种域名解析记录 mydns DNS动态更新 第三十节课

    centos  DNS服务搭建  DNS原理  使用bind搭建DNS服务器 配置DNS转发 配置主从  安装dig工具  DHCP  dhclient  各种域名解析记录  mydns DNS动态更 ...

  2. DNSmasq搭建DNS服务器

    原文地址:http://jirry.me/2016/04/19/dnsmasq-on-aliyun/ DNSmasq 是一个小巧且方便地用于配置 DNS 和 DHCP 的工具,适用于小型网络,它提供了 ...

  3. cobbler 更换dns和dhcp服务器为dnsmasq

    1) 需要配置/etc/cobbler/module.conf, 把manage_dns和manage_dhcp改为manage_dnsmasq 2) 重启cobbler和dnsmasq服务,dnsm ...

  4. 本地配置DNS服务器(MAC版)

    作为一个前端开发者,会遇到使用cookie的情况,常见的如:登录,权限控制,视频播放,图形验证码等,这时候本地开发者在PC上会使用修改hosts的方式添加指向本地的域名,来获取cookie的同域名.如 ...

  5. centos7安装dnsmasq局域网dns

    Dnsmaq介绍: Dnsmasq是一款小巧且方便地用于配置DNS服务器和DHCP服务器的工具,适用于小型网络,它提供了DNS解析功能和可选择的DHCP功能. Dnsmasq可以解决小范围的dns查询 ...

  6. (DNS)dnsmasq部署DNS

    转自:https://www.hi-linux.com/posts/30947.html Dnsmasq提供DNS缓存和DHCP服务.Tftp服务功能.作为域名解析服务器(DNS),Dnsmasq可以 ...

  7. 烂泥:centos安装及配置DNS服务器

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 要在centos配置DNS服务器,要先安装DNS软件BIND.当然我们也可以安装其他的DNS软件,比如国内的开源DNS软件DNSPod. 在此我们以通过 ...

  8. dnsmasq配置

    介绍 dnsmasq是一个dns和dhcp服务 参数说明 dhcp-sequential-ip 默认情况下Dnsmasq分配ip是根据目标机的mac地址按一定的hash规则来的.这样每个目标机在租约期 ...

  9. 第9章 应用层(2)_动态主机配置协议(DHCP)

    2. 动态主机配置协议(DHCP) 2.1 静态地址和动态地址的应用场景 (1)静态地址应用场景 ①IP地址不经常更改的设备(如服务器地址) ②使用有规律的IP地址以便于管理(如学校机房为方便教师管理 ...

随机推荐

  1. .net ServiceStack.Redis 性能调优

    最近在debug生产环境的问题时,发现了ServiceStack 4.0.60版本RedisClient存在一个非常严重的性能问题.在高并发下,PooledRedisClientManager.Get ...

  2. 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II

    好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...

  3. 在finally中调用一个需要await的方法

    最近在把code改写成async+await的形式,发现有些情况下需要在finally中需要调用异步方法,但是编译器不允许在cache和finally中出现await关键字...但是用Wait()或者 ...

  4. JAVA的垃圾回收机制

    1. 垃圾回收的意义 在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之前不能分配给其它对象:而在Java中,当没有对象引用指向原先分配给某个对象的内存时,该内存便成为垃圾.JVM的 ...

  5. 【JavaScript】js数组操作,由push到那么多

    shift() 定义:删除并返回数组的第一个元素: pop() 定义:删除数组最后一个元素,并返回: push() 定义:在数组后边添加一个或者多个元素,并返回新数组的长度: array.push(& ...

  6. add添加

    s = {1,2,3,4,5,6,} 进行添加数据! s = {1,2,3,4,5,6,} s.add('s')#添加字符串's' s.add('3')#添加字符串'3' s.add(3)#添加3和字 ...

  7. H5项目常见问题汇总及解决方案(果断复制粘贴,不解释)

    H5项目常见问题及注意事项 Meta基础知识: H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 //一.HTML页面结构 <meta name="viewport" co ...

  8. 2016 Web开发资源工具大搜罗

    来源于:https://zhuanlan.zhihu.com/p/22730771 作者:余博伦链接:https://zhuanlan.zhihu.com/p/22730771来源:知乎著作权归作者所 ...

  9. 在eclipse中部署jsp项目

    昨天在做实验的时候发现图片的路径不对,怎么改都不对,后来想到在浏览器中输入localhost:8080是tomcat服务器的路径,没找到资源是不是就是项没有部署到tomcat中,去tomcat的web ...

  10. 【BZOJ-3643】Phi的反函数 数论 + 搜索

    3643: Phi的反函数 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 141  Solved: 96[Submit][Status][Discuss ...