立即注册
需要在服务器上对域名进行泛解析,比如访问阿里云的域名统统解析到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)
安装非常简单,通过 yum 即可。
yum install dnsmasq -y
先把配置文件进行备份
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
苍穹云企业级稳定、高可用高防云服务器、云虚拟主机、云存储、云计算、网站服务器租用托管服务提供商,一站式云计算解决方案,国内、香港、免费备案云主机等轻松助力企业及个人云端部署!
品质至精 服务至上
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
技术全解答
售前售后通
专业免费支持
技术全解答
售前售后通
专业免费支持
优惠提前知
服务更全面
行业资讯通
技术全解答
售前售后通
专业免费支持
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
技术全解答
售前售后通
专业免费支持
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
技术全解答
售前售后通
专业免费支持
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通
优惠提前知
服务更全面
行业资讯通