122.在Ubuntu下,/etc/hosts 文件不支持泛域名解析(如 *.example.com)

背景

在Ubuntu下,/etc/hosts 文件不支持泛域名解析(如 *.example.com)。要实现此功能,推荐使用 dnsmasq 软件。

具体实现步骤安装 dnsmasq

打开终端执行:

sudo apt-get update

sudo apt-get install dnsmasq -y

配置泛域名解析编辑 /etc/dnsmasq.conf 文件:

sudo vim  /etc/dnsmasq.conf

在文件末尾添加以下内容(将 example.com 替换为您需要的域名,127.0.0.1 替换为目标 IP):

address=/example.com/127.0.0.1

配置本地 DNS(让本机生效)确保 Ubuntu 优先使用本地 dnsmasq 服务。
修改 /etc/resolv.conf

sudo vim /etc/resolv.conf

将第一行修改为:nameserver 127.0.0.1

重启 dnsmasq 服务

sudo systemctl restart dnsmasq

测试解析使用 ping 或 nslookup 测试,所有子域名应指向同一 IP:

ping test.example.com
ping abc.example.com
posted @ 2026-04-09 15:43  cerofang  阅读(46)  评论(0)    收藏  举报