How to Edit Hosts File in macOS?
Hosts file is an important file located in your mac allows mapping IP addresses to host names. The IP mapping controls the browser, how the site address entered should be handled before it goes to browser’s DNS resolution. There are two main reasons one should think of adding mapping in hosts file:
- To directly map the domain to an IP so that the resolution time is fast.
- To block domains by defaulting it to localhost IP.
In this article we discuss how to edit hosts file in mac OS X with Yosemite version 10.10.2.
Edit Hosts File in Mac OS X
Hosts file is located in the file path “/private/etc/hosts” in mac. You should have an administrator privileges in order to edit the hosts file. Press “Command + Space” buttons in the keyboard to open “Spotlight Search”. Enter “Terminal” and double click on the “Terminal” app appearing as a top hit.
Backing up Hosts File
It is always a good idea to backup things before you start modifying. Type the below command in the Terminal command prompt and press enter to backup your hosts file to the Documents folder.
You will see a warning message and ask you to enter system password to proceed further. Type your password, you will not be able to view the password when typing, so don’t get panic that you are not able to type the password in Terminal app. Press enter after typing the password and check in the Documents folder to find the UNIX executable backup file named “hosts-backup”.
Hosts file in mac is located in the path “/private/etc/hosts/”, open the hosts file by typing the below command in Terminal and type the admin password.
Hosts file will be loaded in nano editor and use arrow marks to reach the last line where you can enter new IP and domain name. For example, you can map this site www.webnots.com with an IP 126.96.36.199 so that whenever you open the site hosts file entry is used to map the IP quickly and the save time on resolving host name.
- Leave a space between IP and domain name.
- Enter one entry per line.
- Don’t delete the previous entries available in the hosts file which are required for system booting.
- Mouse click will not work in nano editor and you need to use arrow keys to go to the last line.
- Type domain name without www and with www in two separate lines to consider both the entries. Here the IP address might be same.
Once you added the required entries, press “Control + O” keys and press enter / return key to confirm the modification to the file “/private/etc/hosts/”. Press “Control + X” to quit the nano editor.
Now the host file is modified but you may need to flush out the existing DNS cache for the entries to be effective in the browser. Type the below command in Terminal and press enter to flush out existing DNS entries.
Open the site you maintained in hosts file in Safari and see how fast it gets loaded compared to before. If you use Chrome you will never see the embarrassing “Resolving host…” message in Chrome status bar any more.
Did some mistake in modifying the hosts file? Don’t worry just restore the backup file saved in the Documents folders by typing the below command in Terminal app.
Blocking Domains Using Hosts File
If you want to block domains using hosts file then the simple way is to point those domains to the localhost IP address. The localhost IP is referred as ::1 for IPv6 and 127.0.0.1 for IPv4 hence add the below lines in hosts file to block a website.