The hosts file is a list of names and IP addresses that Windows consults before asking any DNS server. With it you can block a website across the whole machine or see your new server before the domain points to it. Here you will see where it is, how to save it without the permission error and what to check when Windows seems to ignore it.
In short
- The file is at
C:\Windows\System32\drivers\etc\hosts, has no extension and can only be saved with the editor opened as administrator.- Each line is
IP name: use it to block domains (0.0.0.0) or redirect them to another IP.- If it has no effect, it is almost always cache:
ipconfig /flushdns, the browser’s own cache or its secure DNS.
What the hosts file is and when it is used
When you type an address in the browser, Windows has to translate the name into an IP. Simplified, the order is:
- It looks in its DNS cache (recent answers kept in memory).
- It reads the hosts file. Its entries are loaded into that same cache.
- If it cannot find the name, it asks the configured DNS server (router’s, provider’s or a public one).
Since the hosts file comes before the DNS server, what you put there wins for that machine. It does not affect other devices on the network, only the PC where you edit it. For the rest of the process, see the article on how DNS works.
Format of an entry
Each line has an IP address, at least one space or tab, and one or more names. Anything after # is a comment Windows ignores.

# Block a domain
0.0.0.0 anuncios.ejemplo.com
# Redirect a domain to another server
203.0.113.25 midominio.es www.midominio.es
Rules worth knowing:
- No wildcards.
*.ejemplo.comdoes not work: each subdomain needs its own line. ejemplo.comandwww.ejemplo.comare different names. If you only add one, the other still resolves through DNS.- No ports or paths. No
http://,/folderor:8080. - Case does not matter.
| Use | IP to use | What happens |
|---|---|---|
| Block a website | 0.0.0.0 | The connection fails immediately |
| Block (alternative) | 127.0.0.1 | Tries to connect to your own PC; a local server may answer |
| Test a new server | The server’s public IP | You see the site on that server even if DNS points to the old one |
| Local development | 127.0.0.1 | A name like proyecto.test opens your local server |
0.0.0.0 is better for blocking: nothing listens there, so the browser gives up quickly instead of waiting.
How to edit the hosts file with admin rights
The file is protected. If you open it with a double click and change it, on saving Windows says you lack permission or offers to save it elsewhere. The fix is to open the editor already elevated.
With Notepad
- Press Start and type
Notepad. - Right-click it and choose Run as administrator. Accept the User Account Control prompt.
- In Notepad, go to File > Open.
- Paste
C:\Windows\System32\drivers\etcinto the address bar and press Enter. - At the bottom right, switch the filter from Text Documents to All files. Otherwise the file does not appear because it has no extension.
- Open
hosts, add your lines at the end and save with Ctrl + S.
From PowerShell, in one line
Open Terminal (Admin) and run:
notepad C:\Windows\System32\drivers\etc\hosts
Launched from an elevated console, Notepad also inherits administrator rights.
You can also add a line without opening an editor:
Add-Content -Path "$env:windir\System32\drivers\etc\hosts" -Value "0.0.0.0 distracciones.ejemplo.com"
Make a copy before touching anything if the file already has entries that are not yours, for example from a development program or VPN:
Copy-Item "$env:windir\System32\drivers\etc\hosts" "$env:USERPROFILE\Desktop\hosts.bak"
Practical uses
Block websites on a computer
It is the simplest way to cut access to a domain on the whole PC, in any browser and program. Good for removing distractions or stopping an app from contacting a specific server.
It has clear limits: it is not serious parental control. Anyone with admin rights can delete the line, and a VPN or proxy bypasses it. To block ads across the house, a filtering DNS like Pi-hole is more convenient than maintaining lists on each computer.
Test a server before changing DNS
This is the most valuable use for anyone managing websites. Say you migrate midominio.es to new hosting with IP 203.0.113.25. Before changing DNS records, you want to check everything works there.
Add to hosts:
203.0.113.25 midominio.es www.midominio.esFlush the cache (next section) and close the browser completely.
Open the site. You now see it from the new server; the rest of the world still sees the old one.
Check pages, forms and panel login.
When you change real DNS, remove those lines. If forgotten, months later you will still see a server that may no longer exist and will not understand why.
Watch HTTPS: if the new server has no certificate for the domain yet, the browser shows a security warning. That is normal at this stage.
Check that the entry applies
Use ping, which goes through the Windows resolver and so reads hosts:
ping midominio.es

The first line shows the IP it resolves to. If it is yours, Windows is obeying.
Do not use nslookup for this. It queries the DNS server directly and skips the hosts file, so it always shows the “real” IP. Many people think their hosts file does not work because of this confusion.
In PowerShell, Resolve-DnsName does honor hosts by default:
Resolve-DnsName midominio.es
Why it sometimes does not work
The Windows DNS cache
Windows keeps answers for a while. If you recently visited the site, it may still use the old IP. Flush it from a console:
ipconfig /flushdns
To see what it holds right now, use ipconfig /displaydns.
The browser cache
Chromium-based browsers (Chrome, Edge, Brave) have their own name cache and reuse open connections. The most reliable is to close the browser entirely, not just the tab. In Chrome you can also open chrome://net-internals/#dns and click Clear host cache.
The browser uses secure DNS or a proxy
With secure DNS (DNS over HTTPS) on, the browser may resolve names itself instead of using the system. If you suspect this, turn it off briefly to test: in Chrome under Settings > Privacy and security > Security > Use secure DNS, and in Firefox in the DNS over HTTPS settings. The same happens with a VPN or proxy: they resolve outside your machine and your hosts file does not count.
The antivirus reverts or blocks the change
Much malware edits hosts to hijack bank sites or block updates. That is why Microsoft Defender and other antivirus watch this file. If you add certain domains, especially Microsoft’s, Defender may flag it as a threat and restore the file. Check Protection history in Windows Security: a detection about the hosts file tells you who undid it.
The file was saved under another name
If you did not switch the filter to All files in Notepad, it is easy to end up with hosts.txt. Windows only reads the file named exactly hosts. Enable View > Show > File name extensions in File Explorer to spot it.
Common mistakes
- Putting
http://or a path in the entry. Only the domain name. - Forgetting the
wwwvariant and thinking blocking fails. - Checking with
nslookup, which ignores hosts by design. - Leaving test entries after a migration and still seeing the old server.
- Editing without rights and saving a copy in Documents that Windows never reads.
- Using
*.domain.comexpecting it to block all subdomains.



