How to edit the Hosts file on your Mac
The Hosts file on your Mac plays a significant role for several reasons. Especially when you want to bypass the Domain Name System (DNS) and assign a new Internet Protocol (IP) address to a web page, it becomes particularly relevant. In this context, it is crucial to know how to find and access this file. The good news is that this is a comparatively simple and speedy process. All you need to know is what applications you are using and what steps you need to perform. With this, you can safely edit and update your Mac's Hosts file to resolve domain names to IP addresses. In this article, we'll explain what the Hosts file is for Mac and what purpose it serves. Then, we'll show you how to find and edit it in four straightforward steps. Let's start with the Editing the Hosts file on your Mac start
The importance of the Mac Hosts file and how it works:
Normally, your computer, especially a Mac, uses the Domain Name System (DNS) to find web pages based on the name servers your domain points to. The DNS translates your URL (for example, orcwebhosting.com) into an IP address that the server can understand. But your Mac hosts file allows you to manually assign a web page to a specific IP address or to a different one. In other words, you can configure your domain name to the IP address you want on your own. For example, one reason you might want to edit your hosts file is that Moving your website to a new server. If you change the IP address and domain name in your hosts file, you can test the web page on the new server. This is also useful if the DNS update has not yet taken place. In short, with the Mac Hosts file you can run simulations and tests to make sure everything is working correctly and smoothly before you complete the moving process.
Four steps to find and edit the Hosts file on your Mac:
Now that you have a better understanding of the Mac Hosts file, it's time to get to work. Below we will explain how to find and edit this file in four easy steps.
Step 1: Open your terminal program
To get started, first open your terminal program. Please note that you need administrator rights. If you do not have these, log in with an administrator account and activate the root user account. You can find the Terminal application by navigating to Finder > Go to > Utilities.
After that, you will see various tools that allow you to control your Mac device. Click on the Terminal.
Alternatively, you can press the F4 key to open the launcher. Then type "Terminal" in the search box and select the Terminal icon when it appears.
Step 2: Access your /etc/hosts folder
After the terminal is open, you will need a text editor to access the hosts file. In this tutorial, we will use the Nano text editor because it is flexible and user-friendly. First, install the latest version of the Nano program. Then you can enter the following command in the terminal:
sudo nano /etc/hosts
Then press the Enter key. You will be prompted to enter your administrator password: Enter your password and press the Enter key again.
Step 3: Start editing the Hosts file on your Mac
After you have entered your password and confirmed the entry, the Nano text editor will open. At this point, your screen should look like this:
The following steps depend on your specific intent in the Hosts file. Let's say you want to add a new domain. Use the arrow keys to navigate to the end of the file. There you can enter the IP address of the domain you want to add in a new line:
As shown in the screenshot above, we have inserted "220.127.116.11" for the new domain. Replace this IP address and "mydomain.ch" with the desired values. Note that each entry should be on a separate line. Multiple entries should have the following format:
If you want to undo your changes, you can delete the added lines with your keyboard. At the bottom of the editor window you will find a list of available keyboard shortcuts. In addition to adding IP addresses, you can also block them to prevent your device from redirecting the web page to that IP address. You can use "127.0.0.1" for this purpose. Once you are done, press Ctrl + O to overwrite the existing file followed by the Enter key. Finish the process by pressing Ctrl + X.
Step 4: Clear your DNS cache
If you have edited your Mac hosts file but your changes are not being applied, this could be due to your DNS cache. It is advisable to clear your cache after you have edited the file. Clearing the cache removes stored IP records so that your browser can use the updated IP address from the edited Hosts file. To update your browser's DNS records, enter the following command in the terminal:
sudo killall -HUP mDNSResponder
This step ensures that your changes take effect in your browser. You have now successfully edited the Hosts file on your Mac.