How to locate and edit the host file on macOS

In this article, we shall discuss how to locate and edit the host file on macOS. The hosts file on a Mac is a plain text file that maps hostnames to IP addresses. It’s a crucial part of the operating system’s networking configuration. This file allows you to override DNS settings for specific domains. Please see how to Run or Edit and Delete Tasks via Windows Task Scheduler, and How to Back Up and Restore the Windows Registry.
On macOS, accessing the hosts file is via the terminal. You can use your desired editor. I will be using the iTerm terminal as shown below.
Also, see How to locate and edit the host file on Windows, How to fix Computer Policy could not be updated successfully, and how to “‘Fix the request to add or remove features on the specified server failed“
Locate and edit the host file on macOS
Enter the command below “sudo vim /etc/hosts. Here I am using VIM which is a Text Editor in Unix Systems

You will be prompted to enter your password as shown above and when the right credential is entered, the host database will be opened as seen below.

To manipulate this file, use the “I” button on your keyboard in order to modify this field. This will let you enter a new host entry.
When you are done, you can hit the “Esc” button on your keyboard
- Following by a semicolon “:”
- For by “w” to save and
- Followed by “q” to quit as shown below

Note: Editing the hosts file on a Mac is a powerful way to control domain resolution on your system. Whether you want to block websites, test local development, or override DNS settings, the hosts file offers a straightforward method for managing hostname resolution.
I hope you found this article very useful. Please feel free to leave a comment below.