On OS X is it possible to override DHCP assigned DNS servers but still keeping them for fallback?
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).
Solution 1:
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
Solution 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
Solution 3:
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