router.local that automatically updates to its current LAN IP address, regardless of whether the address is assigned via DHCP or remains static.
Understanding Dynamic Hostname Resolution
In many home and small office networks, devices obtain their IP addresses dynamically through DHCP. While this simplifies network management, it creates a challenge for local services that rely on hostnames rather than numerical IPs. If your router’s IP changes from 192.168.1.10 to 192.168.1.15, any service configured to connect to 192.168.1.10 will break. Furthermore, simply using a static/etc/hosts entry is insufficient because it cannot update when the underlying IP changes.
The solution lies in leveraging DNSmasq’s ability to map hostnames to DHCP leases. By configuring DNSmasq correctly, you can ensure that queries for specific hostnames are resolved against the most recent IP address recorded in the DHCP lease file. This approach works seamlessly for both the gateway device itself and other clients on the network that register their hostnames with the DHCP server. It eliminates the need for complex scripts or external dynamic DNS services when dealing with local LAN resolution.
Configuring Static Mappings for Local Devices
For devices that should always resolve to a specific name, even if they change IPs, you can use thedhcp-host directive within the DNSmasq configuration file. This is particularly useful for critical infrastructure components like routers, NAS drives, or printers. The syntax allows you to bind a MAC address to a fixed IP and a hostname, ensuring that whenever that device connects, it receives the same IP and registers the correct name.
Open your primary DNSmasq configuration file, typically located at /etc/dnsmasq.conf or inside the /etc/dnsmasq.d/ directory depending on your Linux distribution. Add a line that specifies the MAC address, the desired IP, and the hostname. For example:
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.1.10,router
This command tells DNSmasq that any device with the MAC address aa:bb:cc:dd:ee:ff should be assigned the IP 192.168.1.10 and should be known as router. When combined with the local option, this ensures that queries for router are answered locally without forwarding to upstream DNS servers. You must also ensure that the domain option is set to your local suffix, such as .lan or .local, so that fully qualified domain names like router.lan resolve correctly.
Leveraging DHCP Lease Tracking
While static mappings are powerful, they require manual intervention if a device’s MAC address changes or if you want to support arbitrary devices without pre-configuring them. A more flexible approach involves allowing DNSmasq to track DHCP leases and respond to queries based on those records. This is especially relevant for client devices that join the network sporadically. Ensure that your DNSmasq configuration includes thedhcp-leasefile directive. This points to the file where DNSmasq stores active leases, usually /var/lib/misc/dnsmasq.leases. When a device obtains an IP, it sends a hostname during the DHCP handshake. DNSmasq captures this and associates the hostname with the IP in its internal database. To make these resolutions available to your network, you must enable the local option and define your local domain.
local=/lan/
domain=lan
dhcp-range=192.168.1.100,192.168.1.200,12h
With this setup, any device on the 192.168.1.0/24 network that registers a hostname (like my-laptop) will be resolvable as my-laptop.lan. If the laptop’s IP changes due to a lease renewal, the next query for my-laptop.lan will return the new IP. This dynamic behavior is handled entirely by DNSmasq’s runtime state, requiring no additional scripting.
Verifying and Troubleshooting
After applying changes, restart the DNSmasq service to load the new configuration. On systemd-based systems, usesudo systemctl restart dnsmasq. Verify that the service is running and listening on port 53 by checking the status or logs. You can test resolution using standard tools like dig or nslookup. For instance, querying dig router.lan @127.0.0.1 should return the IP address you configured.
If a hostname does not resolve, check two main areas. First, confirm that the device has successfully registered its hostname with the DHCP server. Some operating systems, particularly Windows, may not send a hostname during DHCP requests unless specifically configured to do so. Second, ensure that your local resolver is pointing to the machine running DNSmasq. Clients must have their DNS settings configured to use the local DNSmasq instance, otherwise, queries will go to upstream providers which do not know about your local dynamic hostnames.
For deeper insights into managing multiple DNS records across different servers, consider exploring how to handle complex routing scenarios. See Managing Multiple DNS Records on One Domain with Different DNS Servers for advanced configurations. Additionally, understanding how router caching affects these lookups can help troubleshoot stale entries. Read Understanding Router DNS Caching: Shared Among Users on a LAN to optimize performance.
Frequently Asked Questions
Why does my device’s hostname stop resolving after a reboot?
This usually happens because the device failed to send its hostname during the DHCP handshake. Many OSes require specific settings to advertise their NetBIOS or DNS name. Check the network adapter settings on the client to ensure "Register this connection's addresses in DNS" is enabled.
Can I use .local domains with DNSmasq?
Yes, but you must be careful with mDNS conflicts. DNSmasq supports .local domains, but Apple’s Bonjour/mDNS also uses this namespace. If you experience conflicts, consider using .lan or .home instead, or configure DNSmasq to ignore mDNS packets for those domains.
How do I clear stale DNS entries for a changed IP?
DNSmasq automatically updates its internal cache when a new DHCP lease is issued. However, clients may cache old responses. Restarting the DNSmasq service clears its own cache. For clients, flushing the local DNS cache (e.g., ipconfig /flushdns on Windows) forces them to query again and receive the updated IP.
Does this work for IPv6 addresses?
Yes, DNSmasq handles IPv6 similarly. Ensure your DHCP range includes IPv6 prefixes and that devices are advertising their IPv6 hostnames. The dhcp-host directive can also specify IPv6 addresses, allowing for consistent hostname resolution across both IP versions.
What if I don’t know the MAC address of a device?
You can find the MAC address by checking the DHCP lease file directly (cat /var/lib/misc/dnsmasq.leases). Once you identify the MAC associated with the current IP, you can add a static mapping to guarantee that IP and hostname combination persists across reboots.