The hosts file in Windows 11 is located at C:\Windows\System32\drivers\etc\hosts and is used to map hostnames to IP addresses locally, allowing you to override DNS settings for testing, blocking websites, or configuring custom network behavior without changing router settings.
What You'll Need
- A Windows 11 PC with administrative privileges
- A text editor such as Notepad (built-in) or a third-party editor like Notepad++
- Basic familiarity with file paths and file permissions
Step-by-Step
- Press the Windows key + S to open the search bar, type "Notepad", then right-click the Notepad app and select "Run as administrator". This is required because the hosts file is protected by system permissions and cannot be edited without elevated access.
- In Notepad, click "File" in the top-left corner, then choose "Open". In the file explorer window that appears, navigate to the following path: C:\Windows\System32\drivers\etc\
- Since the hosts file has no file extension, you must change the file type filter at the bottom-right of the Open dialog from "Text Documents (*.txt)" to "All Files (*.*)" to see it. Once visible, select the "hosts" file and click "Open".
- The file will now open in Notepad. You will see lines of text, typically starting with comments marked by a # symbol. Below these, you can add entries in the format: IP_address hostname (e.g., 127.0.0.1 example.com). Each entry must be on its own line.
- After making your changes, click "File" > "Save" to overwrite the file. If you encounter a save error, ensure Notepad is still running as administrator and that the file is not locked by another process (such as antivirus software).
- To verify your changes took effect, open Command Prompt (search for "cmd" and run it normally) and type "ping example.com" (replacing example.com with the hostname you added). If the ping resolves to the IP you specified, the hosts file is working correctly.
- Optional: To flush the DNS cache and ensure Windows uses the updated hosts file immediately, run the command "ipconfig /flushdns" in Command Prompt.
Common Mistakes
- Forgetting to run Notepad as administrator, which results in being unable to save changes to the hosts file due to permission restrictions.
- Editing the wrong file, such as a hosts.txt backup or a file in a different directory, because the file type filter was not set to "All Files" when opening.
- Adding entries with incorrect formatting, such as using commas instead of spaces between the IP address and hostname, or including extra characters like quotes or brackets.
- Assuming changes take effect immediately without flushing the DNS cache, leading to confusion when testing.
- Trying to edit the hosts file from a standard user account without realizing that UAC (User Account Control) blocks access to protected system locations.
Tips
- Always create a backup of the original hosts file before editing by copying it to another location (e.g., Desktop) as hosts.backup. This allows you to restore it if something goes wrong.
- Use the hosts file to block ads or malicious sites by mapping known ad-serving domains to 127.0.0.1 (localhost), preventing them from loading.
- For development work, map local server hostnames (like mysite.local) to 127.0.0.1 to test websites locally without modifying DNS settings.
- If you're unsure whether an entry is working, use "nslookup hostname" in Command Prompt — if it returns a non-authoritative answer from your hosts file, it's being prioritized correctly.
- Some security software may monitor or block changes to the hosts file. If saves fail, temporarily disable real-time protection (only if safe to do so) or add an exception for Notepad.
Troubleshooting
If you cannot save the hosts file:
- Confirm Notepad was launched with "Run as administrator". Look for "Administrator" in the window title.
- Check if another program (like antivirus or endpoint security) has the file locked. Temporarily disable such software or check its logs for blocked access attempts.
- Ensure the file is not set to read-only. Right-click the hosts file in File Explorer, choose "Properties", and uncheck "Read-only" if enabled.
- If the file appears missing, verify you are looking in the correct directory: C:\Windows\System32\drivers\etc\. Note that on 64-bit Windows, System32 contains the 64-bit binaries — there is no separate SysWOW64 hosts file.
If pinging a hostname still returns the wrong IP:
- Double-check the spelling of the hostname in the hosts file and ensure there are no extra spaces or invisible characters.
- Run "ipconfig /displaydns" in Command Prompt to see if the entry appears in the DNS resolver cache. If not, run "ipconfig /flushdns" and try again.
- Confirm the IP address format is correct (e.g., 192.168.1.1, not 192.168.1.1. or 192.168.1).
- Test with a simple entry like "127.0.0.1 testlocal" and ping "testlocal" to isolate whether the issue is with your entry or the system.
Frequently Asked Questions
Can I edit the hosts file without administrator rights?
No, the hosts file is located in a protected system directory and requires administrative privileges to modify. Attempting to save without elevation will result in an access denied error.
Will changes to the hosts file affect all users on the PC?
Yes, the hosts file is a system-wide configuration file. Any changes made apply to all user accounts on the machine.
Is it safe to edit the hosts file?
Yes, as long as you make correct entries. Incorrect entries may cause connectivity issues, but they are easily fixed by restoring the original file or removing problematic lines.
Can I use the hosts file to block websites?
Yes, by mapping a domain to 127.0.0.1 or 0.0.0.0, you prevent the system from resolving it to its real IP address, effectively blocking access at the DNS level.
Does the hosts file work with IPv6?
Yes, you can add IPv6 addresses in the same format (e.g., ::1 localhost) to override IPv6 DNS lookups.
Why does my antivirus warn me when I edit the hosts file?
Many security programs monitor the hosts file because malware often modifies it to redirect traffic to malicious sites. A warning is normal — ensure you made the change intentionally.