How to Edit Hosts File on Mac

November 21, 2023

Introduction

Hosts is a text file used for network configuration in macOS and other Unix-like operating systems. Administrators edit the hosts file to control domain and hostname resolution on the system manually.

This article will show you how to edit the hosts file on Mac.

How to edit hosts file on Mac.

Prerequisites

  • Administrative access to the system.
  • Access to the Terminal.

What Is Mac Hosts File?

The hosts file is a local implementation of the Domain Name System (DNS) that instructs the OS on how to resolve host queries. It lets users manually map IP addresses to any local and public host or domain name.

When a program attempts to reach a host, macOS checks the hosts file before querying a DNS server. If the file contains the relevant mapping, the system skips the DNS check and resolves the host.

Why Edit Mac Hosts File?

Editing the hosts file is frequently performed in network configuration and application development. The most common reasons to edit the file include:

  • Testing apps locally. The hosts file allows users to map domain names to local IP addresses, which is useful when testing an application before going live.
  • Creating server aliases. Custom, easy-to-remember names can simplify access to servers in a development environment.
  • Load balancer testing. Developers can test the load-balancing capabilities of their app by mapping a single domain to multiple IP addresses and monitoring how the app handles traffic requests.
  • Overriding public DNS entries. When testing DNS configuration changes, the hosts file provides a way to temporarily override relevant DNS entries without affecting the system.
  • Blocking access to websites. If you map a public domain name to the localhost IP address, the website becomes inaccessible, which helps with limiting which websites can be visited.

How to Edit Mac Hosts File

The simplest way to edit the hosts file is by using the Mac Terminal app. Follow the steps below to locate, open, and edit the hosts file in macOS.

Step 1: Open Terminal

Open Terminal by searching for it in Spotlight Search (Command + Space), or follow the steps below to run it from Finder:

1. Open Finder by selecting its icon in the dock.

The location of the Finder icon in macOS.

2. Select Applications in the menu on the left side of the window.

3. Expand the Utilities folder.

4. Select Terminal from the list of applications.

Finding the terminal app in macOS.

Step 2: Open Hosts File

Open the hosts file using a Terminal text editor, such as Nano:

sudo nano /private/etc/hosts

The file already contains some default mappings.

The look of the hosts file in the nano editor.

Step 3: Edit Hosts File

Edit hosts by adding your mappings to the bottom of the file. Use the following syntax:

[ip-address] [hostname-or-domain-name]

Note: You can insert any number of empty spaces between the IP address and the domain.

The example below tells the system to go to the IP address 192.168.0.12 every time there is a request to visit the test.local domain.

Editing the hosts file in macOS.

Save the file and exit. The configuration changes are applied automatically, and there is no need to restart the system.

Note: You cannot map a port number in the hosts file. To resolve to a specific port, create a reverse proxy server.

Conclusion

This article introduced the hosts file on Mac and provided steps for editing it. After reading the tutorial, you should know how to configure domain name resolution on your system manually.

Continue setting up your Mac by reading How to Set Environment Variables in macOS.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
What Is 127.0.0.1 Localhost?
February 17, 2022

If you are a beginner in web development or web hosting, learning about localhost is one of the first steps to mastering the craft.
Read more
How to Use The SSH Config File
January 19, 2023

The SSH config file helps easily configure the default values for these connections and enables efficient streamlining of SSH connections.
Read more
Resolve the "Temporary failure in name resolution" Error
November 9, 2023

This tutorial will guide you through troubleshooting and fixing the "Temporary failure in name resolution" error.
Read more
How to Redirect HTTP to HTTPS in Nginx
March 6, 2024

Nginx is a web server that can be easily configured to redirect unencrypted HTTP web traffic to an encrypted HTTPS server.
Read more