Dnsmasq 将本地主机名解析为全网 127.0.0.1

kol*_*pto 9 dnsmasq

我的 dnsmasq 配置如下:

  • 在主机“rtfm.lan”上
  • 域“lan”
  • 有一些 DHCP 预设包括主机名

所有通过 DHCP 注册的“*.lan”主机都可以很好地解析:dnsmasq 知道哪些 IP 分配给了哪些主机名。但是,'rtfm.lan' 是从 /etc/hosts 读取的并解析为 127.0.0.1。

我可以阻止 dnsmasq 读取 /etc/hosts 并手动添加地址:

no-hosts
address=/rtfm.lan/192.168.1.2
Run Code Online (Sandbox Code Playgroud)

但这并不灵活:如果有一天我决定更改我的 IP — 我也必须在 dnsmasq.conf 中更改它。

如何告诉 dnsmasq 使用具有真实 IP 的本地主机名?


以防万一.. 这是我的 DNSmasq 配置:

# Never forward plain names (without a dot or domain part)
domain-needed
# Never forward addresses in the non-routed address spaces.
bogus-priv
# Read resolv.conf serially
strict-order

#==========[ NAMESERVER ]==========#

# Cache size
cache-size=4096
# Don't read /etc/hosts
no-hosts
# Read additional hosts-file (not only /etc/hosts) to add entries into DNS
addn-hosts=/etc/hosts-dnsmasq
# Auto-append <domain> to simple entries in hosts-file
expand-hosts

#=== HOSTNAME OVERRIDES
address=/localhost/127.0.0.1 # *.localhost => 127.0.0.1

#==========[ DHCP ]==========#
# Enable for the local network?
dhcp-authoritative
# Tell MS Windows to release a lease on shutdown
dhcp-option=vendor:MSFT,2,1i

#=== DHCP
# Domain name
domain=lan
# DNS-resolve hosts in these domains ONLY from /etc/hosts && DHCP leases
local=/lan/

# DHCP range & lease time
dhcp-range=192.168.1.70,192.168.1.89,24h 
# Default route
dhcp-option=3,192.168.1.1

#=== FIXED LEASES
# LAN MY HOSTS
dhcp-host=00:23:54:5d:27:fa,                    rtfm.lan,               192.168.1.2
dhcp-host=00:23:54:5d:27:fb,                    rtfm.lan,               192.168.1.2
dhcp-host=c8:0a:a9:45:f1:03, 00:1e:64:9e:e9:5e, wtf.lan,                192.168.1.3
Run Code Online (Sandbox Code Playgroud)

Bil*_*hor 10

通常,您会在文件中定义静态主机/etc/hosts并启用。 允许您为此文件指定备用名称。hostsdnsmasq.confdnsmasq

如果您想rtfm.lan被寻址,192.168.1.2则添加一行读数192.168.1.2 rtfm.lan/etc/hosts. 通常,您的 dnsmasq 主机文件应该可以移植到您的所有服务器。

rtfm.lanIP 堆栈上将使路由短路,并且不会将流量发送到网络上。

如果您使用信号更新/etc/hosts文件和信号,主机文件将被重新读取并应用更改。dnsmasqHUP

编辑:dnsmasq 并非旨在为其自己的主机提供动态主机地址。如前所述,它确实从其租用文件中为 DHCP 客户端提供名称。

如果您的主机是 Internet 网关,则它通常会提供本地静态地址。提供 Internet 网关地址可能会导致路由和防火墙问题。

您可以将 DHCP 客户端配置为编写一行主机文件。该的dnsmasq选项addn-host可用于获取的dnsmasq阅读除了这个文件/etc/hosts。如果地址可能会更改,则 DHCP 客户端可以重写文件并向 dnsmasq 发送HUPIP 地址更改信号。


小智 6

dnsmasq 可以通过禁用从 /etc/hosts 读取并配置另一个配置文件以读取包括自身在内的主机名来解析自己的主机名。

dnsmasq 配置(/etc/dnsmaq.conf):

no-hosts
addn-hosts=/etc/dnsmasq_hosts
Run Code Online (Sandbox Code Playgroud)

在 /etc/dnsmasq_hosts 中,配置自己的主机名指向一个需要的 IP 地址(以及本地局域网中的其他主机名)。