The hosts file is a text file that map a domain to an IP address.
When we type a particular domain in our browser, the operating system checks for information on hosts file that relates to the domain and if exists, will resolve to the IP indicated.
The hosts file can be edited to block certain hostnames (such as malicious hosts, for example), or used for development purposes, such as redirect domains to local addresses.
Edit the hosts file:
Step 1 – Open application terminal.app
You can open the application writing terminal on the spotlight, or simply go to application » Terminal.
Step 2 – Open Hosts File
Open hosts file writing on Terminal:
$ sudo nano /private/etc/hosts
Step 3 – Edition
The edit or add something to hosts file you can simply add a line like this:
Step 4 – Save Changes
When you have finished your changes, press control + o and Enter to save.
Then control + x to exit Nano Editor.
Step 5 – Clean DNS cache
You can clean you DNS cache with your terminal running this simple command:
$ dscacheutil -flushcache
Now you can test your change on the hosts file.