Dnsmasq Reads /etc/hosts and Ignores addn-hosts Configuration
In a Fedora 39 machine with the NetworkManager plugin, the dnsmasq service is often used for DNS caching and providing DHCP services. By default, dnsmasq reads the /etc/hosts file for hostname resolution, but it may ignore the addn-hosts configuration specified in the /etc/dnsmasq.conf file.
Understanding /etc/hosts
The /etc/hosts file is a simple text file that contains mappings between IP addresses and hostnames. It is used for hostname resolution before querying DNS servers, providing faster lookups for frequently accessed hosts.
# Example /etc/hosts file
127.0.0.1 localhost
192.168.1.100 server1.example.com server1
Understanding /etc/dnsmasq.conf
The /etc/dnsmasq.conf file contains the configuration options for the dnsmasq service. One of the options is the addn-hosts directive, which specifies a file containing additional hostname mappings.
# Example /etc/dnsmasq.conf file
user=dnsmasq
group=dnsmasq
interface=lo
bind-interfaces
addn-hosts=/path/to/additional/hosts.file
dnsmasq Ignores addn-hosts Configuration
When the dnsmasq service is started via the NetworkManager plugin, it is possible that the addn-hosts configuration in /etc/dnsmasq.conf is ignored. This is due to the NetworkManager service overriding the dnsmasq configuration options.
Solution: Configure NetworkManager to Use /etc/dnsmasq.conf
To ensure that dnsmasq reads the addn-hosts configuration, you need to configure the NetworkManager service to use the /etc/dnsmasq.conf file.
# Edit /etc/NetworkManager/NetworkManager.conf
[main]
dns=dnsmasq
After editing the /etc/NetworkManager/NetworkManager.conf file, restart the NetworkManager and dnsmasq services for the changes to take effect.
sudo systemctl restart NetworkManager
sudo systemctl restart dnsmasq
- By default,
dnsmasqreads the/etc/hostsfile for hostname resolution. - The
addn-hostsdirective in/etc/dnsmasq.confspecifies a file containing additional hostname mappings. - When
dnsmasqis started via the NetworkManager plugin, theaddn-hostsconfiguration may be ignored. - To ensure that
dnsmasqreads theaddn-hostsconfiguration, configure theNetworkManagerservice to use the/etc/dnsmasq.conffile.