How to Edit Hosts File in Windows 11 (Beginner Guide)
In this article
Editing the hosts file in Windows 11 lets you manually map domain names to specific IP addresses. This is useful for blocking websites, testing a website before changing its DNS, or speeding up network requests by bypassing DNS lookups. The hosts file is a plain text file that Windows checks before contacting a DNS server. This guide walks you through the process step by step, even if you've never edited a system file before.
What You'll Need
- A Windows 11 computer with an administrator account (you'll need admin rights to save the file).
- Notepad (or any text editor, but Notepad is built-in and works fine).
- The IP address and domain name you want to map (for example,
127.0.0.1for localhost, or a specific IP like192.168.1.10).
Step-by-Step
- Open Notepad as administrator. Click the Start button, type
notepadin the search bar, then right-click on the Notepad app and select Run as administrator. If prompted by User Account Control (UAC), click Yes. - Open the hosts file. In Notepad, go to File > Open. In the file dialog, navigate to the path:
C:\Windows\System32\drivers\etc. To get there quickly, paste that path into the address bar of the dialog and press Enter. - Change the file filter. In the bottom right of the Open dialog, make sure the file type is set to All Files (*.*). The hosts file has no extension, so it won't show up if the filter is set to Text Documents (*.txt).
- Select the hosts file. You should see a file named
hosts(no extension). Click it, then click Open. - Understand the file structure. The hosts file contains lines that are either comments (starting with
#) or mappings. Each mapping line has an IP address, followed by one or more hostnames, separated by spaces or tabs. For example:127.0.0.1 localhost. - Add your entries. Go to the bottom of the file (or anywhere after the default lines) and type your new mapping. For example, to block a website, type
127.0.0.1 www.example.com. To point a domain to a specific server, type192.168.1.10 myserver.local. Press Enter after each line. - Save the file. Go to File > Save (or press Ctrl+S). If you get a permission error, you likely didn't open Notepad as administrator. Go back to Step 1 and try again.
- Flush the DNS cache (optional but recommended). Open Command Prompt as administrator (search for
cmd, right-click, and select Run as administrator), then typeipconfig /flushdnsand press Enter. This clears the DNS cache so your changes take effect immediately. - Test your changes. Open a web browser and try to visit the domain you edited. If you mapped it to 127.0.0.1, you should get a connection error (which means the site is blocked). If you mapped it to another IP, you should see that server's content.
Common Mistakes
- Not running Notepad as administrator. This is the most common issue. Without admin rights, Windows won't let you save changes to the hosts file.
- Using the wrong file path. Make sure you're editing the file in
C:\Windows\System32\drivers\etc, not a copy somewhere else. - Adding extra spaces or symbols. Each line should be exactly: IP address, a space or tab, then the hostname. Don't add commas, semicolons, or extra spaces at the end.
- Forgetting to flush DNS. If your changes don't seem to work, it's often because the DNS cache still has the old mapping.
- Editing the wrong file. There's also a file called
hosts.txtin some folders, but that's not the one Windows uses. Stick to the exact path above.
Tips
- Back up the original file. Before making changes, copy the hosts file to your desktop (or rename it to
hosts.bak). This makes it easy to restore if something goes wrong. - Use comments to organize. Add a line starting with
#to explain what each entry does. For example:# Block social media during work hours. - Test with a single entry first. If you're new to this, add just one line, save, and test it before adding more.
- Keep the file clean. Remove old entries you no longer need, as they can cause confusion later.
- Use 127.0.0.1 to block sites. Mapping a domain to 127.0.0.1 (localhost) effectively blocks it because the browser tries to connect to your own computer, which isn't running a web server.
Troubleshooting
- Changes don't take effect. First, flush the DNS cache as described in Step 8. If that doesn't work, check that you saved the file correctly (no .txt extension) and that the line format is correct. Also, make sure you're not using a browser that has its own DNS settings (like some VPN or secure DNS features).
- Can't save the file. This almost always means you didn't open Notepad as administrator. Close Notepad, right-click it, and choose Run as administrator. If you still can't save, check if your antivirus is protecting the hosts file.
- File appears empty. If you see a blank file, you might have opened the wrong file (like a hidden hosts file with a different name). Go back to the exact path and make sure you're looking at the file named
hosts. - Websites still load despite being blocked. Some browsers use DNS over HTTPS (DoH), which bypasses the hosts file. To test, try a different browser or temporarily disable DoH in your browser settings.
- System seems slow or unstable. If you added a lot of entries or made a typo, it could cause issues. Restore the backup you made and test again.
Frequently Asked Questions
Do I need to restart my computer after editing the hosts file?
No, you only need to flush the DNS cache with ipconfig /flushdns. However, if you're blocking a site that's already open in your browser, you may need to close and reopen the browser.
Can I edit the hosts file with any text editor?
Yes, but you must run the editor as administrator. Notepad is the simplest, but other editors like Visual Studio Code or Notepad++ work too.
What does 127.0.0.1 mean?
It's the loopback address, which points to your own computer. When you map a domain to 127.0.0.1, the browser tries to connect to your local machine, which usually fails, effectively blocking the site.
Is editing the hosts file permanent?
Yes, until you remove the lines you added. The changes are saved in the file and persist across reboots.
Can I use the hosts file to speed up my internet?
In theory, yes, by mapping frequently visited sites to their IP addresses, you skip DNS lookup. But in practice, the speed gain is negligible, and it's not worth the maintenance effort.
What if I mess up the hosts file?
If you have a backup, restore it. If not, you can reset the hosts file to the default Windows version by copying the default content from a Microsoft support page or by using a system restore point.