How to Edit Hosts File in Windows 10
Editing the hosts file in Windows 10 lets you manually map domain names to IP addresses, overriding DNS for specific sites. This is useful for blocking websites, testing a web server before DNS changes, or speeding up local network access. You’ll need administrator rights on your PC, and it’s a good idea to have the IP address and domain name you want to use ready.
What You’ll Need
- Administrator account on the Windows 10 PC
- The IP address you want to map (e.g., 127.0.0.1 for localhost)
- The domain name you want to map (e.g., example.com)
- A text editor like Notepad (built-in) or Notepad++
Step-by-Step
- Open Notepad as Administrator. Click the Start button, type
Notepad, right-click the Notepad result, and choose Run as administrator. If prompted by User Account Control, click Yes. - Open the hosts file. In Notepad, click File > Open. In the file dialog, navigate to
C:\Windows\System32\drivers\etc. Change the file type filter from “Text Documents (*.txt)� to All Files (*.*) so the hosts file appears. Select the file namedhostsand click Open. - Review the existing content. The hosts file usually contains several lines starting with
#(comments) and a default line127.0.0.1 localhost. Do not delete these unless you know what you’re doing. - Add your mapping. Go to the end of the file and add a new line. Type the IP address, press Tab or Space, then type the domain name. For example:
127.0.0.1 facebook.com. To block a site, use0.0.0.0as the IP. For testing a local server, use127.0.0.1. - Add multiple entries (optional). If you need to map several domains, put each on a separate line. You can also map a domain to multiple IPs by adding multiple lines, but only the first match is used.
- Save the file. Click File > Save (or press Ctrl+S). If you get a “You don’t have permission to save� error, you likely didn’t open Notepad as administrator. Close and repeat step 1.
- Flush the DNS cache (recommended). Open Command Prompt as administrator (type
cmdin Start, right-click, Run as administrator) and typeipconfig /flushdns. Press Enter. This clears cached DNS so your changes take effect immediately. - Test the change. Open a browser and go to the domain you mapped. If you blocked it, you should see an error or nothing. If you mapped to a local server, you should see your server’s page.
Common Mistakes
- Forgetting to run Notepad as administrator. You’ll get a permission error when saving. Always use “Run as administrator�.
- Using the wrong file path. The hosts file is in
C:\Windows\System32\drivers\etc, not in the Windows root folder. - Adding a trailing period to the domain. Some people add a period after the domain (e.g.,
example.com.). That is a valid DNS root reference but can cause confusion; stick to the plain domain. - Using “https://� or “http://� in the hosts file. The hosts file only takes IP and hostname, not URLs. Use
example.com, nothttps://example.com. - Editing while the file is open in another program. Close any other editor that has the hosts file open; otherwise, changes may be overwritten.
- Not flushing DNS. If you don’t flush, old DNS entries may still be used, and your change may not appear immediately.
Tips
- Back up the original hosts file. Before editing, copy the hosts file to your desktop or rename it to
hosts.bak. If something goes wrong, you can restore it. - Use 0.0.0.0 instead of 127.0.0.1 for blocking. Using
0.0.0.0often causes a faster failure because it’s a non-routable address, whereas 127.0.0.1 may trigger a connection attempt to your own PC. - Comment out with #. If you want to temporarily disable a mapping, add
#at the start of the line. This is safer than deleting. - Keep the file clean. Use comments to label your entries, e.g.,
# Block social mediabefore the lines. This helps you remember why you added them. - Use a text editor that supports Unix line endings. Windows Notepad works fine, but some editors may add extra characters. Stick with Notepad or Notepad++ to avoid issues.
Troubleshooting
- Changes don’t take effect. First, flush DNS (see step 7). If that doesn’t work, restart your browser or clear your browser’s DNS cache. Some browsers (like Chrome) have their own DNS cache; restarting the browser usually clears it.
- “Access denied� when saving. You didn’t open Notepad as administrator. Close Notepad, right-click Notepad, choose “Run as administrator�, then reopen the hosts file and save.
- The file is read-only. In the file’s properties, uncheck “Read-only� if it’s checked. But usually, if you have admin rights, you can save anyway.
- Site still loads after blocking. Check that you used the correct domain (e.g.,
www.facebook.comvsfacebook.com). Some sites use multiple subdomains; you may need to add each one. Also, if the site uses HTTPS, the browser may still resolve via DNS if the hosts file entry is malformed. - Hosts file is missing. If there’s no hosts file in the etc folder, create one by right-clicking, New > Text Document, name it
hosts(without .txt), and open it as admin. It’s rare, but possible. - I can’t find the etc folder. It’s hidden by default. In File Explorer, go to
C:\Windows\System32\driversand you should see theetcfolder. If not, enable “Hidden items� in the View tab of File Explorer.
Frequently Asked Questions
Do I need to restart my computer after editing the hosts file?
No, a restart is not required. Flushing the DNS cache with ipconfig /flushdns is enough. Some applications may need to be restarted, but Windows itself picks up the change immediately.
Can I use wildcards like *.example.com in the hosts file?
No, the hosts file does not support wildcards. You must list each subdomain separately. For example, example.com and www.example.com are two different entries.
What is the difference between 127.0.0.1 and 0.0.0.0?
127.0.0.1 is the loopback address, pointing to your own computer. 0.0.0.0 is a non-routable address that is often used to block sites because it fails faster. Both can be used for blocking, but 0.0.0.0 is generally recommended.
Will editing the hosts file affect other users on my PC?
Yes, the hosts file is system-wide, so it affects all users on the computer. If you only want to affect one user, consider using browser-based blocking extensions instead.
Is it safe to delete the hosts file?
No, the hosts file is a system file. If you delete it, Windows may recreate it, but it’s better to keep it intact. If you mess it up, you can restore it from a backup or copy the default content from the internet.
Can I use the hosts file to redirect a domain to another IP?
Yes, that’s a common use. For example, you can map example.com to 192.168.1.10 to test a local server. Just ensure the IP is reachable from your PC.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.