There's a lot of conflicting information about how to set networking information (such as DNS resolution configuration) from the terminal. In the course of setting up a VPN package (openvpn) on OS X, I had to set the DNS resolution configuration dynamically from a shell script.
As of (at least) 10.3, /etc/resolv.conf (or /var/run/resolv.conf) is NOT the place to do this. Re-writing /etc/resolv.conf resulted in a system where a DNS lookup with host would work, but dig and ping would not. Sometimes /etc/resolv.conf would be magically restored to its original configuration. I thought the smarter option would be in the NetInfo database, except on my machine, I had no resolver configuration hiding there.
,br>
The answer? OS X has a daemon called configd, which magically collects configuration information, sends notifications, and maintains a dynamic database of the current settings. The host command would read my hacked-up /etc/resolv.conf, but smarter DNS lookups would query the network configuration database from configd.
The command-line tools to interface with the configuration daemon are scselect and scutil. scselect provides a list of defined network locations (as in the Network preference pane) and allows you to choose between them. scutil enables much more fine-grained control over the current network configuration. Unfortunately, it only really offers a command-line interface to modify the configuration database. To use scutil from a bash script, you must dynamically create an scutil script as a text file, and pipe it to scutil.
The following sample scutil sessions perform some useful tasks...
To retrieve the current primary network interface:
scutil
> open
> get State:/Network/Global/IPv4
> d.show
<dictionary> {
PrimaryService : 7BB2FEBC-B166-11D9-AA42-000A95EED02C
Router : 198.32.18.254
PrimaryInterface : en0
}
> quit
To retrieve the current DNS settings, having retrieved the ID (that long hex string) of the primary service as shown above:
scutil
> open
> get State:/Network/Service/PRIMARY_SERVICE_ID/DNS
> d.show
<dictionary> {
ServerAddresses : <array> {
0 : 198.35.23.2
1 : 198.32.56.32
}
DomainName : apple.co.uk
}
> quit
To set the resolver configuration, with a domain of 'apple.co.uk' and DNS name server IPs of 198.35.23.2 and 198.32.56.32, and the primary network service ID as obtained above. Root privileges are required for the modification:
sudo scutil
> open
> d.init
> d.add ServerAddresses * 198.35.23.2 198.32.56.32
> d.add DomainName apple.co.uk
> set State:/Network/Service/PRIMARY_SERVICE_ID/DNS
> quit
This last procedure will update /etc/resolv.conf for you as the change filters through the system. scutil does other useful things too -- read the man page and look at the help from within scutil for details.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20050621051643993