The Hosts File: What It Is, Where to Find It, and How to Edit It on Any OS
Your Local DNS Override — No Internet Required
Every time you type a domain name into your browser, your computer quietly runs a lookup process to translate that name into an IP address. But before it ever contacts an external DNS server, it checks a small, often-overlooked text file sitting quietly on your local system: the hosts file.
This file is one of the most powerful and underappreciated tools available to developers, system administrators, and power users alike. It lets you manually map domain names to IP addresses — instantly, locally, and without touching your router or DNS provider. Whether you're testing a new website before it goes live, blocking ad servers, or troubleshooting DNS propagation issues, the hosts file gives you direct, low-level control over how your machine resolves domain names.
In this guide, you'll learn exactly what the hosts file is, where to find it on Windows, macOS, and Linux, and how to edit it safely and effectively.
—
What Is the Hosts File?
The hosts file is a plain-text system file that maps hostnames (domain names) to IP addresses. It predates the modern DNS system and was originally the only mechanism used to resolve names on ARPANET. Today, it still functions as a local DNS override layer — your OS checks it first before making any external DNS queries.
How It Works
When you navigate to a website, your operating system follows this resolution order:
- Check the local hosts file for a matching entry
- Query the local DNS cache
- Contact the configured DNS resolver (e.g., your ISP's DNS or a public resolver like 8.8.8.8)
If the hosts file contains a matching entry, the lookup stops there. No external DNS query is made. This makes it extremely fast and useful for local overrides.
Common Use Cases for the Hosts File
| Use Case | How It Works |
|---|---|
| Local website testing | Point a domain to 127.0.0.1 or a staging server IP |
| Block unwanted websites | Redirect ad or tracking domains to 0.0.0.0 |
| Bypass DNS propagation delays | Manually set the IP for a newly migrated domain |
| Development environment routing | Map custom local domains to Docker containers or VMs |
| Override broken DNS entries | Force a correct IP when DNS is misconfigured |
> Pro Tip for Developers: If you're running a local development environment or testing a site on a VPS Hosting server before DNS propagation completes, editing the hosts file is the fastest way to preview your live site without changing your DNS settings.
—
Where Is the Hosts File Located?
The hosts file location varies by operating system. Here's a quick reference:
Windows
C:WindowsSystem32driversetchostsmacOS
/etc/hostsLinux (All Major Distributions)
/etc/hostsOn macOS and Linux, /etc/hosts is typically a symlink or a direct file managed by the system. On Linux distributions using systemd-resolved, the file still takes priority for local overrides.
—
How to Edit the Hosts File on Windows
Editing the hosts file on Windows requires administrator privileges. The file is protected by the system, so standard user accounts cannot modify it.
Step 1: Open Notepad as Administrator
- Press Windows Key + S and type
Notepadin the search bar - Right-click on Notepad in the results
- Select Run as administrator
- Click Yes if prompted by User Account Control (UAC)
Step 2: Open the Hosts File
- In Notepad, click File → Open
- Navigate to:
C:WindowsSystem32driversetc - In the file type dropdown (next to the filename field), change it from
Text Documents (*.txt)to All Files (*.*) - Select the file named
hostsand click Open
Step 3: Add or Modify Entries
The format for each entry is simple:
[IP Address] [Hostname]Add your entries at the bottom of the file. For example:
# Redirect example.com to local development server
127.0.0.1 example.com
# Point testsite.com to a staging VPS
192.168.1.100 testsite.com
# Block an ad network
0.0.0.0 ads.unwanteddomain.comLines beginning with # are comments and are ignored by the system.
Step 4: Save the File
Click File → Save. If you receive a permissions error, ensure you opened Notepad as administrator. Do not save the file with a .txt extension.
Step 5: Flush the DNS Cache
After saving, flush the DNS cache so your changes take effect immediately:
- Open Command Prompt as administrator
- Run the following command:
ipconfig /flushdnsYou should see the message: *"Successfully flushed the DNS Resolver Cache."*
—
How to Edit the Hosts File on macOS
On macOS, the hosts file is edited via the Terminal using a text editor with sudo (superuser) privileges.
Step 1: Open Terminal
Press Command + Space to open Spotlight Search, type Terminal, and press Enter.
Step 2: Open the Hosts File with nano
Run the following command:
sudo nano /etc/hostsEnter your administrator password when prompted. The password will not be visible as you type — this is normal.
Step 3: Edit the File
The nano text editor will open the hosts file. Use the arrow keys to navigate to the bottom of the file and add your entries:
# Development environment
127.0.0.1 myproject.local
# Staging server
203.0.113.45 staging.example.com
# Block tracking domain
0.0.0.0 tracker.example.netStep 4: Save and Exit
- Press Control + O to write (save) the file
- Press Enter to confirm the filename
- Press Control + X to exit nano
Step 5: Flush the DNS Cache on macOS
Run the appropriate command for your macOS version:
macOS Monterey, Ventura, Sonoma (and most modern versions):
sudo killall -HUP mDNSRespondermacOS Big Sur and earlier (alternative):
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponderYour changes will now be active system-wide.
—
How to Edit the Hosts File on Linux
Linux follows the same general process as macOS, since both are Unix-based systems. You'll need root or sudo access.
Step 1: Open the Terminal
Launch your Terminal application. On most desktop environments, you can find it in your applications menu or press Ctrl + Alt + T.
Step 2: Open the Hosts File
sudo nano /etc/hostsEnter your root or sudo password when prompted.
Step 3: Add Your Entries
Navigate to the bottom of the file and add your custom mappings:
# Local dev environment
127.0.0.1 devsite.local
# Internal server
10.0.0.5 internal.company.com
# Block unwanted domain
0.0.0.0 malicious-ads.example.comStep 4: Save and Exit
- Press Control + O, then Enter to save
- Press Control + X to exit nano
Step 5: Flush the DNS Cache on Linux
The command to flush DNS varies by distribution and DNS resolver:
Ubuntu / Debian (with systemd-resolved):
sudo systemd-resolve --flush-cachesOr on newer versions:
sudo resolvectl flush-cachesCentOS / RHEL / Fedora:
sudo systemctl restart NetworkManagerArch Linux:
sudo systemd-resolve --flush-cachesIf you're running a custom DNS caching daemon like nscd or dnsmasq, restart the relevant service:
sudo systemctl restart nscd
# or
sudo systemctl restart dnsmasq—
Understanding the Default Hosts File Structure
When you first open the hosts file on any OS, you'll see some default entries. Here's what a typical Linux/macOS hosts file looks like out of the box:
##
# 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 localhostKey entries explained:
127.0.0.1 localhost— Maps the loopback address to "localhost". This is essential for local networking and should never be removed.::1 localhost— The IPv6 equivalent of the loopback address.255.255.255.255 broadcasthost— Used for network broadcasting (macOS-specific).
Never delete these default entries. Doing so can break local application networking, database connections, and other system services.
—
Best Practices for Editing the Hosts File
1. Always Create a Backup First
Before making any changes, back up the original file:
Linux / macOS:
sudo cp /etc/hosts /etc/hosts.backupWindows (Command Prompt as Administrator):
copy C:WindowsSystem32driversetchosts C:WindowsSystem32driversetchosts.backupIf something breaks, you can restore the original immediately.
2. Use Comments to Document Your Changes
Always annotate your entries with comments using #. This is especially important in team or server environments:
# Added 2024-01-15 — Staging environment for client project
192.168.1.50 staging.clientproject.com
# Temporary block — remove after campaign ends
0.0.0.0 ads.campaign-tracker.net3. Use 0.0.0.0 Instead of 127.0.0.1 for Blocking
When blocking domains, 0.0.0.0 is generally preferred over 127.0.0.1 because:
- It doesn't attempt a connection to your local machine
- It fails faster (no connection timeout)
- It doesn't interfere with local web servers running on port 80/443
4. Don't Block Critical System Domains
Be cautious about blocking domains related to:
- Windows Update or macOS Software Update services
- Antivirus update servers
- Cloud sync services (Dropbox, OneDrive, iCloud)
- License validation servers for software you use
5. Keep Entries Organized
Group related entries together and use comment headers for clarity:
# ==========================================
# LOCAL DEVELOPMENT ENVIRONMENTS
# ==========================================
127.0.0.1 myapp.local
127.0.0.1 api.myapp.local
# ==========================================
# BLOCKED DOMAINS
# ==========================================
0.0.0.0 ads.example.com
0.0.0.0 tracker.example.net6. Remember That Hosts File Changes Are Local Only
The hosts file only affects the machine it's on. If you're managing multiple servers or need DNS changes that apply across your infrastructure, you'll need to configure DNS at the server or network level. For teams managing multiple environments, a properly configured Dedicated Server with a local DNS resolver (like BIND or Unbound) is a more scalable solution.
—
Advanced Use Cases
Testing a Website Before DNS Propagation
When you migrate a website to a new hosting provider or a new VPS with cPanel, DNS propagation can take anywhere from a few minutes to 48 hours. Instead of waiting, you can add the new server's IP to your hosts file and immediately test the live site as it will appear on the new server:
# Testing migration to new VPS — remove after DNS propagates
203.0.113.10 yourdomain.com
203.0.113.10 www.yourdomain.comOpen your browser, navigate to yourdomain.com, and you'll see the site as hosted on the new server — while everyone else still sees the old one.
Setting Up Local Development Domains
Developers often prefer using realistic domain names for local projects rather than localhost:3000. You can create custom local domains:
127.0.0.1 myproject.dev
127.0.0.1 api.myproject.dev
127.0.0.1 admin.myproject.devPair this with a local web server (Apache, Nginx, or Caddy) configured with virtual hosts, and you have a clean, professional local development environment.
Blocking Entire Ad Networks
You can add multiple entries to block known ad-serving and tracking domains:
0.0.0.0 doubleclick.net
0.0.0.0 googlesyndication.com
0.0.0.0 adservice.google.comFor a more comprehensive approach, projects like StevenBlack's hosts provide regularly updated, consolidated hosts files with tens of thousands of blocked domains.
—
Troubleshooting Common Hosts File Issues
Changes Not Taking Effect
- Did you flush the DNS cache? This is the most common cause. Run the appropriate flush command for your OS.
- Did you save the file correctly? On Windows, ensure you didn't accidentally save it as
hosts.txt. - Is your browser using its own DNS cache? Chrome and Firefox maintain their own DNS caches. Try clearing them or opening a private/incognito window.
- Are you using a VPN? VPN clients often override local DNS settings, bypassing the hosts file entirely.
Permission Denied Errors
- Windows: Ensure Notepad (or your editor) is running as Administrator.
- Linux/macOS: Ensure you're using
sudobefore your command.
Website Still Resolving to Old IP
- Verify the entry is formatted correctly:
IP_ADDRESS[TAB or SPACES]hostname— nohttp://or trailing slashes. - Check for typos in the domain name.
- Confirm there are no conflicting entries higher in the file.
Accidentally Broke Something
Restore your backup:
# Linux/macOS
sudo cp /etc/hosts.backup /etc/hosts
# Windows (as Administrator)
copy C:WindowsSystem32driversetchosts.backup C:WindowsSystem32driversetchosts—
Hosts File vs. DNS: When to Use Each
| Scenario | Hosts File | DNS Configuration |
|---|---|---|
| Quick local test on one machine | ✅ Ideal | ❌ Overkill |
| Team-wide dev environment | ❌ Doesn't scale | ✅ Use internal DNS |
| Blocking sites on a single device | ✅ Works well | ❌ Unnecessary |
| Production domain management | ❌ Not appropriate | ✅ Required |
| Pre-launch site testing | ✅ Perfect | ❌ Would affect all users |
| Multi-server infrastructure | ❌ Too manual | ✅ Essential |
For production environments, always manage DNS through your domain registrar or hosting control panel. If you need to register or manage domains professionally, Domain Registration through a reliable provider ensures your DNS records are propagated correctly and securely across the internet.
Similarly, if you're running a mail server and need to ensure proper email delivery, editing the hosts file is not a substitute for correctly configured MX records. Explore dedicated Email Hosting solutions for reliable, properly configured mail infrastructure.
—
Quick Reference: Hosts File Cheat Sheet
File Locations
| OS | Path |
|---|---|
| Windows | C:WindowsSystem32driversetchosts |
| macOS | /etc/hosts |
| Linux | /etc/hosts |
Open for Editing
| OS | Command / Method |
|---|---|
| Windows | Notepad → Run as Administrator → File → Open |
| macOS | sudo nano /etc/hosts |
| Linux | sudo nano /etc/hosts |
Flush DNS Cache
| OS | Command |
|---|---|
| Windows | ipconfig /flushdns |
| macOS | sudo killall -HUP mDNSResponder |
| Ubuntu/Debian | sudo systemd-resolve --flush-caches |
| CentOS/RHEL | sudo systemctl restart NetworkManager |
Entry Format
[IP Address] [hostname] [optional alias]
# Examples:
127.0.0.1 localhost
127.0.0.1 myapp.local
0.0.0.0 blocked-site.com
192.168.1.10 staging.example.com—
Conclusion: Master Your Local DNS with the Hosts File
The hosts file is a deceptively simple tool with surprisingly broad utility. In just a few lines of plain text, you can redirect domains, block unwanted content, test new server configurations, and troubleshoot DNS issues — all without touching your router, DNS provider, or network infrastructure.
The key takeaways:
- Always back up the hosts file before editing
- Use comments to document every change you make
- Flush your DNS cache after every edit to ensure changes apply immediately
- Use
0.0.0.0rather than127.0.0.1when blocking domains - Remember it's local — hosts file changes only affect the machine they're made on
For developers and sysadmins working with hosted environments, the hosts file pairs perfectly with a well-configured server. Whether you're testing a new application on a VPS Hosting plan, previewing a site on a Shared Web Hosting account before going live, or validating SSL configuration after installing an SSL Certificate, the hosts file gives you the local control you need to work confidently and efficiently.
Now open that file, make your changes carefully, flush your cache, and take full control of how your machine sees the internet.
