How to Modify Hosts File on Mac?
Written by Karolina Peistariene on
How to Open and Edit the Hosts File on Mac Computers?
All Mac computers store a small, text-based file called 'hosts'. Modifying this file is sometimes necessary to solve connection problems, set your own web server, and instruct the system to block access to specific websites (thus preventing opening malicious websites). The most common way to make changes to the host file is using the Terminal application.
In the new version of macOS, you can also modify the host's file via Finder and the text editor. Making changes to the host's file may seem daunting, but it is straightforward and useful to perform certain operations properly. Despite this, users cannot differentiate between the DNS server and the host's file in some cases.
To clarify, we will describe the differences and provide methods to modify the host's file depending on your Mac operating system version.
Video showing How to Modify Hosts File on Mac?
Table of Contents:
- Introduction
- 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?
Firstly, we will clarify the difference between Domain Name System (DNS) server and the host's file. Inexperienced computer users commonly mistake the DNS server, and hosts file to be the same. Therefore, modifications to the host's file that should be applied to the DNS server (and vice versa) lead to problems.
A DNS server is a database that responds to website URL addresses expressed in more meaningful characters. These servers find the correct IP address assigned to the particular URL address and allow you to access the desired website. They also create a hidden cache file where this information is stored, thus increasing loading speed when you try to access a previously visited website.
The host's file is one of the easiest ways to modify or deny DNS server entries. It can be useful to test a web server under development so that you can access it through a URL address rather than its IP address. You can associate a particular IP address and Domain name (i.e., URL address).
The Mac will open the IP address assigned to a specific URL address within the host's file, ignoring the DNS server results. This is also an effective way to block unwanted websites. For example, suppose you are setting up a new company computer and preventing employees from accessing unnecessary websites (such as Facebook, Twitter, etc.).
In that case, you simply assign invalid or other preferred addresses to these website URL addresses. As a further example, you can assign the invalid IP address 0.0.0.0 and URL www.facebook.com within the Mac host file - the computer will attempt to connect to the invalid IP addresses when the Facebook URL is entered.
You can also assign your company homepage IP address rather than an invalid IP. More detailed information about how to prevent Mac from accessing unwanted websites can be found in this article.
If you do not know the full IP address of a specific website, you can use Terminal and the command ping. Launch terminal via Spotlight, press the keyboard shortcut of Command and Spacebar and type in Terminal. Type in the ping command with the URL address and press return. Within the results, you will find the website IP address. The example of the ping command listed below is: ping www.pcrisk.com
Modify hosts file via Finder
On modern versions of the Mac operating system, the host's file might be accessible and modified in two separate ways. First, via Finder - by moving the file to your desktop or another unprotected area.
Second, via the Terminal application. We begin with hosts file editing via Finder.
1. Launch Finder and click the Go option in the menu bar at the top of your screen.
2. Then select Go to Folder and type the following path and click Go:
/private/etc/hosts
3. By default, the "etc." folder is protected against modification. Therefore, copy the host's file to an unprotected location, for example, the desktop. There you will be able to modify the copy of the host's file. Simply double-click on the file to open it via the TextEdit application (or any other application able to open and modify text-based files).
You will find comments at the open file's top (denoted by the hash [#] symbol). Anything placed on the same line that starts with this symbol is ignored by the system. In this way, comments and descriptions can be added to the file. Below the comment lines are the default IP addresses for localhost and broadcast hosts. There might be other data, which is added by various applications.
4. Enter your additional information below all existing entries. A common reason to modify the host's file might be to block unwanted websites from loading on your computer.
5. Once you have finished editing, you must replace the original host's file with this newly created version. Therefore, repeat the above procedure and go to the "etc." folder.
6. Click the Go option in the menu bar at the top of the screen, select Go to Folder and type in the following path: /private/etc/hosts
7. Click Go.
8. Move the modified file to replace the old one. The operating system will ask you to authenticate to make changes within the protected folder. Click authenticate.
9. You will now be asked to choose the action you wish to perform. Click Replace.
10. Finally, you must enter the administrator's account password to confirm your action. If you have blocked websites that already have been visited by this computer, you will need to flush DNS cache - detailed information can be found in this article.
Modify hosts file using Terminal
Modifying the host's file via Terminal can save time moving the host's file in and out of the protected "etc." folder. In this case, however, you must work with the keyboard rather than the mouse or touchpad/trackpad. Using Terminal saves time spent moving the host's file because you will be editing the original host's file with superuser permissions. Therefore, you are advised to create a backup of the host's file, especially if you do not experience these techniques.
1. Launch Terminal using Spotlight. Press the keyboard shortcut of Command and Spacebar. Type in Terminal and press Return.
2. Alternatively, visit the Utilities folder under Applications via Finder to launch this built-in command-line application.
3. Once Terminal is launched, type the following command and press Return:
sudo cp /private/etc/hosts ~/Documents/hosts-backup
4. To restore the host's file, use the following command and press Return to execute:
sudo cp ~/Documents/hosts-backup /private/etc/hosts
5. Type the following command to access the host's file by using a Unix text editor called nano. Press Return to execute the command. You will be asked to enter the administrator's password to confirm access to superuser permissions.
sudo nano /private/etc/hosts
Bear in mind that Terminal will not display any characters when you are entering the password.
6. Simply type the password and press Return.
7. The Terminal window will change. At the top, you will see the path of the host's file, content, and control commands at the bottom of the window.
To navigate while using nano, use your keyboard arrow keys. Leave all existing information unchanged and type your additional data below (adding your data before the existing information could result in the application failing).
8. Enter your data, each on a new line. Once you have finished editing the host's file, press the keyboard shortcut of Control and O and then press Return to save changes within the existing file.
9. Then press the shortcut of Control and X to exit the nano editor. For the changes to take effect, you will need to flush the DNS cache - detailed instructions can be found in this article.
▼ Show Discussion