How to Edit the Hosts File in Windows 11 Safely

The hosts file is protected, so Notepad often refuses to save changes. Open Notepad as administrator, back up the file, make your edit and flush the DNS cache.

Screenshot of a hosts file in a text editor with comment lines and a green box highlighting the added entry 192.168.0.1 wikihow.neve.com.
In this article

You can’t save changes to the hosts file from a normal Notepad window, because Windows protects it and only an app running as administrator can write to it. If you want to edit the hosts file in Windows 11, you need to open your text editor with admin rights first. [1] For more detail, see How to Launch File Explorer as Admin in Windows 11 and 10.

The hosts file is a plain-text file that maps domain names to IP addresses. Windows checks it before asking a DNS server, so an entry there overrides what DNS would return. [2] Without admin rights, you can open the file but saving fails. [3]

The plan is short: back up the file, open Notepad as administrator, make your edit, save it, and flush the DNS cache so the change takes effect.

What does the hosts file do, and where is it?

The hosts file is a plain-text file that maps hostnames to IP addresses, and on Windows 10 and 11 it lives at C:\Windows\System32\drivers\etc\hosts [2][1]. It has no file extension, so it appears in the etc folder simply as “hosts”.

What people use it for

The file lets you override what a domain name points to on your own computer. Common uses include:

  • Blocking a site: point its name at an invalid address such as 0.0.0.0 or 127.0.0.1, the loopback address [1].

  • Redirecting a domain: send a name to a different IP address, for example to test a website on a local server [1].

  • Mapping names to an IP: several domain names can point to the same IP address [2].

The change applies only to your local computer [4].

The line format

Each entry is one line with the IP address first, then the domain name [4]:

<ip address> <domain name>

For example, 127.0.0.1 example.com sends example.com to your own machine [2]. Lines that begin with a pound sign (#) are comments [2]. To remove an entry later, delete its line [2].

Administrator rights are required

You need administrative permissions to edit the file, so you can’t just open it in a normal Notepad window [1]. That is why the steps later in this article start Notepad as an administrator. If you sign in with a standard account, you will be asked for an administrator’s credentials.

How do you back up the hosts file first?

Back up the hosts file before you change it, because a wrong entry can cut off access to sites, and a saved copy lets you undo the damage in seconds. The whole backup takes one command.

  1. Open Command Prompt as administrator. Right-click the Start button and select Terminal (Admin), which gives you the privileges needed to work in the protected folder. [5]

  2. Run this command to copy the hosts file to a new file named hostsBACKUP in the same folder:

copy "C:\Windows\System32\drivers\etc\hosts" "C:\Windows\System32\drivers\etc\hostsBACKUP"

[4]

The first path is the original file and the second is the copy. Keep the quotation marks so the command works with the full paths. 3. Confirm the copy exists. The next command lists the folder, and you should see both hosts and hostsBACKUP:

dir C:\Windows\System32\drivers\etc

[5]

The hostsBACKUP file has no extension, just like the original. That is intentional, and it keeps the copy out of the way, since Windows only reads the file named hosts.

Restoring the backup

If an edit goes wrong, copy the backup back over the original. Run the same command with the two paths swapped, from the same administrator window:

copy "C:\Windows\System32\drivers\etc\hostsBACKUP" "C:\Windows\System32\drivers\etc\hosts"

Command Prompt asks whether to overwrite the existing file. Answer yes, and the hosts file returns to the state it was in when you made the backup.

A black portable external hard drive on a wooden desk, cabled to a silver laptop beside it.

How do you edit the hosts file with Notepad as administrator?

Open Notepad with administrator rights first, then open the hosts file from inside it. That is what lets Windows accept your save.

  1. Click Start (or press the Windows key) and type Notepad.

  2. Right-click Notepad in the results and choose Run as administrator. On Windows 11, you may instead need to click the small downward-facing arrow next to Notepad and then select Run as administrator. [1]

  3. Accept the User Account Control (UAC) prompt.

  4. In Notepad, choose File > Open and browse to C:\Windows\System32\drivers\etc. The hosts file is in that folder. [4]

  5. Change the file type filter from Text Documents (*.txt) to All files, otherwise the hosts file will not show in the list, because it has no extension.

  6. Select hosts and click Open.

  7. Scroll to the end and add a new line in the format <ip address> <domain name>. [4] For example, this line blocks a site by pointing it to an address that goes nowhere:

0.0.0.0 example.com
  1. Press Ctrl+S to save.

Faster option: open it from an elevated Command Prompt

If you already have an elevated Command Prompt open, you can skip the browsing. Run this command to open the file in Notepad. [4]

notepad "C:\Windows\System32\drivers\etc\hosts"

Because the terminal is elevated, Notepad also runs with administrator privileges, so you can save without permission errors. [5]

Another shortcut is the Run box. Press Windows key + R, enter Notepad.exe C:\Windows\System32\Drivers\etc\hosts, then hold Ctrl+Shift while you click OK. [3]

Windows 10 versus Windows 11

The steps are the same on Windows 10 and 11. [5] The hosts file sits in the same folder on both. [1]

How do you make the change take effect?

Saving the file is not quite the last step: Windows can keep using old name lookups until you clear its DNS cache, so flush it and then test the domain.

  1. Open Command Prompt (it can be a normal window for this step). Run this command to clear the cached lookups:

ipconfig /flushdns

ipconfig is the Windows network tool, and /flushdns tells it to empty the DNS resolver cache. [4]

  1. Close your browser completely, then reopen it. Browsers can hold on to their own connection to a site, so a fresh start makes the new entry more likely to apply.

  2. Visit the domain you edited. If you added 0.0.0.0 example.com, the page should now fail to load. If you pointed a name at a different IP address, you should land on that server instead.

  3. If you need to undo the change, reopen the hosts file in Notepad as administrator and delete the line you added, then save and repeat steps 1 and 2.

  4. If you would rather go back to the exact original, copy your backup over the edited file by running this in an elevated Command Prompt:

copy "C:\Windows\System32\drivers\etc\hostsBACKUP" "C:\Windows\System32\drivers\etc\hosts"

This is the backup command with the two paths swapped, so the saved copy replaces the current hosts file. Run ipconfig /flushdns again afterward so Windows drops the entry it cached.

Why won’t the hosts file save, and what else goes wrong?

The most common reason the hosts file won’t save is that Notepad was opened normally, without administrator rights. Any app that edits the hosts file has to run elevated, so a regular Notepad window will refuse to save changes. [3] Close it, reopen Notepad as administrator, and make your edit again. To go further, see How to Run File Explorer as a Different User in Windows.

One quick way to get an elevated Notepad is the Run box. Press Windows key + R, paste Notepad.exe C:\Windows\System32\Drivers\etc\hosts, then hold Ctrl + Shift while you click OK. [3]

Saving under the wrong name

The file must be named exactly hosts, with no extension. If you use Save As and Notepad adds .txt, you end up with hosts.txt, which Windows ignores. Open the etc folder, check the name, and rename the file if needed. If you created a new file by mistake, your original hosts file is untouched, so the changes simply never applied.

Typos and spacing

Each entry follows the format below, with the IP address and the domain name separated by a space. [4]

<ip address> <domain name>

A missing space, a misspelled domain or a typo in the IP address means the line does nothing, or points the name to the wrong place. Compare your line with the format character by character. For example, 0.0.0.0example.com will not work, while 0.0.0.0 example.com will.

Editing the backup copy by mistake

The backup is named hostsBACKUP, and it sits in the same folder as hosts. [4] The two names look alike, so it is easy to open the wrong one. Edits made to hostsBACKUP have no effect on your connections, and you also lose your clean restore point.

To avoid this, open the real file with the elevated command shown in the Notepad step, and check that the title bar reads hosts rather than hostsBACKUP.

Quick recap

The safe routine is short: back up the hosts file, open Notepad as administrator, make your edit, save it, then run ipconfig /flushdns so Windows picks up the change. [4] If something goes wrong, copying the backup over the edited file puts you back where you started.

Your next step is to try it with one harmless line and confirm the result in your browser. Once that works, you can reuse the same routine for any entry. If you want to know more ways to launch apps with elevated rights, the pillar article on how to run a file as administrator in Windows 11 covers them.

Sources

  1. The hosts file is a simple text file that maps web addresses to IP addresses. It can be used to…

  2. How to Edit Host File in Windows 11

  3. How to can edit the file hosts in Windows?

  4. How To Edit The hosts File on Windows 11

  5. How to Edit Hosts File on Windows Using Command Prompt