18

I have installed, configured DNS server(local instance of Dnsmasq) which resolves to localhost as I want, all OK.

When I go offline, it stops working, because OS X empty content of resolv.conf and ignore attempt to reflect changes in this file.

Any idea, how to configure DNS even when offline?

Similar issue(unresolved): http://blog.steamshift.com/geek/leopard-lookupd-and-local-web-development-sites

Main motivation is ease development of RoR application which uses subdomains as account keys. And you can not use 127.0.0.1 *.yourapp.local in /etc/hosts. Some guy registered domain smackaho.st and srt DNS for it like .smackaho.st at 127.0.0.1 but still, you can not use it when you are working offline.

EDIT: tried scutil command, but it seems you can change DNS if offline

NOTE: when you have all interfaces down, you cannot set DNS servers in Pref. panel.

daeltar
  • 311
  • 1
  • 4
  • 8
  • Related: [dnsmasq not working without internet connection in OS X Yosemite](http://superuser.com/questions/835631/dnsmasq-not-working-without-internet-connection-in-os-x-yosemite) on Super User (without a solution but some references). – Arjan Apr 06 '15 at 13:32

6 Answers6

29

SEE UPDATE BELOW!

I also enjoy using Dnsmasq on my local machine, and I had this problem too. Here is the solution:

From man 5 resolver:

The configuration for a particular client may be read from a file
having the format described in this man page. These are at present
located by the system in the /etc/resolv.conf file and in the files
found in the /etc/resolver directory.

/etc/resolver/ is not present by default; you must create it yourself.

Also from the man page:

domain
  Domain name associated with this resolver configuration. This
  option is normally not required by the Mac OS X DNS search system
  when the resolver configuration is read from a file in the
  /etc/resolver directory. In that case the file name is used as the
  domain name.

So if you wanted all dns queries for the top level domain of dev to be routed to the local nameserver, you would:

# mkdir /etc/resolver
# echo 'nameserver 127.0.0.1' > /etc/resolver/dev

configd does not alter files in /etc/resolver/, so this setting will persist through network changes and reboots.

UPDATE 17 July 2012

Unfortunately, as of OS X Lion, the top resolver (as shown by scutil --dns) disappears when no interfaces are active:

# scutil --dns # Online
DNS configuration

resolver #1
  nameserver[0] : 127.0.0.1

...

resolver #8
  domain   : dev
  nameserver[0] : 127.0.0.1

# scutil --dns # Offline
DNS configuration

resolver #1

...

resolver #8
  domain   : dev
  nameserver[0] : 127.0.0.1

Notice that resolver #1 is empty, but that the /etc/resolver derived nameserver entry remains.

It turns out that since you can specify the resolver domain directly in the /etc/resolver/ file, specifying the special Internet root domain . causes the creation of a global resolver entry that looks like:

resolver #8
  nameserver[0] : 127.0.0.1

Now all DNS queries are routed to localhost, even when offline.

Of course, you will still have to resolve your chosen domains as 127.0.0.1 using something like dnsmasq's --address option:

# dnsmasq --address=/dev/127.0.0.1

In summary:

  • Set all your network interface dns servers to 127.0.0.1:
    networksetup -setdnsservers Ethernet 127.0.0.1
    networksetup -setdnsservers Wi-Fi 127.0.0.1
    ...
  • Create a file /etc/resolver/whatever:
    nameserver 127.0.0.1
    domain .
  • Set up a local DNS server and be happy.

cf. http://opensource.apple.com/source/configd/configd-395.11/dnsinfo/dnsinfo_flatfile.c

guns
  • 611
  • 6
  • 6
  • That is exactly what pow is using (http://pow.cx/). – daeltar Sep 05 '11 at 09:00
  • but unfortunately it does not work - https://github.com/37signals/pow/issues/104 – daeltar Mar 14 '12 at 12:54
  • @daeltar: Updated answer with a solution – guns Jul 18 '12 at 05:11
  • When I'm offline, the /etc/resolver rule catches resolution requests. However, I am finding that each time I connect to Wi-Fi or ethernet, I must run the corresponding -setdnsservers command (on OSX 10.8). This seems to be because the DNS servers provided by a network's DHCP server overwrite the manual settings. I wonder if there is a way to set the precedence of the resolver rule so that it is higher than any of the other resolvers. – Eric Drechsel Jan 30 '13 at 22:07
  • Reading `man 5 resolver`, I found a solution. Add a line like ```search_order 1000``` to your resolver.d file, where the number is lower that any of the other resolvers listed by ```scutil --dns``` (all of mine were 300000 or greater). This seems to cause the entry to superscede all others. – Eric Drechsel Jan 30 '13 at 22:23
  • @EricDrechsel, when you set search_order 1000, does the custom resolver get listed higher (with DHCP *On*), or does it still appear at the bottom (ie resolver #8)? I tried your `search order` solution and DHCP would still be superceding me. – amateur barista Mar 25 '14 at 04:43
  • 3
    @guns does this solution still work in yosemite? dnsmasq was working fine for me offline until I upgraded. Now all I get when I run `scutil --dns` when not connected to the internet all I get is `No DNS configuration available` I followed your instructions above but no luck – MatthewLee Oct 23 '14 at 17:36
  • @MatthewLee same issue here after updated to yosemite, any progress? – Lenciel Dec 30 '14 at 06:30
  • @Lenciel unfortunately, no. I figure if I am working on a site without an internet connect I will just need to add the domain name to my hosts files the old fashioned way... – MatthewLee Dec 30 '14 at 16:49
  • 1
    @MatthewLee, yes you are right, it seems the only work around is adding entries in the hosts file. – Lenciel Dec 31 '14 at 03:17
  • 1
    Anyone found a solution for Yosemite yet? – jmagnusson Apr 03 '15 at 12:37
2

Why not make the entries in /etc/hosts instead? I'm having trouble thinking of a situation where you'd need to actually be running a full blown DNS server. I use host file entries all the time to accomplish things like this on my Macs.

The resolver in OS X works differently than that in Linux or other Unixes. This is probably part of what's causing you grief. Like for instance it has a preferences for which method of resolution to use first and it caches the results of all queries for a period of time.

Have you added the DNS server to the interface in the Network preference pane? This should ensure that the resolver uses that server for it's queries should it decide to look for a DNS entry.

  • One reason to use a DNS _forwarder_ on a client machine is to maintain a large blacklist of ad/malware domains without suffering the performance penalty of the resolver daemon grepping the now bloated hosts file on every request. The merits of this approach notwithstanding, dnsmasq loads /etc/hosts into memory where the lookup time will be miniscule. Also, if you do web development, it allows you to avoid the small nuisance of adding local domains for every site you are working on. – guns Jul 26 '10 at 20:45
1

(answering b/c I can't comment yet...)

How are you going offline?

(best answer I got right now)

# man -S 5 resolver
 .
 .
 .
 Note that the /etc/resolv.conf file,
 which contains configuration for the default (or "primary") DNS resolver
 client, is maintained automatically by Mac OS X and should not be edited manu-
 ally.  Changes to the DNS configuration should be made by using the Network
 Preferences panel.

So, you should be able to enter something in the UI, and it should stick. I had done this a couple times when I had not like the DNS server my local DHCP server provides.

benc
  • 663
  • 1
  • 5
  • 13
0

Does OSX have the /etc/dhcp3/dhclient.conf configuration file that normal Linux/UNIX DHCP client configurations have?

If so, it should have a line you can uncomment for

prepend domain-name-servers 127.0.0.1;

to have the DHCP client always add that line to your resolv.conf

Kevin Kuphal
  • 9,064
  • 1
  • 34
  • 41
  • Adding to resolv.conf is not helping on OS X. Content of resolv.conf is generated and itself is symlink on /var/run/resolv.conf and even when I change /var/run/resolv.conf it does not reflect changes. – daeltar Jun 10 '09 at 09:34
0

Try adding a second ethernet service configured with a static address and 127.0.0.1 as your DNS server. Or, add a network Location which sets your DNS server to 127.0.0.1. Both of these changes would be made in "System Preferences" under the "Network" panel.

Josh
  • 9,001
  • 27
  • 78
  • 124
-2

It might help to just put

127.0.0.1       localhost

into /etc/hosts, so it doesn't need to resolver to find localhost.

Sven
  • 97,248
  • 13
  • 177
  • 225