When working with OS X Mountain Lion (10.8), many developers get confused by the traditional Unix DNS resolution mechanisms not behaving as expected. The key revelation comes from that /etc/resolv.conf
notice:
#
# Mac OS X Notice
#
# This file is not used by the host name and address resolution
# or the DNS query routing mechanisms used by most processes on
# this Mac OS X system.
#
# This file is automatically generated.
#
Instead of resolv.conf
, macOS uses its own system configuration daemon (configd
) and the System Configuration framework for DNS resolution. The primary files controlling this are:
/etc/hosts
- Still used for static entries/System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
- The mDNS responder- Network preference pane settings
For your specific case where host servername
isn't resolving an entry from /etc/hosts
, here's how to fix it:
# 1. Edit your hosts file (requires sudo)
sudo nano /etc/hosts
# 2. Add your entry (example format)
192.168.1.100 servername
# 3. Flush the DNS cache (OS X Mountain Lion specific)
dscacheutil -flushcache
killall -HUP mDNSResponder
The host
command might not always respect the hosts file. Try these alternatives:
# Using dig (more reliable for testing)
dig servername
# Using nslookup
nscutil --dns
nslookup servername
# Using the system resolver directly
python -c "import socket; print(socket.gethostbyname('servername'))"
For system-wide DNS server configuration (instead of just hosts file entries):
# View current DNS configuration
scutil --dns
# Set DNS servers via command line (example)
networksetup -setdnsservers Wi-Fi 8.8.8.8 8.8.4.4
If you're still having issues:
- Check permissions on
/etc/hosts
(should be 644) - Verify no trailing spaces or malformed entries
- Test with different tools as shown above
- Restart mDNSResponder:
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
followed by load
If you've checked your /etc/resolv.conf
on OS X Mountain Lion, you might have noticed this message:
#
# Mac OS X Notice
#
# This file is not used by the host name and address resolution
# or the DNS query routing mechanisms used by most processes on
# this Mac OS X system.
#
# This file is automatically generated.
#
This explains why your manual DNS changes in this file don't work. OS X uses a different system for name resolution.
OS X uses the scutil
(System Configuration Utility) to manage DNS settings. Here's how to view your current DNS configuration:
scutil --dns
For network interface-specific settings:
scutil --nwi
To add a custom host entry that will work system-wide:
- Edit
/etc/hosts
(requires sudo):
sudo nano /etc/hosts
Add your entry like:
192.168.1.100 myserver.local
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
The host
command bypasses the local hosts file by default. To test your entry, use:
ping myserver.local
Or use dscacheutil
to query the cache:
dscacheutil -q host -a name myserver.local
For more control, create a custom resolver:
sudo mkdir /etc/resolver
sudo nano /etc/resolver/mydomain
Add content like:
nameserver 8.8.8.8
search mydomain.local
Check all name resolution sources:
nslookup myserver.local
dig myserver.local
Remember that changes might take a few moments to propagate through all system components.