Dnsmasq resolves local hostname to 127.0.0.1 all over the net

My dnsmasq is configured:

  • On host 'rtfm.lan'
  • Domain 'lan'
  • There are some DHCP presets that include hostnames

All '*.lan' hosts registered via DHCP do resolve fine: dnsmasq known which IPs are assigned to which hostnames. However, 'rtfm.lan' is read from /etc/hosts and resolve to 127.0.0.1.

I can prevent dnsmasq from reading /etc/hosts and add an address manually:

no-hosts
address=/rtfm.lan/192.168.1.2

But this is not flexible: if one day I decide to change my IP — i'll also have to change it in dnsmasq.conf.

How can I tell dnsmasq to use local-hostname with its real IP?


Just in case.. here's my DNSmasq config:

# 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

Solution 1:

Normally, you would define your static hosts in /etc/hosts and enable hosts in your dnsmasq.conf file. dnsmasq allows you to specify an alternate name for this file.

If you want rtfm.lan to be addressed as 192.168.1.2 then add a line reading 192.168.1.2 rtfm.lan to /etc/hosts. Normally, your hosts file for dnsmasq should be portable to all your servers.

On rtfm.lan the IP stack will short-circuit the routing and not send traffic onto the network.

If you update your /etc/hosts file and signal dnsmasq with a HUP signal, the hosts file will be reread and changes applied.

EDIT: dnsmasq is not designed to serve up a dynamic host address for its own host. As noted it does serve up names from its lease file for DHCP clients.

If your host is an Internet gateway, it would normally serve a local static address. Serving up the Internet gateway address may cause routing and firewall issues.

You could configure your DHCP client to write a one line hosts file. The dnsmasq option addn-host can be used to get dnsmasq to read this file in addition to /etc/hosts. If the address is likely to change, then the DHCP client could rewrite the file and send dnsmasq a HUP signal on IP address change.

Solution 2:

dnsmasq can resolve its own hostname by disabling reading from /etc/hosts and configuring another config file for reading the hostnames including itself.

dnsmasq configuration (/etc/dnsmaq.conf):

no-hosts
addn-hosts=/etc/dnsmasq_hosts

In /etc/dnsmasq_hosts, configure own hostname point to ip address one needs (as well other hostnames in local lan).