/etc/dnsmasq.conf. To resolve this, you must configure the addn-hosts directive within the NetworkManager-specific settings in /etc/NetworkManager/dnsmasq.d/ rather than editing global dnsmasq files. This ensures that your local DNS overrides are correctly loaded and respected by the active resolver service.
Understanding the Configuration Conflict
On modern Fedora systems, the dnsmasq package is often installed as a dependency for NetworkManager, but it does not run as a standalone daemon by default. Instead, NetworkManager spawns its own instance of dnsmasq to handle local DNS resolution for connected networks. When you edit the standard /etc/dnsmasq.conf file, those changes are ignored because the NetworkManager-managed instance reads from a different set of configuration paths.
The core issue arises when users attempt to add local hostname mappings using the addn-hosts directive in the main configuration file. Since the NetworkManager plugin initializes its own environment, it does not source the global /etc/dnsmasq.conf unless explicitly configured to do so. Consequently, any hosts defined in /etc/hosts might be read if the system falls back to standard resolution, but custom addn-hosts files specified in the global config will be silently skipped. This behavior is not a bug but a feature of how NetworkManager isolates its DNS management to prevent conflicts with other services.
Locating the Correct Configuration Directory
To fix this, you need to place your configuration in the directory specifically designated for NetworkManager’s dnsmasq plugins. On Fedora, this directory is typically /etc/NetworkManager/dnsmasq.d/. Files placed here are automatically read by the NetworkManager service when it starts or restarts its DNS components. Unlike the main configuration file, which may be overwritten or ignored during updates, these drop-in files are persistent and managed directly by the NetworkManager ecosystem.
You should create a new configuration file in this directory, such as local-dns.conf. This file will contain the specific directives needed to include additional host files. By keeping these settings separate from the global dnsmasq configuration, you ensure that your local DNS rules are applied correctly regardless of how NetworkManager manages its internal state. This approach aligns with Fedora’s design philosophy of modular network configuration.
Creating the Host File
First, create the file that contains your actual host mappings. You can use any filename you prefer, but placing it in a logical location like /etc/NetworkManager/hosts keeps things organized. For example, add lines mapping IP addresses to hostnames:
192.168.1.10 myserver.local
192.168.1.11 mydesktop.local
Ensure that the file permissions allow NetworkManager to read them. Typically, root ownership with 644 permissions is sufficient. Once the file is created and populated with your desired mappings, you can proceed to link it via the dnsmasq configuration.
Configuring NetworkManager to Use Additional Hosts
Navigate to the /etc/NetworkManager/dnsmasq.d/ directory and create a new configuration file, for instance, custom-hosts.conf. Inside this file, add the addn-hosts directive pointing to the path of your host file created in the previous step. The syntax is straightforward:
addn-hosts=/etc/NetworkManager/hosts
This directive tells the NetworkManager-managed dnsmasq instance to load the specified file in addition to the standard /etc/hosts file. It is crucial to use the absolute path to avoid any ambiguity. After saving this configuration file, you must restart the NetworkManager service to apply the changes. This can be done using the command sudo systemctl restart NetworkManager.
If you have other related networking issues, such as connection stability problems, you might find resources on Troubleshooting Network-Shared Hard Drive Connection Issues with Samba helpful for broader context, though this specific article focuses on DNS resolution.
Verifying the Configuration
After restarting NetworkManager, verify that the new configuration is being loaded. You can check the status of the NetworkManager service to ensure there are no errors related to DNS configuration. Additionally, you can test the resolution by using tools like dig or nslookup against localhost or your local gateway. For example, running dig myserver.local @127.0.0.53 should return the IP address you defined in your host file.
If the resolution fails, check the journal logs for NetworkManager using journalctl -u NetworkManager to see if dnsmasq encountered any issues loading the additional hosts file. Common mistakes include typos in the file path or incorrect permissions preventing dnsmasq from reading the file. Ensure that the path in the addn-hosts directive exactly matches the location of your host file.
Alternative Approaches and Considerations
While configuring addn-hosts via NetworkManager is the recommended method for most users, some advanced setups might require direct interaction with the dnsmasq process. However, this is generally discouraged on Fedora as it can lead to conflicts with NetworkManager’s automatic configuration. If you are experiencing other system issues, such as hardware recognition problems, you might refer to guides like Fedora 40 Doesn't Recognize USB 3.0 Ports on Gigabyte Motherboard for troubleshooting methodologies that apply to similar system-level configurations.
Another consideration is the use of nss-myhostname, which allows you to resolve hostnames without modifying dnsmasq at all. If your hosts are only used locally and do not need to be resolved by other devices on the network, adding entries to /etc/hostname or using nss-myhostname might be a simpler solution. However, for full local DNS override capabilities, the addn-hosts method remains the most robust and standard approach.
Frequently Asked Questions
Why doesn't /etc/dnsmasq.conf work on Fedora?
Fedora uses NetworkManager to manage DNS, which runs its own dnsmasq instance that ignores the global configuration file. You must place configuration files in /etc/NetworkManager/dnsmasq.d/ for them to be recognized by the active DNS resolver.
Can I use /etc/hosts directly instead of addn-hosts?
Yes, dnsmasq automatically reads /etc/hosts by default. However, using addn-hosts allows you to keep your custom mappings separate from the system-wide hosts file, making management easier and reducing the risk of accidental modifications.
How do I reload the configuration without restarting the whole system?
You can restart just the NetworkManager service by running sudo systemctl restart NetworkManager. This forces the service to re-read its configuration files, including those in the dnsmasq.d directory, without requiring a full system reboot.
What if my changes don't take effect after restarting?
Check the file permissions to ensure dnsmasq can read the host file. Also, verify the path in the addn-hosts directive is correct and absolute. Use journalctl -u NetworkManager to look for error messages related to loading the configuration.
Does this affect DNS resolution for other users on the network?
No, this configuration only affects the local machine’s DNS resolution. To share local hostnames with other devices, you would need to configure your router or a central DNS server, not just the local dnsmasq instance on one Fedora machine.