Modify DNS settings for your Mac by editing the host file

Sep 30, 2014 20:45 GMT  ·  By

If you want to have custom DNS settings for some domains on your Mac (prank or development), you must edit a text file called hosts.

You need administrator rights in order to do this.

There are at least two ways to edit the file:

1. Use an app installed in System Preferences; 2. Directly edit the host file in Terminal.

Now let’s take them one at a time and see how we can achieve that.

1. Install Host app.

It will install a panel in System Preferences, where you can add/remove lines to/from the host file.

This is a fast and easy method if you have rare and little modifications, but it can prove rather frustrating if you have to add multiple lines.

2. The classic way to edit the host file is via Terminal. Thus, open Terminal and run:

code
sudo nano /private/etc/hosts
The format is: IP_address domain.com IP_address www.domain.com * You must have at least one space between IP_address and domain

When you finish editing the file, hit Ctrl+X to exit and Y to save.

You need to restart the DNS service, so run this command in Terminal.

For Mavericks:

code
dscacheutil -flushcache; sudo killall -HUP mDNSResponder
For older than Mavericks OS, run:
code
sudo killall -HUP mDNSResponder
That’s it, you’re done!

Edit the Host File on Mac (2 Images)

Host panel
nano edit host file
Open gallery