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