How do I use my localhost DNS server when offline, on Snow Leopard

5

2

I've followed these instructions for setting up a local DNS server on my mac, and using it to server wildcard subdomains on localhost. It's a really nice thing:

http://mikeferrier.ca/2011/04/04/setting-up-wildcard-dns-on-localhost-domains-on-osx/

One weakness, though: I'm sometimes trying to work offline, and the local BIND instance is only consulted when you have a live connection with 127.0.0.1 in the DNS server list.

Is there a way to achieve this effect offline?

Ross M Karchner

Posted 2011-06-16T11:45:56.410

Reputation: 236

127.0.0.1 is offline. You can always connect to 127.0.0.1 = localhost. The only thing you need is the loopback interface aka localhost aka 127.0.0.1. I'm not an osx buff, and I don't have enough cred here to make a comment... What exactly is meant by "work offline"? A safari mode? – Ярослав Рахматуллин – 2011-06-16T12:43:15.437

I'm working on a site that uses subdomains-- using that trick above, I can fake that on localhost, so I have one.mydomain.local, and two.mydomain.local, etc, all resolving to 127.0.0.1. But it only works when I have an active connection. By offline, I just mean not connected to any network – Ross M Karchner – 2011-06-17T02:59:25.787

just make sure "lo", the loop-back is up. ifconfig lo up or whatever (where whatever is something like ifconfig 127.0.0.1 lo up; route add -net 127.0.0.0 gw 127.0.0.1 netmask 255.0.0.0 -- but you should not have to do this manually) – Ярослав Рахматуллин – 2011-06-17T03:11:04.853

Localhost is fine-- what I want is my locally running instance of BIND to be consulted for domain lookups, even when I'm not online. – Ross M Karchner – 2011-06-17T11:08:48.743

Related thread on StackOverflow here. – Barnabas Kendall – 2011-07-01T20:26:06.367

Answers

2

You need to set your resolver configuration to use 127.0.0.1 for that local domain:

sudo mkdir /etc/resolver
sudo vim /etc/resolver/local

Add the following lines to the /etc/resolver/local configuration:

nameserver 127.0.0.1
search_order 1

The name of the configuration file "local" is the name of your search domain. (Mine happens to be "dev"; if you followed that tutorial, yours would be "local".)

You can then go into your System Preferences / Network configuration and remove the custom DNS server you had set there for 127.0.0.1. The above resolver configuration means your localhost DNS will only be consulted for "local" domains.

More details in this helpful serverfault answer.

Andrew Vit

Posted 2011-06-16T11:45:56.410

Reputation: 990

1

The thread A small bit of hardware hacking == No Wi-Fi suggests using a hardware loopback device ($6.79 on Amazon).

This might be a solution to trick OS X into thinking it has a valid network connection.

This also requires using a static IP address, since there is no DHCP server.

image

harrymc

Posted 2011-06-16T11:45:56.410

Reputation: 306 093

I'm buying it now to see if this works. Have you actually tried this, along with the local DNS thing? – Barnabas Kendall – 2011-07-04T22:07:10.527

Hope this works for you. But no, I never had this problem, nor a Mac. Did you try just connecting a network cable to the card and using a static IP? – harrymc – 2011-07-05T05:59:50.497

This is interesting-- but there has to be a software solution, right? – Ross M Karchner – 2011-07-06T16:49:40.847

Hmmmm.... maybe. – harrymc – 2011-07-06T17:40:33.630

1

Perhaps I am missing something, but you seem to be overlooking the /etc/hosts file completely. Unix operating systems will reference /etc/hosts before making DNS requests. localhost resolves to 127.0.0.1 because it is specified so in /etc/hosts!

http://en.wikipedia.org/wiki/Hosts_%28file%29

You can edit /etc/hosts with any text editor, but you'll need root privileges.

sudo open -a TextEdit /etc/hosts

127.0.0.1 one.mydomain.local
127.0.0.1 two.mydomain.local
127.0.0.1 example.com subdomain.example.com

solo

Posted 2011-06-16T11:45:56.410

Reputation: 373

The idea is to not have to manually tend /etc/hosts-- I want anything.local to resolve to 127.0.0.1 – Ross M Karchner – 2011-07-07T14:38:09.410

/etc/hosts is not very helpful when you're developing sites that use dynamic subdomains, like username.example.com. On my local DNS server I have a zone for *.dev so if I just rename ".com" to ".dev" then it goes to my localhost server. – Andrew Vit – 2011-07-31T21:05:37.497