前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 dnsmasq 部署 DNS 服务器

使用 dnsmasq 部署 DNS 服务器

原创
作者头像
zero000
修改2021-07-14 18:05:36
2.3K0
修改2021-07-14 18:05:36
举报
文章被收录于专栏:程序员菜谱程序员菜谱

简介

dnsmasq 常用作部署简单的 DNS 服务器 和 DHCP 服务器,下文将介绍如何使用 dnsmasq 部署一个简单的 DNS 服务器

安装 dnsamsq

参考这里

配置 dnsmasq

sudo vi /etc/dnsmasq.conf
domain-needed
bogus-priv
no-resolv          
server=8.8.8.8    # forward 的服务器
server=8.8.4.4    # forward 的服务器
local=/mydomain.org/ # mydomain.org 使用本地解析,不进行forward
listen-address=::1,127.0.0.1,192.168.1.10 # 绑定指定的网卡接口
expand-hosts      # 使用下面domain自动补全域名解析,例如/etc/hosts 配置了 "test 127.0.0.1" , 这时候客户端查询 "dig test.mydomain.org" 可以获取到对应的解析

# 下面 DHCP 相关,domain 字段上面 DNS 配置会使用到
domain=mydomain.org
dhcp-range=192.168.1.100,192.168.1.200,24h
dhcp-option=option:router,192.168.1.1
dhcp-authoritative
dhcp-leasefile=/var/lib/dnsmasq/dnsmasq.leases

添加记录

直接使用/etc/hosts的记录用作 dns 解析

sudo vi /etc/hosts

添加下面记录

127.0.0.1   localhost localhost.localdomain
::1         localhost localhost.localdomain
192.168.1.1    router
192.168.1.10   dnsmasq
192.168.1.20   server1
192.168.1.30   server2

启动 dnsamsq

sudo systemctl restart dnsmasq

测试

正向解析测试

$ nslookup server1
Server:       127.0.0.1
Address:      127.0.0.1#53
Name:         server1.mydomain.org
Address:      192.168.1.20

反向测试

$ nslookup 192.168.1.20
20.1.168.192.in-addr.arpa    name = server1.mydomain.org.

测试外部域名解析

$ nslookup fedoramagazine.org
Server:       127.0.0.1
Address:      127.0.0.1#53
Non-authoritative answer:
Name:    fedoramagazine.org
Address: 35.196.109.67

实现 DNS Round Robin

查询一些历史数据,dnsmasq 本来不支持 DNS Round Robin 的,但后续的版本好像已经支持了这个功能

关键是 addn-hosts 这个配置

以刚才的配置为例,添加下面配置

domain-needed
bogus-priv
no-resolv          
server=8.8.8.8    # forward 的服务器
server=8.8.4.4    # forward 的服务器
local=/mydomain.org/ # mydomain.org 使用本地解析,不进行forward
listen-address=::1,127.0.0.1,192.168.1.10 # 绑定指定的网卡接口
expand-hosts      # 使用下面domain自动补全域名解析,例如/etc/hosts 配置了 "test 127.0.0.1" , 这时候客户端查询 "dig test.mydomain.org" 可以获取到对应的解析

....

addn-hosts=/etc/dnsmasq.addn-hosts/

创建对应的文件,存储域名A解析记录

$ mkdir -p /etc/dnsmasq.addn-hosts/

$ vi /etc/dnsmasq.addn-hosts/test.domain

# 注意,这里需要添加 FQDN(Fully Qualified Domain Name) 域名
192.168.1.30   server2.test.com
192.168.1.31   server2.test.com
192.168.1.32   server2.test.com

最后重启dnsamsq

sudo systemctl restart dnsmasq

这时候,测试记录

$ dig server2.test.com +short
192.168.1.30
192.168.1.31
192.168.1.32

参考:

  1. https://fedoramagazine.org/dnsmasq-provide-dns-dhcp-services/

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 安装 dnsamsq
  • 配置 dnsmasq
  • 添加记录
  • 启动 dnsamsq
  • 测试
  • 实现 DNS Round Robin
  • 参考:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档