山滚云

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

分类: 阿里云常识作者:时间:2023-01-06 07:37:00浏览量:300℃

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

需要在服务器上对域名进行泛解析,比如访问阿里云的域名统统解析到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,阿里云轻量应用服务器,阿里云数据库sql,阿里云对象存储,阿里云企业邮箱,阿里云安全waf等近百款阿里云计算产品

业务范围:阿里云代理,VPS推荐网,虚拟主机 ,云服务器   微信:ganshangwoniu    QQ:549233124    业务咨询热线:177-2050-9380

   Copyright © 2008-2025 阿里云代理版权所有   

马上电话咨询
阿里云代理微信号