When developing web applications, staging new site migrations, or testing server infrastructure on macOS, you often need your Mac to resolve a domain name to a specific IP address—such as your local machine (127.0.0.1) or a remote staging server—before updating public DNS records. Rather than setting up a private DNS nameserver, you can override public DNS lookups directly using the macOS hosts file. Located in the Unix root subsystem at /etc/hosts, this plain text configuration file maps human-readable domain names to specific IP addresses with absolute local priority.
How the Hosts File Functions in macOS
Whenever an application or web browser on your Mac requests a connection to an internet address (such as api.example.com), macOS checks its local network lookup hierarchy:
- The Local Hosts File (
/etc/hosts): Evaluated first. If an IP mapping exists for the requested hostname, macOS immediately uses that IP and bypasses all external network lookups. - DNS Cache (mDNSResponder): Evaluates cached responses from previous lookups.
- Configured DNS Servers: Consults your local router or upstream resolvers (such as Cloudflare or Google DNS) if no local mapping is found.
By editing this file, you can safely preview a new web host, develop virtual hosts in local Docker containers, or block distracting and malicious domains locally without altering external network equipment.
Step 1: Open Terminal on Mac
Because /etc/hosts is a protected system file owned by the root superuser, standard text editors like TextEdit cannot overwrite it without root permissions:
- Open Spotlight by pressing Command + Spacebar.
- Type
Terminaland press Return.
Step 2: Back Up the Original Hosts File
Before modifying system files, always create a timestamped backup copy so you can quickly restore defaults if necessary:
sudo cp /etc/hosts /etc/hosts.backup
Enter your macOS administrator password when prompted. Note that the terminal will not display asterisks or characters while you type your password; simply type it and press Return.
Step 3: Open the Hosts File in the Nano Text Editor
Launch the built-in command-line text editor nano with administrative privileges:
sudo nano /etc/hosts
You will see the default macOS loopback mappings, typically looking similar to this:
##
# 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
Step 4: Add Your Custom Domain Mappings
Use the Down Arrow key on your keyboard to navigate to the bottom of the file beneath the default entries.
Syntax for Hosts Entries
Each entry must follow this strict format: [Target IP Address] [Tab or Space] [Domain Name]
Example A: Pointing a Live Domain to a Local Development Server
If you are building a new version of mytestsite.com locally using Docker, Node.js, or Apache running on port 80/443:
127.0.0.1 mytestsite.com
127.0.0.1 www.mytestsite.com
Example B: Pointing a Domain to a Remote Staging Server
To preview a site hosted on a new cloud VPS (for example, IP 203.0.113.45) before repointing registrar nameservers:
203.0.113.45 clientproject.co.uk
203.0.113.45 staging.clientproject.co.uk
Example C: Blocking Distracting or Ad-Serving Domains Locally
You can route tracking domains or unwanted sites into a black hole (loopback address):
0.0.0.0 analytics.trackingdomain.com
Step 5: Save and Exit Nano
Once you have added your custom entries:
- Press Control + O (Write Out) on your keyboard.
- Press Return to confirm writing changes to
/etc/hosts. - Press Control + X to exit the nano editor and return to the standard shell prompt.
Step 6: Flush the macOS DNS Cache
macOS caches DNS queries aggressively via the mDNSResponder service. To ensure your browser reads the newly modified hosts file immediately rather than relying on stale cached IP addresses, flush the DNS cache:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Press Return. This command silently clears the lookup cache and restarts the responder daemon without interrupting active network connections.
Step 7: Verify the Mapping with Ping or Curl
Test that macOS resolves the domain to your custom IP address:
ping -c 2 mytestsite.com
The terminal output should display responses originating from your specified IP (such as 127.0.0.1 or 203.0.113.45), verifying that the local override is active.
Troubleshooting Common Hosts File Issues on Mac
- Browser Still Opens the Old Public Website: Modern browsers like Google Chrome and Microsoft Edge maintain internal DNS caches. Navigate to
chrome://net-internals/#dnsoredge://net-internals/#dnsand click Clear host cache. If Secure DNS (DNS over HTTPS) is enabled in your browser, it may bypass/etc/hostsentirely; temporarily disable DoH while testing local hosts. - Permission Denied When Saving: Ensure you opened the file with
sudo nano /etc/hosts. Editing withoutsudoopens the file in read-only mode. - Restoring Default Settings: If an erroneous entry breaks network resolution, restore your backup:
sudo cp /etc/hosts.backup /etc/hosts sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder