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
Cristian says
Hi. After I press Control+O I can’t find the host file because some folder are hidden in terminal . How can I fix this problem? Thanks in advance.
ConSlasH says
Your pressed CMD(COMMAND)+O not CTRL(CONTROL)+O 🙂
Joseph LaMonica says
No, at least on Yosemite you must press the “control” button.. it worked for me…
Ale says
it worked for me. I just could not save the file in Textedit because some “denied permission” issue. But this did it. thnx
JOrgeEA says
Hi. After I press Control+O appear this:
File Name to Write: /private/etc/hosts
^G Get Help ^T To Files M-M Mac Format M-P Prepend
^C Cancel M-D DOS Format M-A Append M-B Backup File
What can i do?
JOrgeEA says
Hello. After I press Control+O show that:
File Name to Write: /private/etc/hosts
^G Get Help ^T To Files M-M Mac Format M-P Prepend
^C Cancel M-D DOS Format M-A Append M-B Backup File
what can I do?
THNks
iHash says
After Control+O press ENTER, then press Control+X