The Windows hosts file is a small text file that tells your own computer which IP addressIP address The number address of a device on the internet, like 203.0.113.10. Computers use it to find each other, the way post uses street addresses. More about IP address → to use for a domain name, before it asks DNS. This guide is for site owners and developers on Windows 10 or 11 who want to find it, edit it without breaking anything, and use it to test a website on a new server.
The hosts file lives at **C:\Windows\System32\drivers\etc\hosts and has no file extension. To edit it, open Notepad as administrator**, open that file, add a line such as 203.0.113.10 example.com www.example.com, and save. The change affects only your computer and works straight away in most programs, although a browser may need restarting.
What the hosts file does
When you type a web address, Windows has to turn the name (example.com) into an IP address (203.0.113.10) before it can connect. Normally it asks a DNSDNS The internet’s phone book. It turns a name people can remember, like example.com, into the number address computers use to find the server. More about DNS → resolver, the service your internet provider or router points you at. The hosts file is checked first. If the name is listed there, Windows uses that address and does not need to look the name up in DNS.
That makes it useful for a few everyday jobs:
- Previewing a site on a new server before you switch the real DNS records, which is the safest way to test a migration.
- Pointing a name at a development machine, such as a test copy of a site on your local network.
- Blocking a site on one computer by pointing its name at an address that goes nowhere.
The key limit is that the file only changes what your computer sees. Nobody else on the internet is affected, and nothing on the server changes. To change where a domain points for everyone, you edit its DNS records instead, and the change then spreads as described in DNS propagation.
Where the hosts file is on Windows
On Windows 10 and 11 the path is:
C:\Windows\System32\drivers\etc\hosts
A few details trip people up:
- The file is called
hosts, with no extension. It is nothosts.txt. If you see ahosts.txtin the folder, Windows is ignoring it. - The folder may look empty when Notepad’s file picker is set to show only text files. Change the picker to All files to see it.
- If Windows is installed on a drive other than C:, the path starts with that drive letter instead. In PowerShell,
$env:windir\System32\drivers\etc\hostsalways gives the right location.
A fresh hosts file contains only comment lines, which start with a hash sign. Even the localhost lines are commented out, because Windows handles localhost itself. If your file contains active lines you did not add, see the section on problems below.
How to edit the hosts file
The file is protected, so an ordinary Notepad window can open it but not save it. You need to run your editor with administrator rights.
Open Notepad as administrator
- Click Start and type
Notepad. - Right-click Notepad in the results and choose Run as administrator. Accept the User Account Control prompt.
- In Notepad, choose File, then Open.
- Paste
C:\Windows\System32\drivers\etc\hostsinto the File name box and press Enter.
You can do the same in one step from an administrator PowerShell window:
notepad $env:windir\System32\drivers\etc\hosts
To open an administrator PowerShell window, right-click the Start button and choose Terminal (Admin) on Windows 11, or Windows PowerShell (Admin) on Windows 10.
Back up the file first
Before changing anything, keep a copy so you can undo a mistake. In an administrator PowerShell window:
Copy-Item $env:windir\System32\drivers\etc\hosts $env:windir\System32\drivers\etc\hosts.backup
Windows does not read hosts.backup, so the copy does no harm sitting in the same folder.
Add an entry
Each entry is one line: the IP address, at least one space or tab, then one or more host names. Everything after a # on a line is a comment and is ignored, so you can leave yourself a note at the end of the line.
203.0.113.10 example.com # new server, remove after the move
203.0.113.10 www.example.com # new server, remove after the move
Keep these rules in mind:
- List every name you will visit.
example.comandwww.example.comare different names, and so is any subdomainSubdomain A name in front of your domain, like shop.example.com or blog.example.com. It can show a different site from the main one. More about Subdomain → such asshop.example.com. Each needs its own entry. - Wildcards do not work. A line such as
203.0.113.10 *.example.comis not understood. - Put the IP address first. Reversing the order is a common slip and the line is then ignored.
- One name per line is easiest to read and avoids problems with very long lines.
- Add a dated comment so you remember to remove a temporary entry later.
Save and test
Press Ctrl+S to save. If Notepad offers a Save as dialog instead, you did not open it as administrator, or Notepad is about to add .txt. Close it, reopen Notepad as administrator and try again.
To check that Windows now uses your entry, run this in PowerShell or Command Prompt:
ping -n 1 example.com
The first line should show the address you added, such as Pinging example.com [203.0.113.10]. It does not matter whether the ping gets a reply, because many servers block ping. What matters is the address in brackets.
You can also ask PowerShell to resolve the name the same way Windows programs do:
Resolve-DnsName example.com
nslookup ignores the hosts file. It always asks a DNS server directly, so it will keep showing the public address even when your entry is working. Use ping or Resolve-DnsName to test the hosts file, and nslookup to see what everyone else sees. Running Resolve-DnsName example.com -NoHostsFile shows the public answer from PowerShell too.
Using the hosts file to test a site move
This is the job most site owners reach for the hosts file for. You have copied a site to a new server but the domain still points at the old one. By adding the new server’s address to your hosts file, you see the new copy while every visitor keeps using the old one.
- Find the new server’s IP address in your new host’s control panelControl panel A website you log into to manage your hosting with buttons and forms, instead of typing commands. cPanel and Plesk are the common ones. More about Control panel → or welcome email.
- Add entries for the bare domain and the
wwwversion, as shown above. - Close every browser window and open the browser again, so it forgets addresses it already looked up.
- Visit the site and work through it: pages, images, forms, logins and checkout if you have one.
- To be sure which server you are on, add a temporary file such as
servertest.txtto the new server only and visithttps://example.com/servertest.txt. If it loads, you are on the new server. - When testing is finished, remove or comment out the lines before you switch DNS, so you see the same thing as everyone else.
Expect a certificate warning if the new server does not yet have an SSL certificateSSL/TLS certificate The small file that proves a website is really who it says it is and turns on the padlock (HTTPS). More about SSL/TLS certificate → for the domain. Free certificates from Let’s Encrypt usually cannot be issued until the real DNS points at the new server, so a warning during testing is normal. You can click through it for a quick look, but do not enter real passwords on a page that shows one.
For the full order of a move, see how to change hosts without downtime and the website migration checklist.
How to undo or reset the hosts file
To undo a single change, open the file as administrator again and either delete the line or put a hash sign at the start of it. Commenting out is handy when you will need the entry again.
To return to the backup you made:
Copy-Item $env:windir\System32\drivers\etc\hosts.backup $env:windir\System32\drivers\etc\hosts -Force
If you have no backup and the file is a mess, you can replace its contents with nothing but comment lines. Windows works perfectly well with a hosts file that contains no active entries. Microsoft also publishes a support article with the default text, linked below.
After any change, clear the cachedCache A saved copy of something, kept so it does not have to be made or fetched again. Like keeping a printed copy instead of reprinting it for every person who asks. More about Cache → answers so they do not linger:
ipconfig /flushdns
See how to flush your DNS cache for clearing browser caches as well.
Common problems
| Symptom | Cause | Fix |
|---|---|---|
| Access denied, or Notepad asks where to save | Notepad is not running as administrator | Close it and reopen with Run as administrator |
| Entry is ignored completely | File was saved as hosts.txt | Rename it to hosts with no extension, using All files in the save dialog |
ping shows the new address but the browser shows the old site | The browser cached the old address or uses its own secure DNS | Restart the browser, or turn off secure DNS in its settings while testing |
nslookup still shows the old address | nslookup never reads the hosts file | Test with ping or Resolve-DnsName instead |
| Works for example.com but not www.example.com | Only one name was added | Add a line for every name you visit |
| Entry works on one network but not over a VPN or proxy | The VPN or proxy looks the name up on its own side | Disconnect the VPN or proxy while testing |
| Entry disappears after a while | Security software restored the file | Check your antivirus history; Microsoft Defender can flag entries that redirect well-known domains |
| Lines you never added, pointing popular sites elsewhere | Possible malware or an old tool | Run a full security scan, then reset the file |
The hosts file on macOS and Linux
The same idea exists on every common operating system. On macOS and Linux the file is /etc/hosts, with the same format, and you edit it with administrator rights:
sudo nano /etc/hosts
That makes the hosts file a handy trick to know whichever computer you are testing from.
Official documentation
Related
- DNS lookup: how to check any domain’s DNS records shows what the rest of the world sees for your domain, not just your computer.
- How to transfer a WordPress site to a new host walks through a move where hosts file testing is a key step.
- Google DNS: what 8.8.8.8 is and how to use it covers changing the DNS resolver itself, which affects every name rather than a few.
Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.


