How is DNS lookup configured for OSX Mountain Lion?

56,593

Solution 1

The host tool does not simply resolve names (as in, using the system name resolver) but actually queries dns servers (as in, sending packets to udp/53 and possibly tcp/53): it doesn't know nor use the local hosts file.

If you want to test the operating system's resolver (as in, gethostbyname() and similar libc functions) you can try to ping the name you added to /etc/hosts and it will honor what you put in that file.

Also, as you already found out DNS lookup on Mac OSX does not use /etc/resolv.conf, and the correct way to configure which DNS servers are queried is in the Network Settings gui and/or networksetup commandline tool. I honestly don't know if you can configure the order in which sources are tried, but the standard behavior is to try /etc/hosts first and dns servers after that.

Solution 2

DNS resolvers can be added in OS X via the networksetup command:

sudo networksetup -setdnsservers Wi-Fi 8.8.8.8 8.8.4.4

Insert name of network connection as appropriate. These resolvers will appear in resolv.conf as it is automatically generated, but direct edits to resolv.conf will not result in those resolvers being used. I assume the resolvers are stored in a plist somewhere; I will search and report back.

Edited to add: It looks like the XML plist file storing the DNS servers is /Library/Preferences/SystemConfiguration/preferences.plist.

The host file in /private/etc/hosts should work to force resolution for particular names (/etc is a symlink to /private/etc). Can you verify your syntax and that you are editing the correct file?

Share:
56,593

Related videos on Youtube

Ben Flynn
Author by

Ben Flynn

"I mean, James, the Internet is a place where absolutely nothing happens. You need to take advantage of that." - Strong Bad

Updated on September 18, 2022

Comments

  • Ben Flynn
    Ben Flynn over 1 year

    If I cat my resolv.conf, I see 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.
    #
    

    I am trying to add a DNS entry. I edited my hosts file and flushed the dns cache, but the name is not resolving if I use host servername. I thought perhaps that host was not configured to look at the hosts file. How can I get my new entry to resolve, and what is OSX using if not resolv.conf?

  • Ben Flynn
    Ben Flynn about 11 years
    So I could use this command to add the hosts file?
  • Luke404
    Luke404 about 11 years
    you can't, and you don't need to
  • phoebus
    phoebus about 11 years
    No, if you want to add names to the hosts file, just add them directly. The hosts file is checked first when resolving names, followed by the local resolution cache and then your configured DNS servers. Using the "host" command to test it though will not check the local hosts file. One easy way to test would be to use a network utility like ping.
  • Ben Flynn
    Ben Flynn about 11 years
    @Luke404 I did verify that ping works. Is cat-ing the hosts file the only way to see how name is being resolved?
  • Ben Flynn
    Ben Flynn about 11 years
    I found a perl wrapper for getaddrinfo: search.cpan.org/~pevans/Socket-GetAddrInfo-0.22
  • Marcin
    Marcin over 8 years
    Sometimes the interface get goofy names, to get the exact name you need you can do networksetup -listallnetworkservices Also, if that contains spaces, you need to single quote the entire name, i.e.: sudo networksetup -setdnsservers 'white space' 8.8.8.8 8.8.4.4
  • Rune FS
    Rune FS almost 8 years
    Is it possible to configure the resolver to use a different port than 53?
  • Johnny Utahh
    Johnny Utahh almost 4 years
    @phoebus et al, echoing Rune FS's comment: Is it possible to configure the resolver to use a different port than 53?