9

(this issue went away for me with the 10.7.1 update - /etc/hosts now works as always for me)

I've updated my Mac to Lion and now I notice that /etc/hosts is consulted last, even after DNS. This is very annoying as I have a lot of hostnames in there that I use for development.

Where is the name resolution order configured? I can check it using dscacheutil, so here's what a Snow Leopard machine tells me:

pilif@tali ~ % dscacheutil -configuration
DirectoryService Cache search policy:
    /Local/Default
    /BSD/local

Settings:
AAAA Queries  - Disabled (link-local IPv6 addresses)
Default TTL   - 3600
Policy Flags  - 0

And here is what Lion tells me

pilif@kosmos ~ % dscacheutil -configuration
DirectoryService Cache search policy:
    /Local/Default

Unable to get details from the cache node
Unable to get cache configuration information

aside of the two errors, I would assume that /BSD/Local is what makes it read /etc/hosts earlier.

Does anybody have any idea where this "Cache search policy" is stored and how to change it back?

I know that I can create host name entries using dcsl, but I'd really like to keep my /etc/hosts which I use on various machines.

Update: The resolution order can apparently be configured in the directory Utility. Unfortunately, this installations Directroy Utility doesn't list the BSD files any more in the Services tab.

Is this feature gone from Lion? Or is this installation hosed?

6 Answers 6

5

I solved the problem (and thus posting as an answer instead of amending the question):

The BSD files are indeed not listed in Directory Utility, nor in dscacheutil any more, but at least /etc/hosts is still read, but there is a problem in that multiple host names per IP address don't seem to be supported anymore or at least, they don't work right ATM.

When your old /etc/hosts could have looked like

127.0.0.1 localhost foo foobar

This would cause the ~10 second wait time to resolve any of these host names.

But if you use

127.0.0.1 localhost
127.0.0.1 foo
127.0.0.1 foobar

Resolution will be instant.

RedGrittyBrick's answer is also valid, but I specifically want to continue to use the hosts file over modifying the local directory as it's shared between various development machines of mine.

To answer the rest of my questions too (now all is clear to me):

  • The cache resolution order you configure in the directory utility where you can tell it which of the enabled directories you want to look at in what order.
  • To configure directories, also use the directory utility
  • The directory utility is launched by going to System Preferences > Accounts > Login Options > Join Directory > Directory Utility
  • In Lion, the BSD Files "directory" isn't available any more even though the help file still refers to it
  • As I said, /etc/hosts is still read, but there's the bug I described above.
3
  • Putting each entry on its own line didn't solve it for me. Can you visit "foo" and "foobar" in both Chrome and Safari? For me, it only works in Chrome. Can you override an existing website, e.g. "127.0.0.1 www.google.com"? For me, that doesn't work in either Chrome or Safari. Jul 22, 2011 at 18:45
  • This resolved the 10s wait for me. Thank you, that was driving me crazy. Nov 10, 2011 at 18:04
  • Resolved it for me on 10.7.3 yay!
    – EMiller
    Apr 24, 2012 at 19:29
3

The matter is Lion handles .local TLD differently because it's reserved for some Multicast DNS features (used by Bonjour). The only way i found to solve this issue is using a different TLD for development hosts (ie: .dev). It works fine for me, hope it's gonna be helpful to others!

1
  • this was absolutely what was happening on my box. thx
    – slf
    Mar 12, 2013 at 17:13
1

As I expect you know, the traditional Unix way to handle this is by using a hostresorder or order directive in /etc/resolv.conf. OS X does (or can be made to) read and use these files but OS X has a separate system administered through network preferences which I believe overwrites these configuration files at bootup.

http://hints.macworld.com/article.php?story=20070223050607406

http://docs.info.apple.com/article.html?path=ServerAdmin/10.6/en/od4939886e.html

https://discussions.apple.com/thread/2493759

http://blog.daemon.com.au/go/blog-post/managing-the-host-file-on-leopard

This doesn't answer your question but the info and links may help find one. I'd have put this in a comment if I could condense it down to fit.

1
  • I know about dscl, but I specifically wanted to use the hosts file which can't be explicitly turned on and off in Lion any more. It's always read, but with different parsing rules than before - see my answer to my question.
    – pilif
    Jul 21, 2011 at 16:24
1

It's possible to work around this problem by using dnsmasq as a local DNS and adding entries there, or use dnsmasq to use /etc/hosts.

It's possible to use a script to set the DNS server order:

Filename: setdsn
-------------------------------------------------
#!/bin/bash

# Script is used to set the Nameserver Lookup under Max OS X 10.4 with the Console
# Script by Stephan Oeste

if [ $# -lt 2 ] ; then
echo "Use: $0 [2.Nameserver]"
echo "Example Use: $0 example.tld 1.2.3.4 1.2.3.5"
exit 1
fi

PSID=$( (scutil | grep PrimaryService | sed -e 's/.*PrimaryService : //')<< EOF
open
get State:/Network/Global/IPv4
d.show
quit
EOF
)

scutil << EOF
open
d.init
d.add ServerAddresses * $2 $3
d.add DomainName $1
set State:/Network/Service/$PSID/DNS
quit
EOF
-------------------------------------------------

Create the file:

chmod +x setdns

And then use ist with (Example): setdns domain.com 12.23.34.45
(Posted by emzy on http://hints.macworld.com/article.php?story=20050621051643993)

If you want the script to automatically load on network change, you should create a .plist, put it in /Library/LaunchDaemons and use:

sudo launchctl load -w /LibraryLaunchDaemons/name.your.plist
1

I ran into this issue in Snow Leopard while trying to set up a transparent Software Update Server. I've gotten it working on Lion now as well. The Software Update Server itself is halfway between a hack and a kludge, but this issue was solved pretty elegantly. Here's what I know:

  • /etc/hosts does exist in Lion and like recent OS X versions is set to be read after DNS.
  • /etc/resolv.conf exists in Lion but is a symlink to /var/run/resolv.conf.
  • /var/run/resolv.conf is rewritten anytime your network configuration is updated. That can be due to restarts, DHCP lease renewals, etc..

I created the following script. /usr/local/hostsBind:

mv /var/run/resolv.conf /var/run/resolv.conf.new
echo order hosts, bind > /var/run/resolv.conf
cat /var/run/resolv.conf.new >> /var/run/resolv.conf

this backup the current main resolv.conf file, creates a new one with the desired order of hosts before BIND, and concatenates the previous file to the end.

I call this script by watching main resolv.conf file with the following launched job at /Library/LaunchDaemons/com.domain.hostsBind.plist (you can change com.domain to something that makes sense for you):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.domain.hostsBind</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/hostsBind</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/var/run/resolv.conf</string>
    </array>
</dict>
</plist>

This has been working for our organization with a Lion Software Update server.

Last thing to note, this works just fine with Snow Leopard as well if you change the path for resolv.conf to /etc/resolv.conf. Lion just threw the curveball of the symlink to /var/run/ instead of /etc/.

-b

P.S.: Source for the script: http://forums.macrumors.com/showthread.php?p=6742920

1
  • when I tried this, the job kept prepending order on the file because changing the file triggered a new update.
    – harmanjd
    Apr 22, 2013 at 18:52
1

There are a number of things wrong with the apache-server that ships with OSX Lion.

  1. A lot of modules are loaded by default.
    Open up /etc/apache2/httpd.conf and go through the list of enabled modules (all of them are enabled by default). You can probably turn off a lot of them by putting a # in front of the line.

    These are the ones I turned off:

#LoadModule authn_dbm_module libexec/apache2/mod_authn_dbm.so
#LoadModule authn_anon_module libexec/apache2/mod_authn_anon.so
#LoadModule authn_dbd_module libexec/apache2/mod_authn_dbd.so
#LoadModule authn_default_module libexec/apache2/mod_authn_default.so
#LoadModule authz_groupfile_module libexec/apache2/mod_authz_groupfile.so
#LoadModule authz_user_module libexec/apache2/mod_authz_user.so
#LoadModule authz_dbm_module libexec/apache2/mod_authz_dbm.so
#LoadModule authz_owner_module libexec/apache2/mod_authz_owner.so
#LoadModule authz_default_module libexec/apache2/mod_authz_default.so
#LoadModule auth_basic_module libexec/apache2/mod_auth_basic.so
#LoadModule auth_digest_module libexec/apache2/mod_auth_digest.so
#LoadModule dbd_module libexec/apache2/mod_dbd.so
#LoadModule mime_magic_module libexec/apache2/mod_mime_magic.so
#LoadModule unique_id_module libexec/apache2/mod_unique_id.so
#LoadModule proxy_connect_module libexec/apache2/mod_proxy_connect.so
#LoadModule proxy_ftp_module libexec/apache2/mod_proxy_ftp.so
#LoadModule proxy_scgi_module libexec/apache2/mod_proxy_scgi.so
#LoadModule proxy_ajp_module libexec/apache2/mod_proxy_ajp.so
#LoadModule dav_module libexec/apache2/mod_dav.so
#LoadModule dav_fs_module libexec/apache2/mod_dav_fs.so
#LoadModule bonjour_module libexec/apache2/mod_bonjour.so
#LoadModule fastcgi_module libexec/apache2/mod_fastcgi.so
  1. The hosts-file is a not perfect.
    Now open up your hosts file, located at /etc/hosts. Here you will see this line (and some comments):

127.0.0.1 macbook-pro-van-bart.local

(other computername off course). Add the following line (change the computername):

fe80::1%lo0 macbook-pro-van-bart.local
  1. Take a look at your apache config
    Make sure your vhosts do not rely on IPs. Change them like this:

NameVirtualHost 127.0.0.1:80

Becomes:

NameVirtualHost *:80

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .