Mac OSX changing /etc/hosts has no effect even after killing mDNSResolver
Categories:
Troubleshooting /etc/hosts on macOS: When Changes Don't Take Effect

Discover why your /etc/hosts file modifications might not be working on macOS, even after restarting mDNSResponder, and learn effective solutions.
Modifying the /etc/hosts
file is a common practice for developers and system administrators to map domain names to specific IP addresses locally. This is particularly useful for testing websites before DNS propagation, blocking unwanted domains, or redirecting services. However, macOS users often encounter a frustrating issue: changes made to /etc/hosts
don't seem to take effect, even after attempting to flush the DNS cache by killing the mDNSResponder
process. This article delves into the common causes of this problem and provides comprehensive solutions to ensure your host file changes are recognized.
Understanding macOS DNS Resolution
Before troubleshooting, it's crucial to understand how macOS handles DNS resolution. Unlike some other operating systems, macOS uses a complex system that involves multiple layers, including mDNSResponder
(for Bonjour/mDNS and DNS caching), configd
(for network configuration), and the standard /etc/hosts
file. When you type a hostname into your browser or terminal, the system consults these various sources in a specific order to resolve the IP address.
flowchart TD A[Application Request Hostname] --> B{macOS DNS Resolver Stack} B --> C{Check /etc/hosts} C -- Hostname Found --> D[Return IP from /etc/hosts] C -- Hostname Not Found --> E{Query mDNSResponder Cache} E -- Hostname Found --> F[Return IP from Cache] E -- Hostname Not Found --> G{Query Configured DNS Servers} G --> H[Return IP from DNS Server] D --> I[Resolve Complete] F --> I H --> I
Simplified macOS DNS Resolution Flow
The /etc/hosts
file is typically the first place the system looks for a hostname-to-IP mapping. If an entry is found, that IP address is used immediately, bypassing external DNS servers. If not, the request proceeds to the mDNSResponder
cache and then to the configured DNS servers. The key here is ensuring that mDNSResponder
and other system services are aware of the changes to /etc/hosts
.
Common Causes and Solutions
Several factors can prevent your /etc/hosts
changes from taking effect. Understanding these can help you pinpoint the exact issue.
sudo
when editing /etc/hosts
to ensure you have the necessary permissions. Incorrect permissions can prevent the system from reading the file.1. Incorrect File Editing or Syntax Errors
The most basic issue is often a mistake in editing the file itself. /etc/hosts
requires a specific format: IP_ADDRESS HOSTNAME [ALIAS]
. Each entry should be on a new line. Comments start with #
.
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 mylocalwebsite.dev # My local development site
192.168.1.10 myotherserver.local
Example of a correctly formatted /etc/hosts
file.
2. DNS Cache Not Flushed Properly
While killing mDNSResponder
is a common suggestion, it might not always be sufficient or correctly executed. macOS has multiple layers of DNS caching. Simply killing the process might not clear all caches or might not trigger a full reload of the hosts file by all relevant services.
1. Flush the DNS Cache (Recommended Method)
The most reliable way to flush the DNS cache on modern macOS versions is using the dnscacheutil
or dscacheutil
command. The specific command varies slightly by macOS version.
2. For macOS 10.10.4 (Yosemite) and later:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
This command flushes the dscacheutil
cache and then sends a HUP signal to mDNSResponder
, which forces it to reload its configuration and caches without fully restarting it.
3. For macOS 10.7 (Lion) to 10.10.3 (Yosemite):
sudo discoveryutil mdnsflushcache; sudo discoveryutil udnsflushcaches
4. For macOS 10.5 (Leopard) to 10.6 (Snow Leopard):
sudo dscacheutil -flushcache
3. Browser or Application-Level Caching
Even after flushing the system DNS cache, your web browser or other applications might have their own internal DNS caches. This is a very common reason for persistent issues.
4. Network Configuration Issues
Sometimes, network configuration changes or VPNs can interfere with how /etc/hosts
is processed. If you're using a VPN, try disabling it temporarily to see if your changes take effect.
1. Renew DHCP Lease
Renewing your DHCP lease can sometimes help refresh network settings. Go to System Settings
> Network
, select your active network interface (e.g., Wi-Fi or Ethernet), click Details...
, then TCP/IP
, and click Renew DHCP Lease
.
2. Check DNS Server Order
Ensure that your local DNS resolution (which includes /etc/hosts
) is prioritized. While /etc/hosts
is usually checked first, misconfigurations can sometimes alter this. You can inspect your current DNS configuration using scutil --dns
in the terminal.
5. Permissions and Ownership
Incorrect file permissions or ownership on /etc/hosts
can prevent the system from reading it. The file should typically be owned by root
and have read-only permissions for others.
ls -l /etc/hosts
# Expected output: -rw-r--r-- 1 root wheel 248 Oct 26 10:00 /etc/hosts
Checking permissions and ownership of /etc/hosts
.
1. Correct Permissions (if needed)
If the permissions are incorrect, you can fix them using:
sudo chmod 644 /etc/hosts
sudo chown root:wheel /etc/hosts
2. Verify File Integrity
Ensure the file is not corrupted or has hidden characters. Opening it in a plain text editor like nano
or vi
is usually safe. Avoid rich text editors.
Verifying Your Changes
After applying any of the solutions, it's essential to verify that your /etc/hosts
changes are now active.
1. Use ping
Open Terminal and type ping mylocalwebsite.dev
(replace with your hostname). If it resolves to the IP address you specified in /etc/hosts
, it's working.
2. Use nslookup
or dig
These tools query DNS servers directly, but nslookup
can sometimes show local resolution. nslookup mylocalwebsite.dev
might show Server: 127.0.0.1
if it's resolving locally. For dig
, use dig mylocalwebsite.dev
and look for the ANSWER SECTION
.
3. Test in Browser
Open your web browser and navigate to the hostname you configured. If it loads the expected content, your changes are successful.
By systematically going through these troubleshooting steps, you should be able to resolve issues with /etc/hosts
not taking effect on your macOS system. Remember that patience and a methodical approach are key when dealing with network and DNS configurations.