How to open and edit the hosts file on Mac computers?
Every Mac computer stores a small, text based file called hosts, sometimes modifying this file might be necessary to solve connection problems, set your own web server, demand computer to block access to specific website in order to prevent opening malicious websites. There might be a lot of different reason why would you modify this particular file, however it is not something, that lives within your desktop folder. The most frequent method to make changes within your host file is using Terminal application, it works on all version of Mac operating system, however the path sometimes varies.
What is more, in new version of macOS you will be able to modify the hosts file via finder and text editor as well. The idea to make changes within your hosts file might look a little bit frightening, however with a proper guidance it is really easy and what is most important useful method to achieve desired results. Quite frequent case, when users can't find difference between DNS server and hosts file, in order to make a clear view we will describe the actual difference between these two, also you will find the methods how to modify your hosts file depending on version of your Mac's operating system.
Table of Contents:
- What is the difference between DNS and hosts file?
- Modify hosts file via Finder
- Modify hosts file using Terminal
- Video Showing how to modify hosts file on Mac
What is the difference between DNS and hosts file?
First of all, let's make a clear view of difference between Domain Name System (DNS) servers and hosts file. One of the most frequent mistake between unexperienced computer users, that DNS and hosts file are recognised as same thing, due to this reason various modifications within hosts file, that should be performed with DNS server and vice versa results in unexpected issues.
To prevent this frustrating situation, the DNS servers is database, that responds to websites URL addresses expressed in letters. These servers finds the correct IP address assigned to the particular URL address and allows you to access the desired website. Also they create a hidden cache file, where this particular information is stored, to increase the loading speed whenever you will try to access the same website.
The hosts file is one of the easiest way to modify or deny DNS server results, also it may be really useful, if you're testing a newly developed web server, that is still in developing state, but you desire to access it through URL address, instead of it's IP address. Within hosts file you will be able to associate the particular IP address and Domain name, in other words URL address. Your Mac will open the IP address assigned to particular URL address within hosts file, ignoring the DNS server results. This is a perfect way to block some unwanted websites as well. For example, you're setting the new company computer, and desire to prevent employees from accessing unnecessary websites, such as Facebook, Twitter or any other. All you need to do, assign invalid or any preferred address to these website's URL address, for example, assign the invalid IP address 0.0.0.0 and URL www.facebook.com within Mac's host file and computer will attempts to connect the invalid IP addresses whenever the Facebook's URL will be entered. You may assign your company's homepage IP address instead of invalid IP as well. More detailed information about methods how to prevent Mac from accessing unwanted websites you will find in this article.
If you do not know the accurate IP address of appreciated website, you may use Terminal and the command ping. Launch terminal via Spotlight, press keyboard shortcut of Command and Spacebar and type in Terminal. Type in the ping command with the URL address and press return, within results you will find the website's IP address, the example of ping command listed below. ping www.pcrisk.com
Modify hosts file via Finder
On modern versions of Mac operating system the hosts file might be accessible and modified in two separate ways. First would be via Finder, by moving the into your desktop or any other unprotected are, the second would be to use the Terminal application. Let's begin with the hosts file editing via Finder, launch finder and click on Go option in the menu bar at top of your screen, then select Go to Folder and type in the following path, the click Go.
By default the etc folder is protected against any modifications, due to this reason copy the hosts file to unprotected location, for example, desktop. There you will be able to modify the copy of hosts file, simply double click on file to open it via TextEdit application, or any other application you're using to open and modify text based files.
Inside at the top of the file you will find the commented text, basically it's all text that is written after hash (#) symbol. Anything written in the same line, that starts with this symbol is ignored by computer, due to this reason it is used to leave comments and descriptions. Below the comment lines you will find the default IP addresses for localhost and broadcasthost. There might be any other data, added by various applications, it is recommended to add your appreciated information below the all existing. One of the most frequent case to modify your hosts file would be to block some unwanted websites from loading on your computer. Once you've finished the editing you need to replace the original hosts file with newly created one, repeat the procedure and go to etc folder.
Click on Go option in the menu bar at top of your screen, then select Go to Folder and type in the following path, the click Go. /private/etc/hosts Move the modified file to replace the old one, the operating system will ask you to authenticate in order to make changes within protected folder, click authenticate, then you will be asked to choose the action you desire to perform, click Replace. Finally, you have to enter the administrators account password to confirm your action. In case, you've blocked websites, that already been visited with this computer, you will need to flush DNS cache, detailed information you will find in this article.
Modify hosts file using Terminal
Modifying hosts file via Terminal saves you time spent while moving hosts file in and out of protected etc folder, however you have to work with keyboard instead of navigating using mouse or touchpad/trackpad. Using Terminal you will save time spent on moving hosts file, because you will be editing the original hosts file with the permissions of super user, due to this reason it is recommended to create the backup of hosts file if you're going to modify the particular file for the very first time, or not sure what you're going to do. First launch Terminal by using Spotlight, press the keyboard shortcut of Command and Spacebar, type in Terminal and press Return. Alternatively you may visit Utilities folder under Applications via Finder to launch this built-in command line application. Once Terminal launched, type in the following command and press Return.
- sudo cp /private/etc/hosts ~/Documents/hosts-backup
To restore hosts file use the following command and press Return to execute.
- sudo cp ~/Documents/hosts-backup /private/etc/hosts
Further, type in following command to access the hosts file by using Unix text editor called nano, press Return to execute command, you will be asked to enter the administrators password to confirm access to super user permissions.
- sudo nano /private/etc/hosts
Bear in mind, that Terminal will not display anything while you're entering the password, simply type in the password and press Return once again. The Terminal window will change and in the top of the window you will see the path to hosts file, content of it and the control commands in the bottom of window. To navigate while using nano use your keyboards arrow keys, leave all existing information and add your desired data below the existing, if you add in front of existing information, some of your application may not work.
It is recommended to use new line for each data you desire to add. Once you've finished editing the hosts while press the keyboard shortcut of Control and O and then press Return to save changes within the existing file, then press shortcut of Control and X to exit the nano editor. In order to changes take an effect you will need to flush the DNS cache, detailed instructions you will find in this article.
Video Showing how to modify hosts file on Mac