Windows

Editing the hosts File in Windows: A Complete Guide

This guide explains in detail how to locate, edit, and save the system hosts file in Windows. You will learn how to redirect domain names to local IP addresses or block unwanted websites.

Updated at February 16, 2026
10-15 min
Medium
FixPedia Team
Применимо к:Windows 10Windows 11Windows Server 2019/2022

Introduction / Why You Need This

The hosts file is a local text file in Windows that allows you to manually map domain names (like site.com) to IP addresses. This system has existed since the early days of the internet and operates before DNS server lookups.

Primary uses:

  • Block unwanted websites (ads, malicious domains) by redirecting them to 127.0.0.1.
  • Local development and testing — for example, to make myapp.local in a browser open your local web server (127.0.0.1:8080).
  • Redirect domains to a different server without changing DNS.

This guide will show you how to safely edit this system file in Windows 10 and 11.

Requirements / Preparation

Before you begin, ensure:

  1. You have administrator privileges on the computer. Without them, you won't be able to save changes in the system folder.
  2. You have a simple text editor (Notepad, Notepad++, VS Code).
  3. You understand which IP address and domain you want to map.
  4. (Optional) Create a backup of the original hosts file. Simply copy it to another location (like your Desktop) and name it hosts.backup.

Step 1: Find and Open the hosts File with Administrator Rights

The hosts file is located in a protected system folder. The direct path is: C:\Windows\System32\drivers\etc\.

How to open:

  1. Open File Explorer and navigate to C:\Windows\System32\drivers\etc\.
  2. You will see the hosts file (without the .txt extension). If the .txt extension is visible, it means file extensions are shown, and the file will be named hosts.txt.
  3. Do not double-click the file! This won't work because it has no file association. Instead:
    • Right-click the hosts file.
    • Select "Open with""Choose another app".
    • In the list, find and select Notepad (or your editor).
    • IMPORTANT: Before clicking "OK", be sure to check the box "Always use this app to open .txt files" (even if the file has no extension, the system treats it as a text file).
  4. An alternative (and often more convenient) method:
    • Open Notepad.
    • In the menu, select "File" → "Open".
    • In the dialog box, set "File type" to "All files (.)".
    • Navigate to the folder C:\Windows\System32\drivers\etc\ and select the hosts file.

If you get an "Access denied" error, it means Notepad was launched without administrator rights. Close it, find Notepad in the Start menu, right-click it, and select "Run as administrator", then open the file through it.

Step 2: Add the Necessary Entry

The opened hosts file contains several comment lines (starting with #) and possibly some default entries. Anything starting with # is ignored by the system.

Entry format:

<IP-address><space><domain_name>

Examples:

  • 127.0.0.1 localhost — standard entry for the local computer (usually already present).
  • 127.0.0.1 ads-site.com — blocks the site ads-site.com.
  • 192.168.1.100 myproject.local — redirects myproject.local to a local server at IP 192.168.1.100.
  • 0.0.0.0 tracking.net — an alternative blocking method (the address 0.0.0.0 also means "nothing").

Important rules:

  • Each entry on a new line.
  • At least one space or tab between the IP and the domain.
  • Do not add extra characters, especially after the domain.
  • To block subdomains (e.g., sub.ads-site.com), you must list each subdomain separately or use masking via a local DNS server (the hosts file does not support * wildcards).

Step 3: Save the File in the Correct Encoding

This is the most common reason changes don't work! By default, Notepad saves files in UTF-8 with BOM encoding, which the system may misinterpret.

How to save correctly:

  1. In Notepad, click "File" → "Save" (or press Ctrl+S).
  2. In the "Save as type" dropdown, select "All files (.)".
  3. Ensure the "File name" field contains just hosts (without .txt).
  4. Click the "Encoding" dropdown and select ANSI (for English Windows) or UTF-8 (for Russian Windows, but without BOM — in modern Notepad versions, selecting UTF-8 saves without BOM by default). If in doubt — choose ANSI.
  5. Click "Save". If the system asks to confirm replacement — agree.

If you are using Notepad++:

  • After editing, select "Encoding" → "Convert to ANSI".
  • Then save (Ctrl+S). The file will be overwritten correctly.

Step 4: Flush the DNS Cache

Windows caches DNS query results to speed up operations. After modifying the hosts file, old entries might still be used.

  1. Open Command Prompt (cmd) or PowerShell as an administrator.
  2. Type the command:
    ipconfig /flushdns
    
  3. You should see the message: Successfully flushed the DNS resolution cache.

Note: On some systems (especially with antivirus software), a reboot may be required, but in most cases ipconfig /flushdns is sufficient.

Step 5: Verify the Result

Method 1: Ping (cmd)

  1. Open Command Prompt (admin rights not required).
  2. Run:
    ping example.com
    
    Replace example.com with the domain you added to hosts.
  3. The response should show the IP address you specified in the file (e.g., 127.0.0.1), not the site's real IP.

Method 2: Browser

  • Open your browser and navigate to the domain.
  • If you blocked the site — it should not load (an ERR_CONNECTION_REFUSED or similar error).
  • If you redirected to a local server — your local project should open.

Method 3: View Current Resolution (nslookup)

nslookup example.com

This command shows which DNS server provided the answer. However, note that nslookup may not consider the hosts file, as it uses its own query mechanism. It's better to rely on ping.

Potential Issues

❌ "Access denied" when saving

  • Cause: Notepad was launched without administrator rights.
  • Solution: Close the editor, restart it via "Run as administrator".

❌ Changes don't take effect after ipconfig /flushdns

  • Check syntax: Ensure there are no extra characters after the domain and that there is a space between the IP and domain.
  • Check encoding: The file must be in ANSI or UTF-8 without BOM.
  • Antivirus/Windows Defender: Some antiviruses (Avast, AVG, Kaspersky) or the built-in Defender may "restore" the hosts file. Temporarily disable hosts protection in your antivirus settings.
  • Proxy servers/Firewall: If a corporate proxy is used, local hosts rules may be ignored.

❌ Blocking doesn't work for HTTPS sites

  • Cause: The hosts file operates at the IP level. If you block https://site.com via 127.0.0.1, the browser will try to establish an HTTPS connection with 127.0.0.1 and receive an SSL error (certificate mismatch). This is normal and confirms the block.
  • For complete blocking (to prevent even connection attempts), use the address 0.0.0.0.

❌ Doesn't work for subdomains

  • The hosts file does not support wildcards (*.site.com). You need to list each subdomain separately. For mass blocking, consider using specialized programs (like HostsMan) or firewall settings.

❌ Site still loads

  • Check for typos in the domain.
  • Check if a higher-priority rule overrides your entry (e.g., VPN or network DNS server settings). As a last resort, try temporarily disabling your VPN/antivirus.
  • Ensure you didn't save the file as hosts.txt. The system looks for a file named hosts with no extension.

❌ System "deleted" my entries after reboot

  • Cause: Malware activity or some system utilities (like Microsoft's "Disk Cleanup") may reset the hosts file.
  • Solution: Regularly check system integrity, use antivirus software. For permanent blocking, consider using a firewall (Windows Defender Firewall) or browser extensions (uBlock Origin).

F.A.Q.

Why aren't changes to the hosts file being applied?
How to restore the hosts file to its original state?
Antivirus is blocking the hosts file save. What to do?
How does the hosts file differ from a DNS server?

Hints

Find and open the hosts file with administrator privileges
Add the necessary entry
Save the file in the correct encoding
Clear the DNS cache
Check the result
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community