If you’ve ever wanted to block websites, redirect domains, or test server changes locally, you’ll need to access a special system file called the Hosts file. This file acts like a mini-DNS system for your PC, mapping hostnames (like example.com) to specific IP addresses.
In this detailed guide, we’ll show you exactly how to locate and edit the Hosts file in Windows 11 — safely and correctly.
What Is the Hosts File in Windows?
The Hosts file is a simple text file that Windows uses to map domain names to IP addresses before reaching out to the DNS server.
For example, if your Hosts file contains this entry:
127.0.0.1 facebook.com
Every time you try to visit facebook.com, Windows will redirect it to your local machine instead of the internet — effectively blocking access.
The file is mainly used by developers, IT admins, and power users for:
- Blocking unwanted websites.
- Redirecting URLs for testing.
- Overriding DNS records temporarily.
Step 1: Locate the Hosts File in Windows 11
The Hosts file is stored deep within the System32 folder.
Here’s the full path:
C:\Windows\System32\drivers\etc\hosts
You can open it directly using File Explorer or the Run dialog.
Option 1: Using File Explorer
- Press Windows + E to open File Explorer.
- Navigate to:
C:\Windows\System32\drivers\etc - Inside the folder, you’ll see several files — including one named hosts.
Tip: The file might not show up because it has no extension. Make sure File name extensions and Hidden items are enabled in the File Explorer View menu.
Option 2: Using the Run Command
- Press Windows + R, type:
notepad C:\Windows\System32\drivers\etc\hosts - Press Enter.
However, you’ll likely need administrator privileges to edit the file — which brings us to the next step.
Step 2: Open the Hosts File as Administrator
You can’t edit the Hosts file unless you open it with admin rights.
Method 1: Using Notepad (Recommended)
- Click Start, type Notepad, and right-click it.
- Select Run as administrator.
- In Notepad, click File → Open.
- Navigate to the Hosts file location:
C:\Windows\System32\drivers\etc - Change the file type filter from Text Documents (*.txt) to All Files (.).
- Select hosts and click Open.
You can now freely edit the file.
Step 3: Edit the Hosts File
Once opened, you’ll see something like this by default:
# Copyright (c) 1993-2009 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# The IP address and the host name should be separated by at least one space.
# 127.0.0.1 localhost
# ::1 localhost
To block or redirect websites, simply add a new line at the bottom using this format:
<IP address> <domain name>
Examples:
1. Block a website:
127.0.0.1 facebook.com
127.0.0.1 www.facebook.com
2. Redirect a domain to another site or IP:
192.168.1.5 mywebsite.com
3. Map custom domains for local development:
127.0.0.1 testsite.local
Tip: Each line supports one entry. You can add as many as you need.
Step 4: Save Your Changes
After editing:
- Go to File → Save (or press Ctrl + S).
- Close Notepad.
Your changes take effect immediately — there’s no need to restart your PC.
If you get a “Permission Denied” error, make sure you opened Notepad as Administrator (Step 2).
Step 5: Flush DNS Cache (Optional but Recommended)
Windows may cache old DNS entries. To apply your Hosts file changes instantly, clear your DNS cache:
- Press Windows + S, type cmd, and select Run as administrator.
- Type the following command:
ipconfig /flushdns - Press Enter.
You’ll see a message: “Successfully flushed the DNS Resolver Cache.”
Now, when you visit a domain you modified, it will follow the new IP mappings from your Hosts file.
Step 6: Restore the Original Hosts File (If Needed)
If something goes wrong — like websites not loading properly — you can reset your Hosts file to default.
Option 1: Manually Replace It
- Delete the current hosts file from:
C:\Windows\System32\drivers\etc - Create a new file using Notepad with the following default content:
# Copyright (c) 1993-2009 Microsoft Corp. # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # The IP address and the host name should be separated by at least one space. # 127.0.0.1 localhost # ::1 localhost - Save it as hosts (no extension) in the same location.
Option 2: Use Command Prompt
Run this command in an elevated CMD window:
copy %WinDir%\System32\drivers\etc\hosts %WinDir%\System32\drivers\etc\hosts.backup
del %WinDir%\System32\drivers\etc\hosts
Then recreate it using the default template above.
Step 7: Alternative — Edit Hosts File via PowerShell
You can also modify the file quickly using PowerShell:
- Run PowerShell as Administrator.
- To open it in Notepad:
notepad "$env:WinDir\System32\drivers\etc\hosts" - To view it directly in the terminal:
Get-Content "$env:WinDir\System32\drivers\etc\hosts"
💡 Advanced tip: Developers can use automation scripts to temporarily modify the Hosts file during local testing and then revert it back afterward.
Common Issues and Fixes
| Problem | Cause | Solution |
|---|---|---|
| “Access Denied” when saving | Not running Notepad as admin | Reopen Notepad as Administrator |
| Changes not applying | DNS cache not cleared | Run ipconfig /flushdns |
| Hosts file not visible | Hidden or missing extension | Enable “File name extensions” and “Hidden items” in File Explorer |
| Site still loads despite being blocked | Browser caching | Clear your browser cache or try another browser |
Wrapping Up
Editing the Hosts file in Windows 11 gives you powerful control over how your system connects to websites and servers.
To recap:
- Locate:
C:\Windows\System32\drivers\etc\hosts - Edit: Open with Notepad as Administrator.
- Modify: Add IP and domain mappings.
- Apply: Save and flush DNS cache.
Once configured, the Hosts file can help you block distractions, test domains, or troubleshoot DNS-related problems — all without needing extra software.