质量为本、客户为根、勇于拼搏、务实创新

www.cangqong.com

< 返回新闻公共列表

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

发布时间:2022-09-03 20:23:27

需要在服务器上对域名进行泛解析,比如访问阿里云的域名统统解析到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)

20220706093317332.png



安装 Dnsmasq

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

yum install dnsmasq -y

20220706093116264.png



配置  Dnsmasq

先把配置文件进行备份

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

20220706093252891.png


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

vim /etc/dnsmasq.conf

20220706094239949.png

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

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


DNS 配置文件

vim /etc/resolv.conf

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

nameserver 127.0.0.1  # 一定要放在第一个

20220706094442078.png

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


20220706112301863.png


启动服务

[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.

20220706094625425.png


查看运行状态

[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.

20220706094711078.png


测试


[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



苍穹云企业级稳定、高可用高防云服务器、云虚拟主机、云存储、云计算、网站服务器租用托管服务提供商,一站式云计算解决方案,国内、香港、免费备案云主机等轻松助力企业及个人云端部署!



/template/Home/cangqong/PC/Static

客服服务中心

品质至精 服务至上

售前咨询

  • 售前二胖
    添加售前胖胖微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前毛毛
    添加售前毛毛微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前小赖
    添加售前小赖微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前舟舟
    添加售前舟舟微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前小溪
    添加售前小溪微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前小志
    添加售前小志微信

    优惠提前知

    服务更全面

    行业资讯通

  • 技术周工
    添加技术周工微信

    优惠提前知

    服务更全面

    行业资讯通

  • 技术刘工
    添加技术刘工微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前甜甜
    添加售前甜甜微信

    优惠提前知

    服务更全面

    行业资讯通

  • 技术方工
    添加技术方工微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前佳佳
    添加售前佳佳微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前霍霍
    添加售前霍霍微信

    优惠提前知

    服务更全面

    行业资讯通

  • 技术豆工
    添加技术豆工微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前小特
    添加售前小特微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前小米
    添加售前小米微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前小黄
    添加售前小黄微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前小潘
    添加售前小潘微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前小美
    添加售前小美微信

    优惠提前知

    服务更全面

    行业资讯通

  • 技术程工
    添加技术程工微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前小情
    添加售前小情微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前苒苒
    添加售前苒苒微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前小蒋
    添加售前小蒋微信

    优惠提前知

    服务更全面

    行业资讯通

  • 售前兮兮
    添加售前兮兮微信

    优惠提前知

    服务更全面

    行业资讯通

值班售后/技术支持

财务续费/备案专员

投诉建议

紧急电话

400-876-1858