ubuntu安装dnsmasq 做dns服务器

本文介绍在ubuntn22.04上安装dnsmasq用做自定义域名服务器,可以在公网上使用。

目标

准备两台不是同一个局域网内的服务器,其中A服务器是ubuntu22.04。在A上安装dnsmasq dns服务器,配置自定义域名,然后在B服务器上配置A做为dns服务器,可以解析自定义的域名;

机器准备

121.37.14.228 dns服务器------A服务器;
124.71.213.146 dns客户端------B服务器;

安装和配置dns服务器

注:安装dnsmasq最好使用root用户。

  1. 在A服务器上安装dnsmasq
apt-get update
apt-get install dnsmasq
  1. 停止和关闭开机自启原来的dns
systemctl stop systemd-resolved
systemctl disable systemd-resolved
  1. 配置dnsmasq.conf
vi /etc/dnsmasq.conf

添加配置到dnsmasq.conf,可直接在后面添加,也可以使用搜索功能找到默认位置,打开注释(这些配置都是默认注释的)

# 监听所有网络接口
interface=eth0
# 绑定到指定接口
bind-interfaces
# 自定义hosts文件
addn-hosts=/etc/dnsmasq.hosts
# 设置要转发的上游 DNS 服务器
server=114.114.114.114
server=8.8.8.8
# 启用缓存以提高性能
cache-size=50
# 启用日志记录以便调试
log-queries
log-dhcp
  1. 创建/etc/dnsmasq.hosts文件
# 可复制etc/hosts
cp /etc/hosts /etc/dnsmasq.hosts
# 配置自定义域名,eg: 120.78.215.251 npm.hlb.com
vi /etc/dnsmasq.hosts

在这里插入图片描述

配置NetworkManager

# 在NetworkManager.conf中另起一行添加 dns=dnsmasq
vi /etc/NetworkManager/NetworkManager.conf

在这里插入图片描述

开放端口

ufw allow 53/tcp
ufw allow 53/udp

重启服务

systemctl restart dnsmasq
## 查看状态
systemctl status dnsmasq

##重启networkmanager
systemctl restart NetworkManager

本地验证

在A服务器上使用nslookup或者dig命令验证;

nslookup nmp.hlb.com
dig npm.hlb.com

在这里插入图片描述

远程服务器上验证

在B服务器上验证;

  1. 修改/etc/resolv.conf文件
# 修改resolv.conf的nameserver=A服务器的ip
# nameserver 121.37.14.228
vi /etc/resolv.conf
  1. 使用nslookup或者dig命令验证
    在这里插入图片描述
  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dnsmasq是一个轻量级的DNS和DHCP服务器软件,它可以作为本地DNS缓存服务器来使用。使用Dnsmasq作为DNS缓存服务器可以提高DNS解析速度,并减少网络带宽的使用。以下是如何在Linux系统中使用Dnsmasq作为DNS缓存服务器的步骤: 1. 安装Dnsmasq软件 在Ubuntu系统中,可以使用以下命令安装Dnsmasq软件: ``` sudo apt-get install dnsmasq ``` 2. 配置Dnsmasq 打开Dnsmasq的配置文件`/etc/dnsmasq.conf`,添加以下配置内容: ``` # Listen on the local interface only interface=lo # Never forward plain names (without a dot or domain part) domain-needed # Never forward addresses in the non-routed address spaces bogus-priv # Enable DNS caching cache-size=1000 # DNS servers to use server=8.8.8.8 server=8.8.4.4 ``` 以上配置中,`interface`指定Dnsmasq监听的网络接口,这里设置为本地回环接口`lo`;`domain-needed`指定不转发没有域名部分的请求;`bogus-priv`指定不转发本地地址和私有地址空间的请求;`cache-size`指定DNS缓存大小;`server`指定要使用的DNS服务器地址,这里使用Google的公共DNS服务器。 3. 重启Dnsmasq服务 执行以下命令重启Dnsmasq服务: ``` sudo systemctl restart dnsmasq ``` 4. 修改本地DNS解析配置 打开`/etc/resolv.conf`文件,将其中的DNS服务器地址修改为`127.0.0.1`,即本地Dnsmasq服务的地址。 现在,Dnsmasq已经作为本地DNS缓存服务器启用了,可以根据需要自行修改Dnsmasq的配置文件来进行更多的配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值