How to Locate and Edit the Hosts File on Windows 11

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

  1. Press Windows + E to open File Explorer.
  2. Navigate to: C:\Windows\System32\drivers\etc
  3. 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

  1. Press Windows + R, type: notepad C:\Windows\System32\drivers\etc\hosts
  2. 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)

  1. Click Start, type Notepad, and right-click it.
  2. Select Run as administrator.
  3. In Notepad, click File → Open.
  4. Navigate to the Hosts file location: C:\Windows\System32\drivers\etc
  5. Change the file type filter from Text Documents (*.txt) to All Files (.).
  6. 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:

  1. Go to File → Save (or press Ctrl + S).
  2. 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:

  1. Press Windows + S, type cmd, and select Run as administrator.
  2. Type the following command: ipconfig /flushdns
  3. 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

  1. Delete the current hosts file from: C:\Windows\System32\drivers\etc
  2. 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
  3. 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:

  1. Run PowerShell as Administrator.
  2. To open it in Notepad: notepad "$env:WinDir\System32\drivers\etc\hosts"
  3. 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

ProblemCauseSolution
“Access Denied” when savingNot running Notepad as adminReopen Notepad as Administrator
Changes not applyingDNS cache not clearedRun ipconfig /flushdns
Hosts file not visibleHidden or missing extensionEnable “File name extensions” and “Hidden items” in File Explorer
Site still loads despite being blockedBrowser cachingClear 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.

Posted by Arpita

With a background in Computer Science, she is passionate about sharing practical programming tips and tech know-how. From writing clean code to solving everyday tech problems, she breaks down complex topics into approachable guides that help others learn and grow.