Hosts file is an important file located in your Mac allows mapping IP addresses to host names. The IP mapping controls the browser and determines how the site address entered should be handled before it goes to browser’s DNS resolution. In this article we discuss how to edit hosts file in Mac. If you are also using Windows, check how to edit hosts file in Windows OS.
Reasons for Editing Hosts File
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 faster.
- To block domains by defaulting it to localhost IP.
Make sure you have administrator privileges before trying to edit the hosts file.
Open Terminal App
- 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.
Note: You can also open Terminal from the path “/Applications/Utilities/” through Finder.
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.
sudo cp /private/etc/hosts ~/Documents/hosts-backup
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”.
Open Hosts File
Hosts file in Mac is located in the path “/private/etc/hosts/”. You can open it by typing the below command in Terminal and type the admin password.
sudo nano /private/etc/hosts
The file’s content will be loaded in nano editor and use arrow keys to reach the last line where you can enter new IP and domain name. For example, you can map a site www.webnots.com to an IP 74.220.193.173 so that whenever you open the site hosts file entry is used to map the IP quickly and save time on resolving host name.
Editing the File
- 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.
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.
127.0.0.1 blocked-site.com
::1 blocked-site.com
Flushing DNS
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.
dscacheutil -flushcache
Open the site you entered 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 status bar any more. Note that sometimes you may also need to close all browser windows or restart the system for the changes to be effective.
Restore File from Backup
Do not worry if you made some mistakes when modifying the file. You can restore the backup file saved in the Documents folders by typing the below command in Terminal app.
sudo cp ~/Documents/hosts-backup /private/etc/hosts
3 Comments
Leave your reply.