A hosts file can be used on your Mac to override default DNS (Domain Name System) information. Altering a hosts file comes in handy when you are testing a server before going live with it. You’ll be able to use its domain name instead of the machine IP address. By entering your computer’s IP address and using that domain name, your Mac goes to that device instead.
Another way to use hosts files is to block spyware by using 0.0.0.0 for their IP address, then entering the name of the domain you’d like blocked.
Edit the Hosts File
We’ll be showing you how you can edit the hosts file the most effective way, using Terminal on your Mac. The Terminal application is in your Mac’s “Utilities” folder. First off, you’ll need to know the IP address of the device where you want to direct your Mac. Otherwise, at least know the domain names of Internet sites you’d like your Mac to avoid.
- Navigate to Terminal on your Mac and double tap on the track pad, or double click your mouse.
- Now, type “sudo nano /etc/hosts,” and hit “enter” or “return” on your keyboard.
- Next, you’ll be required to enter your administrator password to proceed.
You should be in the Nano Text Editor.
When you would like to add a new device or domain, move the cursor by using the arrow keys on your Mac’s keyboard, place your cursor after the text on your screen, and start typing. On your local network, you can map a specific IP address to a domain—type the IP address, then type the domain name.
How to Avoid Sites
When you’re trying to keep your Mac away from particular websites, use 127.0.01, which maps your Mac back to your Mac. Your Mac most likely is assigned a different IP address by the router, so entering 127.0.01 defaults to the local machine, due to the default settings in that hosts file.
After you’re done, hold down the “control” and “O” keys on your keyboard to save the file you’ve edited. Then, hold down “control” and “X” to exit.
Clear the DNS Cache
Once you’re back at the command line in Terminal, type “sudo killall –HUP mDNSResponder” and then hit “return.” Doing this clears the DNS cache on your Mac and avoids confusion with any changes you’ve made to the hosts file.
Make Yourself a Note
At some point down the road, you may need to undo the changes you’ve made to the hosts file to keep things working correctly on your Mac, so it’s best you don’t forget these changes you’ve made. Write yourself a note in your Mac’s notes app as a friendly reminder about the changes.