Update local DNS or hosts file

Hello all,



I been doing iOS for about 3 years now and able to transfer most of my skill in iOS to OS X development. So far, I notice there are differences such as the higher privileges, file system and so on.



The particular question I have is about the hosts file (located in /private/etc/hosts). I’m sure we all know it’s for DNS lookup and things. Just wondering, is there an API for updating the hosts file? Could someone point me to the API I should use?



Alternatively, is there API for updating domain lookup result on the mac temporally?




Thanks,

iOS apps cannot, in general, make changes that affect the system as a whole, and that includes modifying DNS settings.

If you need to use DNS names for testing, you have a bunch of options:

  • the Bonjour .local name

  • run your own DNS server (OS X Server makes this really easy) and configure your iOS device to point to that

  • use a service like

    xip.io

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Ok, that sounds good.


However, I can get higher privileges on MAC OS X and modify the hosts file in /private/etc/hosts. If I just edit the host file using file API it feels very dangerous, is there a hosts file specific API?

Have you read the man(8) page for mDNSResponder?

Or the man(5) page for hosts on the mac?

There’s no API for editing

/etc/hosts
per se, but you can set up host records (
kODRecordTypeHosts
) in your local directory store. You should be able to do this from the command line using dscl but I’ve not done that in a very long time (like the Rhapsody era) so YMMV.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Update local DNS or hosts file
 
 
Q