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.localin 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:
- You have administrator privileges on the computer. Without them, you won't be able to save changes in the system folder.
- You have a simple text editor (Notepad, Notepad++, VS Code).
- You understand which IP address and domain you want to map.
- (Optional) Create a backup of the original
hostsfile. Simply copy it to another location (like your Desktop) and name ithosts.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:
- Open File Explorer and navigate to
C:\Windows\System32\drivers\etc\. - You will see the
hostsfile (without the.txtextension). If the.txtextension is visible, it means file extensions are shown, and the file will be namedhosts.txt. - Do not double-click the file! This won't work because it has no file association. Instead:
- Right-click the
hostsfile. - 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).
- Right-click the
- 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 thehostsfile.
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 siteads-site.com.192.168.1.100 myproject.local— redirectsmyproject.localto a local server at IP192.168.1.100.0.0.0.0 tracking.net— an alternative blocking method (the address0.0.0.0also 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:
- In Notepad, click "File" → "Save" (or press
Ctrl+S). - In the "Save as type" dropdown, select "All files (.)".
- Ensure the "File name" field contains just
hosts(without.txt). - 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.
- 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.
- Open Command Prompt (cmd) or PowerShell as an administrator.
- Type the command:
ipconfig /flushdns - 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)
- Open Command Prompt (admin rights not required).
- Run:
Replaceping example.comexample.comwith the domain you added to hosts. - 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_REFUSEDor 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.comvia127.0.0.1, the browser will try to establish an HTTPS connection with127.0.0.1and 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 namedhostswith 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).