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

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:

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?

5

4 Answers

I want also my bind9 to work when I'm offline. Unfortunately the /etc/resolver/* solution does not work when all NICs are disconnected.

But I found this excellent answer. The only disadvantage is that is run-time based, e.g. the configuration does not survive restart.

To solve this I introduced simple /Library/LaunchDaemon which manages it during the OS startup.

The shell script:

#!/bin/bash
LOG="/opt/local/var/log/local-ns.log"
function register() { cat <<EOF | /usr/sbin/scutil
d.init
d.add ServerAddresses * 127.0.0.1
d.add SupplementalMatchDomains * <YOUR-DOMAIN-HERE>
set State:/Network/Service/<YOUR-DOMAIN-HERE>/DNS
EOF scutilret=$? echo "$0[$$]" `date` "scutilret:$scutilret" >> "$LOG"
}
echo "Starting registering " `date` > "$LOG"
for (( i = 0; i < 10; i++ )); do register scutil --dns 2>&1 >> "$LOG" cnt=`scutil --dns | grep 127.0.0.1 | wc -l` if [ "$cnt" -ne 0 ]; then echo "DNS management done" >> "$LOG" break; fi echo "DNS not ready yet" >> "$LOG" echo "iteration $i" >> "$LOG" /bin/sleep 2
done
echo "DONE" >> "$LOG"
exit 0

The DNS subsystem is not immediately ready when the script starts so hence the check guarded by 10 tries. And the /Library/LaunchDaemons/YOUR-LABEL-HERE.plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "">
<plist version="1.0">
<dict> <key>Label</key> <string>YOUR-LABEL-HERE</string> <key>Program</key> <string>/bin/bash</string> <key>ProgramArguments</key> <array> <string>/bin/bash</string> <string>/opt/local/root/local-ns.sh</string> </array> <key>RunAtLoad</key> <true /> <key>LaunchOnlyOnce</key> <true /> <key>Disabled</key> <false /> <key>KeepAlive</key> <dict> <key>SuccessfulExit</key> <false /> </dict>
</dict>
</plist>

Works while online or offline on macOS Catalina.

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.

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

4

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!

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
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like