帮助中心>安装Dnsmasq让本地hosts支持泛解析

安装Dnsmasq让本地hosts支持泛解析

0 1596
发布于:2022-07-06 09:22:04

需要在服务器上对域名进行泛解析,比如访问阿里云的域名统统解析到200.200.200.200,然后发现hosts文件不支持类似*aliyun.com的这种写法。通过互联网查询可以通过Dnsmasq来解决这个问题,原理其实就是本机的DNS指向Dnsmasq服务器,然后 Dnsmasq 通过类似通配符 (*) 的方式进行匹配,凡是匹配到 *.baidu.com 的都解析到 6.6.6.6。利用 Dnsmasq 实现 hosts 泛解析。

操作步骤

服务器环境

[root@iZbp1io7ymuutxnjc2lblhZ ~]# uname -a

Linux iZbp1io7ymuutxnjc2lblhZ 3.10.0-1160.66.1.el7.x86_64 #1 SMP Wed May 18 16:02:34 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

[root@iZbp1io7ymuutxnjc2lblhZ ~]# cat /etc/redhat-release

CentOS Linux release 7.9.2009 (Core)




安装 Dnsmasq

安装非常简单,通过 yum 即可。

yum install dnsmasq -y



配置  Dnsmasq

先把配置文件进行备份

cp /etc/dnsmasq.conf /etc/dnsmasq.conf_bak


Dnsmasq 的配置在配置文件中都有详细的说明,你可以通过阅读配置文件的注释更改自己想要的配置,我只是想做泛解析,所以我的配置如下:

vim /etc/dnsmasq.conf


域名解析默认读取 /etc/hosts 文件到本地域名配置文件(不支持泛域名)。

DNS 配置默认读取 /etc/resolv.conf 上游 DNS 配置文件,如果读取不到 /etc/hosts 的地址解析,就会转发给 resolv.conf 进行解析地址。


DNS 配置文件

vim /etc/resolv.conf

# 这些都是常用的DNS,可以配置很多(

nameserver 127.0.0.1  # 一定要放在第一个


注意:阿里云服务器的话建议默认DNS也进行修改,还有锁死文件 chattr +i /etc/resolv.conf



启动服务

[root@iZbp1io7ymuutxnjc2lblhZ]# systemctl enable --now dnsmasq

Created symlink from /etc/systemd/system/multi-user.target.wants/dnsmasq.service to /usr/lib/systemd/system/dnsmasq.service.



查看运行状态

[root@iZbp1io7ymuutxnjc2lblhZ ~]# systemctl status dnsmasq

● dnsmasq.service - DNS caching server.

   Loaded: loaded (/usr/lib/systemd/system/dnsmasq.service; enabled; vendor preset: disabled)

   Active: active (running) since 三 2022-07-06 09:45:22 CST; 1min 19s ago

 Main PID: 20885 (dnsmasq)

   CGroup: /system.slice/dnsmasq.service

           └─20885 /usr/sbin/dnsmasq -k


7月 06 09:45:22 iZbp1io7ymuutxnjc2lblhZ systemd[1]: Started DNS caching server..

7月 06 09:45:22 iZbp1io7ymuutxnjc2lblhZ dnsmasq[20885]: started, version 2.76 cachesize 10000

7月 06 09:45:22 iZbp1io7ymuutxnjc2lblhZ dnsmasq[20885]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN DHCP DHCPv6 no-Lua TFTP no-conntrack i... inotify

7月 06 09:45:22 iZbp1io7ymuutxnjc2lblhZ dnsmasq[20885]: reading /etc/resolv.conf

7月 06 09:45:22 iZbp1io7ymuutxnjc2lblhZ dnsmasq[20885]: ignoring nameserver 127.0.0.1 - local interface

7月 06 09:45:22 iZbp1io7ymuutxnjc2lblhZ dnsmasq[20885]: using nameserver 100.100.2.136#53

7月 06 09:45:22 iZbp1io7ymuutxnjc2lblhZ dnsmasq[20885]: using nameserver 100.100.2.138#53

7月 06 09:45:22 iZbp1io7ymuutxnjc2lblhZ dnsmasq[20885]: read /etc/hosts - 3 addresses

Hint: Some lines were ellipsized, use -l to show in full.



测试


[root@iZbp1io7ymuutxnjc2lblhZ ~]# ping www.aliyun.com

PING www.aliyun.com (200.200.200.200) 56(84) bytes of data.

--- www.aliyun.com ping statistics ---

2 packets transmitted, 0 received, 100% packet loss, time 999ms

[root@iZbp1io7ymuutxnjc2lblhZ ~]# ping jsrv2.aegis.aliyun.com

PING jsrv2.aegis.aliyun.com (200.200.200.200) 56(84) bytes of data.


修改配置文件要重启服务
systemctl restart dnsmasq

扫码赞赏,鼓励支持

还没有人赞赏,快来当第一个赞赏的人吧!
    强势安利

    什么是阿里云服务器ECS的入网带宽和出网带宽

    2022-07-05 10:53:49

    强势安利

    阿里云关闭云盾/监控/云安全中心(安骑士)扫描

    2022-07-06 14:15:34


    关键词: 阿里云服务器
    提交成功,系统已安排等保测评顾问稍后为您服务
    微信咨询 获取代理价(更低折扣)
    更低报价 更低折扣 代金券申请
    咨询热线:18120815391
    操作成功
    操作失败
    申请优惠
    立即申请优惠
    提交成功,系统已安排等保测评顾问稍后为您服务
    微信咨询 获取代理价(更低折扣)
    更低报价 更低折扣 代金券申请
    咨询热线:18120815391
    操作成功
    操作失败