The hosts file on your Mac is a small but important text document that has the ability to map hostnames to specific IP addresses. While the modern Internet uses a variety of public and private DNS servers to assign IP addresses, the hosts file is a handy way to override those DNS servers, allowing you to manually point a website’s address to the desired IP address or block access. to a site entirely pointing to an internal or unused IP address.
Let’s take a quick look at how to edit the Mac hosts file on macOS.
How to Edit Your Mac Hosts File with Text Editing
There are two main ways to edit the hosts file in Mac OS X.
The first is to use TextEdit, since the hosts file is a plain text document. However, you cannot open the file directly since it resides in a protected area of the file system. Instead, we need to copy the file to an unprotected location, such as the Desktop, edit it, and then copy it again.
- To find the hosts file, open the Discovererwhich you can access by clicking on the desktop, the smiley face finder at the bottom left of the screen or by pressing command + Space bar + Discoverer.

- Now select Go from the menu.

- Then select Go to folder from the menu.

- In the box, enter “/private/etc/hosts” in the box and press Return.

- A new Finder window will open and your Mac’s hosts file will be selected. Click and drag it out of the Discoverer window and drop it on your desktop.
Block IP addresses
By default, the /etc/hosts file is relatively simple. It contains a series of lines of descriptive text that are “commented” with the pound sign or number (also called pound or hashtag) (#).
Any line that begins with the # sign is a comment and is not read by the /etc/hosts file. For each line, the computer ignores any text after the pound sign, making it a good way to add notes and descriptions to your files. It also contains default IP values for localhost and broadcasthost. To edit the file, you’ll add your own lines after broadcasthost.

In addition to testing newly developed websites and web hosting migrations, another reason to edit the hosts file is to block access to specific websites.
In our example, we will pretend that the computer we are using is a device that we want to use exclusively for work, without allowing ourselves to be distracted by Facebook on our work machine.
- To do this, type the IP address you want to assign, followed by two tabs and the host name.
- In our case, we want to block Facebook, so we will map www.facebook.com to 0.0.0.0, which, as an invalid IP address, will generate an error.

Redirect web addresses
Alternatively, you can enter the IP address of a valid site instead of 0.0.0.0, which will cause users trying to access Facebook to be directed to the site of your choice. To determine the IP address of a website, you can use the dig command, which comes standard with macOS. TO dig the site through the Terminal, we simply open the Terminal and then run the dig command on the URL, which will then return the IP address as output.
- Open the terminal and type “
$ dig www.techjunkie.com + short104.25.27.105“- Note: The +short option keeps the output with only the information we need, which is the IP address.
- Take note of the IP address that is returned and use it in your Mac’s hosts file mapping. For example, The New York Times The website returns an IP address of 170,149,172,130. If we assign that to Facebook in our hosts file, every time someone using a Mac tries to go to Facebook, they will see The New York Times load instead.

- You can add as many entries to the hosts file as you like by entering new assignments on each line. Once you have made the desired changes to the hosts file, save it to its current location on the desktop.
- Next, drag and drop the hosts file from your desktop to its original location on /private/etc.. If you have closed the Finder window that points to this directory, simply use the Finder > Go > Go to Folder option, discussed above, to reopen it.

- When you return the hosts file to its original location, macOS will ask you what to do with the unmodified hosts file that’s already there. Choose Replace and then enter your administrative user password to authenticate the transfer.
With the modified hosts file now in place, launch your web browser to test the changes.
Clear your DNS cache
In most cases, the new mapping should work right away, but if you don’t see the correct behavior, you may need to flush your DNS cache, which will force the browser to read your hosts file to determine the mapping.
To flush the DNS cache on your Mac, open Terminal and use the following command (sudo allows you to run this command as administrator of your machine):
- Now write “
sudo killall -HUP mDNSResponder” and press Return. Note: You will need to enter your administrator password to run this command. - To have your Mac clear its DNS cache and then confirm that you’ve cleared it by echoing a confirmation, enter these two commands separated by a semicolon as shown here: “
sudo killall -HUP mDNSResponder;say DNS cache has been flushed.”
Your Mac will then appear over the speakers saying “DNS cache has been flushed.”
How to Edit Your Mac Hosts File in Terminal with Nano
The steps in the previous p are pretty simple, but if you want to avoid copying the hosts file, you can edit it directly in Terminal using the UNIX Nano Text Editor, which is built into macOS.
- To get started, launch Terminal, type the following command and press Return. As with all sudo commands, you will also need to enter your administrator password to run it:
"sudo nano /private/etc/hosts“ - Now you will see the hosts file open in the elder brother editor or push or another editor of your choice. To navigate and edit the file in elder brotherUse the arrow keys on your keyboard.

- Just as we did with the TextEdit method above, we can add, edit, or remove hostname mappings at will. Because we start Nano using sudo, any changes will be authenticated and saved directly to the original hosts file, without the need to copy it outside of your home directory.
- When you have finished making changes, press Control-X to exit the editor, AND to save, and Return to overwrite the existing hosts file.
As we mentioned above, be sure to flush your DNS cache if you notice that your new mappings aren’t working correctly.
Our examples mentioned blocking and redirecting distracting sites in a work environment, but you can also follow these steps to manually block access to malicious websites and, of course, other uses as well.
If you ever make a mistake and aren’t sure how to fix it, you can always restore the default contents of the hosts file by using one of the methods above to enter the following default information:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost::1 localhost
fe80::1%lo0 localhost
Final thoughts
Knowing how to edit the hosts file in macOS offers a convenient way to override DNS servers and manually point a website address to a new IP address or block access, and by following the steps outlined above, you can accomplish this task very quickly .
If you are a Mac user and found this article helpful, you may want to check out other TechJunkie tutorials including How to find the exact CPU model of your Mac andHow to Flush DNS on Mac Mojave.
What is the reason you are looking to edit your Mac’s hosts file? How did it turn out? Tell us in the comments below!