The hosts file is used by your computer to map hostnames to IP addresses.
By adding or removing lines to your hosts file you can change where certain domains will point when you access them in a browser or using other software. You can block certain hosts names, like ad-serving/malicious hosts, or used for web development purposes, i.e. to redirect domains to local addresses.
This is an important file and one that is under the computer administrator’s control, so you’ll need an account with full privileges to make any changes.
Making a backup of the hosts file can be a good idea if you plan on making significant changes or just want to play around with modifications and see what happens.
Step 1: Launch Terminal, found in /Applications/Utilities/ or launched through Spotlight
Step 2: Type the following command at the prompt to backup hosts file to documents folder:
sudo cp /private/etc/hosts ~/Documents/hosts-backup
Step 3: Type the following command at the prompt to open hosts file:
sudo nano /private/etc/hosts
Step 4: Enter the administrator password when requested, you will not see it typed on screen as usual with the command line
Step 5: Once the hosts file is loaded within nano, use the arrow keys to navigate to the bottom of the hosts file to make your modifications
Step 6: When finished, hit Control+O followed by ENTER/RETURN to save changes to /private/etc/hosts, then hit Control+X to exit out of nano
Step 7: Verify your hosts modifications. You may need to clear DNS cache first.
If you want to restore the modified hosts to the backup of the original file use this command:
sudo cp ~/Documents/hosts-backup /private/etc/hosts
Franco says
Long time I did not use nano 🙂 I thought the new rootless system of el capitan would not allow to modify the host file unless you go in the terminal of the recovery mode ? Good News
I do have one problem. I have Many entry (overs a 1000) in my host file likes
127.0.0.1 somewebsite.com
All of them work. and I go nowhere if i go in safari to somewebsite.com
Except one
I try it in network utility and the answer is 127.0.0.1
but when i go to safari, I end up at the website anyway.
I clear the dns cache with sudo killall -HUP mDNSResponder
and even restarted the computer,
I also have my DNS server on a mac mini, also with the same host file.
How can this happen ?
Franco says
oops
i meant:
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
But doing it work on a 10.10.5 machine
can’t seems to make it work on a 10.11 one
Franco says
Problem solved. It seems using nano does not change the host file for me right now. I used Gas Mask, then using the flush and the kill did not seems to work. A restart did the job.
Last thing I need to test is editing the host file with nano in the recovery mode.
Robert Wakeford says
Interesting articale. however, I do not seem to have permission to edit my hosts file.
The only thing that I’ve down to my mac is to install MAMP pro. Everything seems to be OK with my installation except I cannot change the host file
Jochen Elias says
Thank you for this tutorial! Worked well 🙂