使用DNSmasq搭建ipv4和ipv6的dhcp服务器

使用DNSmasq搭建ipv4和ipv6的dhcp服务器

DNSmasq是一个小巧且方便的配置DNS和DHCP的工具,适用于小型网络,它提供了DNS功能和可选的DHCP功能。在测试使用的CentOS7中,可以直接通过yum进行DNSmasq的安装。另外,在OpenStack(Queens版)中,DNSmasq也作为基础组件提供DNS和DHCP服务。

这里介绍如何在CentOS7为基础操作系统搭建一个简易的DHCP服务,为小型网络中的虚拟机和服务器、PC等提供动态IPv4和IPv6地址。

操作系统:CentOS7.2.1511

DNSmasq版本:dnsmasq-2.66-14.el7_1.x86_64,通过yum安装

1. 安装

首先,通过yum安装dnsmasq。

yum install dnsmasq

2. 配置启用dhcp

首先确认两个文件

  1. /etc/dnsmasq.conf
  2. /var/lib/dnsmasq/dnsmasq.leases

这两个文件,第一个是dnsmasq的配置文件,第二个是已经租约的客户端标识文件。租约文件中会记录哪些客户端从这里获取过ip地址,然后在下次客户端进行请求ip地址的时候,就不会重新分配,而是进行续约。

/etc/dnsmasq.conf中配置进行生效DHCP服务。因为/etc/dnsmasq.conf属于软件提供的默认配置文件,另外有一个配置文件目录,/etc/dnsmasq.d/这里放置的配置文件,默认情况下,会被加载,然后覆盖掉/etc/dnsmasq.conf中的配置。所以我们可以在/etc/dnsmasq.d/中添加自定义的配置文件,进行特性定制,而不会影响到原有的配置文件中的配置。
可以查看/etc/dnsmasq.conf配置文件内容如下:

    628 # Include another lot of configuration options.
    629 #conf-file=/etc/dnsmasq.more.conf
    630 conf-dir=/etc/dnsmasq.d

这里指定了配置文件目录,会读取目录中的配置文件进行加载。

由于配置文件中的内容特别多,不一一列举,只看生效DHCP的配置项。

生效DHCP的配置,主要有以下几项:

    147 # This is an example of a DHCP range where the netmask is given. This
    148 # is needed for networks we reach the dnsmasq DHCP server via a relay
    149 # agent. If you don't know what a DHCP relay agent is, you probably
    150 # don't need to worry about this.
    151 dhcp-range=192.168.0.50,192.168.0.150,255.255.255.0,12h

这里是配置IPv4地址的配置,对应有地址段和掩码,租期等信息。

IPv6类似,如下:

    187 # Do DHCP and Router Advertisements for this subnet. Set the A bit in the RA
    188 # so that clients can use SLAAC addresses as well as DHCP ones.
    189 dhcp-range=1234::2, 1234::500, slaac

这里的配置可以自动进行路由广播并进行DHCP地址分配。对IPv6网络不是很了解,最初没有选用slaac模式,无法自动获取IPv6地址。slaac是指Stateless address autoconfiguration,无状态地址自动配置。

3. 配置网卡IP地址

配置完成后,还有一个需要注意的问题。因为DHCP是通过运行DNSmasq的机器进行提供服务的。它会默认监听53端口提供服务。这个监听是在0.0.0.0:::上,所以动态分配的ip地址段,即2中配置的段,应该在本地的某块网卡上**有对应段的地址**。如果没有的话,将会报如下错:

Dec 31 20:13:39 localhost dnsmasq-dhcp[6952]: no address range available for DHCP request via eno1

因为网卡eno1并没有上边配置的段的ip地址。所以,手动在本地网卡上配置要分配的段的ip地址。

4. 启动服务

通过如下命令启动服务:

systemctl start dnsmasq

启动后,在/var/log/message中会打印出启动日志:

Jan  2 20:22:05 localhost systemd: Started DNS caching server..
Jan  2 20:22:05 localhost systemd: Starting DNS caching server....
Jan  2 20:22:05 localhost dnsmasq[22139]: started, version 2.66 cachesize 150
Jan  2 20:22:05 localhost dnsmasq[22139]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN DHCP DHCPv6 no-Lua TFTP no-conntrack ipset auth
Jan  2 20:22:05 localhost dnsmasq-dhcp[22139]: DHCP, IP range 192.168.0.50 -- 192.168.0.150, lease time 2m
Jan  2 20:22:05 localhost dnsmasq-dhcp[22139]: DHCPv6, IP range 1234::2 -- 1234::500, lease time 1h
Jan  2 20:22:05 localhost dnsmasq-dhcp[22139]: router advertisement on 1234::
Jan  2 20:22:05 localhost dnsmasq[22139]: no servers found in /etc/resolv.conf, will retry
Jan  2 20:22:05 localhost dnsmasq[22139]: read /etc/hosts - 2 addresses

可以确定服务已经启动成功,可以提供DHCP服务了。

5. 注意事项

由于我实验的环境是通过kvm虚拟机进行的,kvm虚拟机有一个问题,如果网卡选用了macvtap模式的话,那么宿主机和虚拟机之间的网络是不通的,所以获取不到ip地址。之前因为这个问题,卡了好久。另外在配置kvm虚拟机,选择macvtap模式的时候,它会提示你。虚拟机之间进行通信没有问题。如果使用VMware选用桥接模式,就不会有这个问题,不影响宿主机和虚拟机之间的通信。

猜你喜欢

转载自blog.csdn.net/stpice/article/details/103907006
今日推荐