Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code: Skills Get Started
FAQ’s Sections
DNS Linux

The macOS Hosts File: The Complete Guide to Local DNS Control

Whether you're a developer testing a new site before launch, troubleshooting a server migration, or simply blocking distracting websites, the macOS hosts file is one of the most powerful — and most underused — tools in your arsenal. This comprehensive guide walks you through everything you need to know: what the hosts file is, where to find it, how to edit it safely, and how to leverage it for real-world workflows like staging server testing and local development environments.

What Is the Hosts File?

The hosts file is a plain-text system file that maps human-readable hostnames (like example.com) to specific IP addresses. Before your Mac sends a DNS query to an external resolver, it checks the local hosts file first. If a matching entry exists, it uses that IP address — no DNS server involved.

This makes the hosts file a kind of local DNS override layer, giving you granular control over how your machine resolves domain names. It operates silently in the background, and when used correctly, it's an incredibly efficient tool for developers and system administrators alike.

Core Use Cases at a Glance

Use CaseWhat It Does
Website BlockingMaps a domain to 0.0.0.0 to prevent access
Local DevelopmentMaps a custom domain to 127.0.0.1 for localhost testing
DNS Testing / MigrationPoints a live domain to a new server IP before DNS propagates
Staging EnvironmentsPreviews a site on a new host without changing public DNS
Security HardeningBlocks known malicious or ad-serving domains

Where Is the Hosts File Located on macOS?

On macOS, the hosts file lives in the /etc/ directory. The full path is:

/etc/hosts

This is a protected system file, which means you need administrator (root) privileges to modify it. You cannot simply double-click it and start editing — you must use the Terminal with elevated permissions via sudo.

> Note: The /etc/ directory on macOS is actually a symbolic link to /private/etc/, so /etc/hosts and /private/etc/hosts point to the same file.

How to Edit the Hosts File on macOS: Step-by-Step

Step 1 — Open Terminal

Launch the Terminal application. You can do this in two ways:

  • Spotlight Search: Press Command + Space, type Terminal, and hit Enter
  • Finder: Navigate to Applications > Utilities > Terminal

Step 2 — Open the Hosts File with Elevated Privileges

Use the nano text editor with sudo to open the hosts file:

sudo nano /etc/hosts

You will be prompted to enter your macOS administrator password. Type it and press Enter. Note that the password field will remain blank as you type — this is normal behavior for sudo in Terminal.

Step 3 — Understand the Default Entries

Once the file opens in nano, you'll see the default entries that macOS ships with:

##
# 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

Do not delete or modify these default entries. They are required for your system to function correctly. All custom entries should be added below these lines.

Step 4 — Add Your Custom Entries

The syntax for a hosts file entry is straightforward:

<IP address>    <hostname>    [optional alias]

Each entry must be on its own line. Here are practical examples:

Block a website:

0.0.0.0    facebook.com
0.0.0.0    www.facebook.com

Map a local development domain:

127.0.0.1    myproject.local
127.0.0.1    myproject.dev

Point a domain to a specific server for migration testing:

192.168.1.100    mywebsite.com
192.168.1.100    www.mywebsite.com

You can also add inline comments using the # character to keep your hosts file organized:

# --- Local Development Projects ---
127.0.0.1    project-alpha.local
127.0.0.1    project-beta.local

# --- Migration Testing: New Server IP 203.0.113.50 ---
203.0.113.50    clientsite.com
203.0.113.50    www.clientsite.com

Step 5 — Save the File

Once you've made your changes in nano:

  1. Press Control + O to write (save) the file
  2. Press Enter to confirm the filename
  3. Press Control + X to exit nano

Step 6 — Flush the DNS Cache

Editing the hosts file alone isn't enough — macOS caches DNS lookups, so you need to flush the DNS cache to force your system to recognize the new entries immediately.

Run the following command in Terminal:

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

This command does two things:

  • dscacheutil -flushcache — clears the local DNS cache
  • killall -HUP mDNSResponder — restarts the mDNSResponder process, which handles DNS resolution on macOS

After running this, open your browser and test the domain. Changes should take effect immediately.

> macOS Version Note: The flush command above works on macOS Monterey, Ventura, Sonoma, and Sequoia. On older versions (pre-Yosemite), the command may differ slightly.

Real-World Use Cases in Depth

Use Case 1: Blocking Websites

The hosts file is one of the simplest and most effective ways to block distracting or harmful websites at the system level — no third-party software required.

By mapping a domain to 0.0.0.0 (a non-routable address), you prevent your browser from ever reaching the site:

0.0.0.0    reddit.com
0.0.0.0    www.reddit.com
0.0.0.0    twitter.com
0.0.0.0    www.twitter.com

Unlike browser extensions, this block applies system-wide — it affects every browser and application on your Mac.

Use Case 2: Local Development Environments

If you're building websites or web applications locally, the hosts file lets you assign custom domain names to your localhost environment instead of using the generic http://localhost or http://127.0.0.1.

127.0.0.1    myshop.local
127.0.0.1    clientproject.local
127.0.0.1    wordpress-staging.local

This is especially useful when working with WordPress, Joomla, or Laravel projects that use absolute URLs or cookie domains tied to a specific hostname. Pair this with a local web server stack (like MAMP, Laravel Valet, or a Docker container) and you have a professional local development workflow.

If you're running your development environment on a remote VPS Hosting instance rather than locally, the same principle applies — you can point a domain directly to your VPS IP for testing before updating public DNS records.

Use Case 3: Testing Server Migrations Without DNS Propagation

This is arguably the most valuable use case for developers and sysadmins. When you migrate a website to a new server — whether you're switching hosting providers or upgrading your infrastructure — DNS propagation can take anywhere from a few minutes to 48 hours.

The hosts file lets you preview the site on the new server immediately, from your own machine, without affecting anyone else.

Example scenario: You're migrating mywebsite.com to a new Dedicated Server with the IP address 203.0.113.50. Add this entry:

203.0.113.50    mywebsite.com
203.0.113.50    www.mywebsite.com

Now, when you type mywebsite.com in your browser, your Mac loads the site from the new server — even though public DNS still points to the old one. This allows you to:

  • Verify all pages load correctly
  • Check that SSL certificates are properly installed
  • Test contact forms, checkout flows, and dynamic functionality
  • Confirm database connections and media files are intact

Once you're satisfied everything works, update the public DNS records. The transition will be seamless for your visitors.

Use Case 4: Bypassing DNS for Troubleshooting

Sometimes DNS issues can make a site inaccessible even when the server itself is running fine. The hosts file lets you bypass DNS entirely and connect directly to a known IP address to diagnose the problem.

This is particularly useful when:

  • Your domain's DNS records are misconfigured
  • You're testing a new SSL Certificates installation before DNS has propagated
  • You need to verify that a server is responding correctly while DNS issues are being resolved

Use Case 5: Testing Email Hosting Configurations

If you're setting up or migrating Email Hosting and need to verify that your mail server is reachable at a specific IP before DNS records update, you can temporarily map your mail domain in the hosts file to test connectivity and configuration.

Verifying Your Hosts File Changes

Before flushing the DNS cache, it's good practice to verify your entry was saved correctly. You can view the current contents of the hosts file without editing it using:

cat /etc/hosts

You can also use the ping command to verify a hostname resolves to the expected IP:

ping mywebsite.com

The output should show the IP address you mapped in the hosts file.

Restoring the Default Hosts File

If your hosts file becomes cluttered or something goes wrong, you can reset it to its default state. Open the file with sudo nano /etc/hosts, remove all custom entries, and ensure the file contains only the original defaults:

##
# 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

Save the file, exit nano, and flush the DNS cache using the command from Step 6. Your system will return to using standard DNS resolution for all domains.

Security Considerations

While the hosts file is a powerful tool, keep these security best practices in mind:

  • Limit access: Only administrators should have write access to /etc/hosts. Verify permissions with ls -la /etc/hosts — it should show -rw-r--r--.
  • Remove test entries after use: Don't leave migration testing entries in place after you've finished. They can cause confusion and unexpected behavior later.
  • Be aware of malware: Some malware modifies the hosts file to redirect legitimate domains to malicious servers. Periodically review your hosts file to ensure no unauthorized entries exist.
  • Use version control for teams: If multiple developers share a development environment, consider documenting your hosts file entries in a shared README or version-controlled configuration file.

Quick Reference: Essential Commands

TaskCommand
Open hosts file for editingsudo nano /etc/hosts
View hosts file (read-only)cat /etc/hosts
Flush DNS cache (macOS Ventura/Sonoma)sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Check file permissionsls -la /etc/hosts
Verify hostname resolutionping yourdomain.com
Save in nanoControl + O, then Enter
Exit nanoControl + X

Frequently Asked Questions

Do I need to restart my Mac after editing the hosts file?

No. Flushing the DNS cache with the dscacheutil command is sufficient. A restart is not required.

Will changes to the hosts file affect other users on my Mac?

Yes. The hosts file is system-wide, so changes affect all user accounts on the same machine.

Does the hosts file work with HTTPS?

Yes, but with a caveat. The hosts file only controls IP resolution — it does not handle SSL/TLS certificates. If you redirect a domain to a new server via the hosts file and the SSL certificate on that server doesn't match the domain, your browser will show a certificate warning.

Can I use wildcards in the hosts file?

No. The macOS hosts file does not support wildcard entries. Each hostname must be listed explicitly on its own line.

Will editing the hosts file affect my VPN?

It depends on your VPN configuration. Some VPNs override local DNS settings, which may cause hosts file entries to be ignored while the VPN is active.

Conclusion: Take Full Control of Your Local DNS

The macOS hosts file is a deceptively simple yet remarkably powerful tool. With a single text file and a few Terminal commands, you can block distracting websites, build professional local development environments, and test server migrations with complete confidence — all without waiting for DNS propagation or touching your live infrastructure.

For developers working with VPS Control Panels or managing sites on Shared Web Hosting, mastering the hosts file is a foundational skill that will save you hours of troubleshooting time. The next time you're preparing a site migration or spinning up a new development project, reach for /etc/hosts first — it's the fastest, most reliable way to control how your Mac resolves any domain on the internet.