hosts文件不支持泛域名解析,如果在内网需要泛域名解析,只能自己搭建一个dns服务器
服务器配置
yum install dnsmasq -y
cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
vi /etc/dnsmasq.conf
#定义dnsmasq从哪里获取上游DNS服务器的地址, 默认是从/etc/resolv.conf获取
#resolv-file=
# 严格按照 resolv-file 文件中的顺序从上到下进行 DNS 解析, 直到第一个成功解析成功为止
strict-order
# 监听的 IP 地址
listen-address=127.0.0.1,192.168.0.1
# 泛域名解析,访问任何 baidu.com 域名都会被解析到 6.6.6.6
address=/baidu.com/6.6.6.6
# 包含的配置文件,可以指定目录、具体文件、文件通配符等,包含多个目录或文件可以用英文逗号分隔
conf-dir=
启用并立即启动服务
systemctl enable --now dnsmasq
查看dns服务器状态
systemctl status dnsmasq
安装网络工具箱,包含nslookup工具,可以用来检查dns
yum install bind-utils -y
客户端配置
CentOS7之后的版本,dns服务器配置文件由systemd-resolved这个系统服务控制,直接修改/etc/resolv.conf无效,过一会就会被重置
这个服务的配置文件是 /etc/systemd/resolved.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details
[Resolve]
DNS=192.168.0.1
#FallbackDNS=8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844
#LLMNR=yes
修改完成后重启服务
systemctl restart systemd-resolved
cat /etc/resolv.conf 查看修改结果
安装网络工具箱,包含nslookup工具,可以用来检查dns
yum install bind-utils -y
nslookup baidu.com 查看结果为 6.6.6.6,说明配置成功
阿里云服务器因为使用了cloud-init服务,网络配置会被cloud-init重置,所以无法通过修改/etc/systemd/resolved.conf来达成配置目的
我们可以通过锁定/etc/resolv.conf文件的方式来强制修改dns配置
mv /etc/resolv.conf /tmp/resolv.conf.bak #备份文件
vi /etc/resolv.conf #修改文件
nameserver 192.168.0.1
chattr +i /etc/resolv.conf #锁定文件
DNS使用53端口,注意放行端口