Every Mac has a “hosts” file. It’s a small plain text file that resides on your machine for the purpose of mapping its hostname to IP addresses. Its function is quite simple, and the average computer user may never have to interact with it unless an issue forces them to. The hosts file also tracks things like licenses when you do updates. However, this file can get confused for any number of reasons and will cause problems when it does so.
If your Mac isn’t working quite how you expect it to, we’ll show you how to find out if the hosts file is causing those issues. We’ll also show you how to repair the hosts file to fix it.
How to Know if Your macOS Hosts File Needs Repairing
A computer with a corrupt hosts file will typically have problems when connecting to the internet. For example, you may try to do a system update but get a message that you can’t update because your Mac isn’t connected to the internet. Yet, when you launch your web browser, your computer connects to the internet just fine.
In cases like this, and others, you can take a look at your hosts file to see if there is any corrupt information residing in it.
To access your hosts file, open Finder. From the menu bar, click Go > Go to folder and type in /private/etc/hosts. Then hit the Enter key. Double-click the hosts file to open it. It will open in a simple text editor. A healthy hosts file should end with the following three lines:
There should not be any text beyond the last localhost line. A clean hosts file should look like the one in the picture below.
If you see additional lines, that’s what’s keeping your machine from connecting.
In the example below, you can corrupt code was written into the hosts file by an app while it was trying to do an update. For whatever reason, this app likely failed mid-update and left the partial text behind in the hosts file. This text will have to be removed by hand before the Mac will be able to update correctly. At this point, you can edit out the bad script using the simple text editor that it opened in.
How to Repair the macOS Hosts File
Follow the below steps to remove the corrupt lines. First, take your tooltip and highlight everything below the second localhost line. Double-check that you have highlighted the correct text; you don’t want to alter anything in the text above that second localhost line. Now hit Delete.
Your Mac will now give you the message:
You don’t own the file “hosts” and don’t have permission to write to it. You can duplicate this document and edit the duplicate. Only the duplicate will include your changes.
Click Duplicate. Now save the hosts copy file to your desktop and close the original hosts file. You will note that the saved new file is a corrected, clean hosts file that has no additional text after the second localhost line.
Next, Control-click to Rename the original hosts file; set it to hosts original. You will be prompted to enter your password. After that’s done, go to your desktop and rename hosts copy to hosts. Lastly, drag the edited hosts file back into the etc folder in Finder.
You will now have an updated hosts file, minus the corrupt text. You will also have the original hosts file, which is useful if you find that this didn’t fix the problem and you need to revert to it.
Don’t be surprised or alarmed if your computer requires you to authenticate all of your actions during this process. This is part of the Mac’s protection protocols that keep nefarious programs from altering your machine without your authorization or knowledge.
Checking if the macOS Hosts File Repair Fixed the Problem
When finished, close all of your open windows and restart your machine. You should first confirm that your Mac is able to get onto the internet after restarting. If there are no issues, then try repeating whatever activity triggered the original problem. If your hosts file was indeed the issue, your computer should now be behaving correctly.
You may also want to look at the best Mac tools to repair corrupted files in case this issue appears elsewhere.
Looking Beyond the Hosts File
While the hosts file is a significant potential cause of connection issues, especially if an app has added text to it, it’s by no means the only place a connection problem can occur. If you are still having the same problem, you will have to troubleshoot beyond the hosts file. There are lots of ways to do that, but at least you’ll know you’ve ruled out a common potential cause.