/etc/hosts priority over dns in leopard

Discussion in 'macOS' started by tannn, Dec 10, 2008.

  1. tannn macrumors newbie

    Joined:
    Dec 10, 2008
    #1
    Hey,

    How / where can i set /etc/hosts or the hostfile to take priority over DNS. I have set some hostnames/ips up in the hostfile but the domain controller keeps taking precedence unless i dscacheutil -flushcache. Is there a comparable nsswitch.conf in Leopard?

    i am looking for something similar to lookupd which is not available for 10.5 OS X.

    cheers
     
  2. jmxp69 macrumors 6502

    Joined:
    Dec 10, 2008
    #2
    Put the following in /etc/resolv.conf above your nameserver line:

    order hosts, bind
     
  3. belvdr macrumors 603

    Joined:
    Aug 15, 2005
    #3
    Where did you find that? I have never seen that in resolv.conf.

    Now, you can edit /etc/nsswitch.conf and make sure the line beginning with "hosts" has listed after it "files dns".
     
  4. tannn thread starter macrumors newbie

    Joined:
    Dec 10, 2008
    #4
    resolv.conf is temporary, the second i reboot the system it erases the entry.

    and, there is no nsswitch.conf in Mac OS X Leopard.
     
  5. jmxp69 macrumors 6502

    Joined:
    Dec 10, 2008
    #5
    It's a *nix convention. Google resolv.conf options. You'll see it.
     
  6. jmxp69 macrumors 6502

    Joined:
    Dec 10, 2008
    #6
    It's not technically temporary. dhcp clients overwrite resolv.conf. If you don't run dhcp, resolv.conf is persistent. If you have to use dhcp (and most people do), the next trick is to put a boot script that copies your adjusted resolv.conf to /etc after booting up.

    I haven't fiddled this yet, but you might be able to get away with:

    chown root:root /etc/resolve.conf and chmod og-rw /etc/resolv.conf to prevent the dhcp daemon from being able to write/modify the file.
     
  7. belvdr macrumors 603

    Joined:
    Aug 15, 2005
    #7
    Check here:

    http://www.macosxhints.com/article.php?story=20070223050607406

    I did that and found nothing. I also checked HP-UX, Solaris, and Linux, but no joy.
     
  8. tannn thread starter macrumors newbie

    Joined:
    Dec 10, 2008
    #8
    i run dhcp, and theres gotta be a better way than this..
     
  9. tannn thread starter macrumors newbie

    Joined:
    Dec 10, 2008
    #9
  10. belvdr macrumors 603

    Joined:
    Aug 15, 2005
    #10
    True, but then if you switch networks, and require different DNS servers, you're hosed.
     
  11. jmxp69 macrumors 6502

    Joined:
    Dec 10, 2008
    #11
    order hosts, bind works

    Try it, if it doesn't, delete the line. Regarding reboot, just put a bootup script that copies ~/myresolv.conf to /etc/resolve.conf

    simple.
     

    Attached Files:

  12. belvdr macrumors 603

    Joined:
    Aug 15, 2005
    #12
    Did you even try the command?
     
  13. tannn thread starter macrumors newbie

    Joined:
    Dec 10, 2008
    #13
    yes i did, look:

    marc-anssa-macbook-pro:~ manssa$ lookupd -configuration
    -bash: lookupd: command not found
    marc-anssa-macbook-pro:~ manssa$

    lookupd does not exist in leopard.
     
  14. tannn thread starter macrumors newbie

    Joined:
    Dec 10, 2008
    #15
    I have no idea how to write that kind of script, unfortunately :(
     
  15. belvdr macrumors 603

    Joined:
    Aug 15, 2005
    #16
    Why did Apple do this? This is just another dumb idea.
     
  16. jmxp69 macrumors 6502

    Joined:
    Dec 10, 2008
    #17
    Do this as root:

    cp /etc/resolv.conf /etc/resolve.conf.mine
    nano /etc/rc.local

    In nano:
    cp /etc/resolve.conf.mine /etc/resolv.conf

    In resolv.conf make sure you have above nameserver
    order hosts, bind

    That should take care of it.

    One other option: If this is a home network, disable dhcp, use a static address instead.
     
  17. jmxp69 macrumors 6502

    Joined:
    Dec 10, 2008
    #18
    It's not apple. It's the way *nix works. It's been working that way for a looooooooooooong time. hosts files were outgrown about a week into the Internet. Bind was the right answer. Generally the default behaviour is to consult the bind system over hosts. BTW: resolv.conf was never an issue until dhcp showed up, but when you dynamically assign IP addresses, you have to tell the client where to search for dns which is why it overwrites resolv.conf
     
  18. belvdr macrumors 603

    Joined:
    Aug 15, 2005
    #19
    I think I would do something different, in case you go to another site, and grab different DNS servers.

    You could do:
    Code:
    mv /etc/resolv.conf /etc/resolv.conf.new
    echo order hosts, bind > /etc/resolv.conf
    cat /etc/resolv.conf.new >> /etc/resolv.conf
    
    This way, no matter what you grab from the DHCP server, you put that above it.

    Since I don't have Leopard, can either of you check that widget link above? It may set the option permanently without the need for the script.
     
  19. belvdr macrumors 603

    Joined:
    Aug 15, 2005
    #20
    The latest versions of Solaris, HP-UX, and Linux use /etc/nsswitch.conf that defaults it to consult hosts first. I do this on all of our boxes at work without the need to mess around with any other files. Just add entries to /etc/hosts and go.

    Apple chose to make it hard to change the behavior.
     
  20. jmxp69 macrumors 6502

    Joined:
    Dec 10, 2008
    #21
    Yup, that would be even better. Good call.
     
  21. jmxp69 macrumors 6502

    Joined:
    Dec 10, 2008
    #22
    And those aren't really consumer OS's. No surprise in the enterprise that long term conventions are massaged to meet the needs of IT admins....

    So I stand corrected, but many many many *nix distros still use resolv.conf as described.
     
  22. belvdr macrumors 603

    Joined:
    Aug 15, 2005
    #23
    Yeah, that's probably right, except for Linux. Either way, this guy should be set in one way or another. :)
     
  23. jmxp69 macrumors 6502

    Joined:
    Dec 10, 2008

Share This Page