10

By default I would like to use the google public DNS servers and to fallback to intranet ones when these are failing.

Is this possible? ... I am looking for a setup that would not break when I move my laptop no other networks.

It would be acceptable if I found a solution specific to this wifi network connection (to do the trick only in this case, and to use defaults for others).

2
  • What does it mean "when google public DNS servers are failing"? There are plenty of ways to script changing network locations and/or simply script the entry of different DNS, but it's not clear how you would test failure of google DNS. Worse, the comment below about VPN will further complicate things if you add that to your requirements mix.
    – bmike
    Nov 7 '13 at 14:09
  • I'm surprised this isn't possible. I sometimes have the DNS servers which are supplied to me via DHCP fail so I use the Google ones. However I can't use any networks which use DNS to implement a captive portal now. If I could have the DHCP supplied servers as a backup then it would solve this. Did you find a solution?
    – Jake
    Mar 24 '15 at 16:44
4

You can use ipconfig getpacket to find the DNS server that DHCP recommended:

$ ipconfig getpacket en0 
...
domain_name_server (ip_mult): {192.168.3.2, 192.168.42.1}
...

So, assuming you are using the Wi-Fi adapter and its device is en0, you can:

#!/bin/bash

default_servers=$( ipconfig getpacket en0 | \
   perl -ne'/domain_name_server.*: \{(.*)}/ && print join " ", split /,\s*/, $1' )
networksetup -setdnsservers Wi-Fi 127.0.0.1 $default_servers
2

Yes, you can add the Google DNS servers at the top of the list and follow those with your own (intranet servers).

  • Go to System Preferences > Network
  • Select your network interface from the list on the left
  • Click the Advanced button on the right
  • In the DNS tab of the dialog, click "+" to add or "-" to remove entries (you can also edit entries by double clicking or selecting and clicking on the address)
  • Add the Google DNS addresses as the first two entries
  • Add your intranet DNS servers below those
  • Click OK
  • Click Apply
  • Close System Preferences
1
  • 1
    I know this, but the problem is that this makes impossible for me to use the same VPN from another location. It should be another solution, maybe something to configure in the VPN server (PPTP) or to configure a DNS with 127.0.0.1 address and to configure a local DNS that does still use the DHCP provided DNS servers.
    – sorin
    Nov 7 '13 at 13:48
1

There's also a way to do this from the command-line if you're a network administrator looking for a way to do this in a batch:

networksetup listallnetworkservices
# look for the correct network device here, probably "Wi-Fi"
sudo networksetup -setdnsservers Wi-Fi 8.8.8.8 8.8.4.4

Replace "Wi-Fi" with the correct network device. You may, instead, wish to use:

device=`networksetup listallnetworkservices | grep Wi-Fi`
sudo networksetup -setdnsservers "$device" 8.8.8.8 8.8.4.4
1

I'm trying to do the same. I'm going to manually run a caching dns server locally and have it learn intranets as I join.

I'm excited that the dns server might even be able to differentiate between intranet and internet hostnames before making the recursive call.

I'm worried that I'll either need short TTLs, or need to write hooks so that every time an interface changes I consider flushing the dns cache, or similiar.

I'll report back if successful...

The first 50%

After looking at the Wikipedia comparison of DNS servers, Dnsmasq comes to the top of my list.

Turns out brew has a forumla for dnsmasq, and it even has some services sub-interface written for brew...great, less to think about, just have to work out the config.

$ brew install dnsmasq  # [libidn] internation domain names
$ cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf
# To have launchd start dnsmasq now and restart at startup:
$ sudo brew services start dnsmasq 

(brew also has bind, but minimal state is attractive too me -- not having to clear it when doing network configuration).

Todo

  • Figure out configuring Network Preferences to use my new dns, and
  • Configure dnsmasq to ensure it can use intranet settings. (the real challenge)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .