What Is the Windows Hosts File and How to Edit It

Featured Image Windows Hosts File Editing It Windows11 10

The Windows hosts file is a plain text file on your Windows PC that can be manually maintained by you as an administrator. Although optional, the hosts file has been retained in Windows 11 and 10 to help manage any website, server network address, ad trackers, spyware, and Trojans. This easy-to-understand guide details what the Windows hosts file is and how to open and edit it.

Also read: How to Be More Productive with Hot Corners in Windows

What Is the Windows Hosts File?

If you’ve ever used a phone company’s directory, you’d know how it maps any phone record to a unique person, business or residence. In the same manner, the hosts file in any operating system matches IP addresses with unique domain-based hostnames (or websites), server names, proxies, and more.

In Windows systems, as with Linux or Mac, the hosts file’s main purpose is to enable simple IP address mapping of hostnames and servers. Therefore, it is similar to the Domain Name System (DNS).

Windows Hosts File Windows11 What Is Hosts File

Each hosts file entry follows the default localhost entry in a separate line. Each line is preceded by a hash (#) symbol that allows you to insert comments. The real applications of the Windows hosts file requires you to omit the # sign.

Windows Hosts File Windows11 What Is Hosts Contd

Also read: How to Capture a Scrolling Screenshot in Windows

Where Is the Hosts File on Windows?

The Windows hosts file is always found at “C:\Windows\System32\Drivers\etc.” It’s a text file that is not associated with any program. You can easily open it with Notepad though.

Windows Hosts File Windows11 Etc Hosts File Location

Note: If you try adding another file with the name “hosts” to the “etc” folder, it will have negligible impact on the original hosts file process, as you’re simply renaming it as another duplicate file.

How to Open the Hosts File in Windows as Administrator

The only way to open the hosts file in Windows as the administrator is to use Notepad. The reason you need Admin access is that the hosts file’s internal entries can only be managed by users with the highest admin privileges.

  1. Go to the Search menu in Windows and open Notepad as administrator. If the admin option is not visible to you, click the down arrow symbol. Also, If you’ve saved the Notepad shortcut in another PC location, you can open it by right-clicking on the app symbol and selecting “Run as administrator.”
Windows Hosts File Notepad Admin
  1. Once the blank Notepad file is open, go to “File -> Open” to search for files on your PC that work with Notepad in Admin mode.
Windows Hosts File Notepad Admin Open
  1. Navigate to the folder “C:\Windows\System32\Drivers\etc.” You may not see anything at first. Click on a tiny text document’s menu item to reveal more options.
Windows Hosts File Notepad Admin Filetye Textdocument
  1. Select “All files” in the menu option chosen above. Now the hosts file is clearly visible along with any other files. Choose the correct hosts file in Notepad Admin mode while avoiding any duplicates.
Windows Hosts File Notepad Admin All Files Open
  1. The hosts file will populate Notepad in Admin mode. You can freely edit this document and save it without encountering errors.
Windows Hosts File Notepad Admin Mode
  1. If you only wish to check the hosts file and not edit it, you can directly open it with Notepad from a simple right-click.
Windows Hosts File Windows11 Open Right Click

Also read: How to Boot to Safe Mode in Windows

How to Edit My Hosts File in Windows

Here we will learn how to modify the hosts file in Windows, which is how the end use applications are served.

  1. Open the hosts file in Notepad in Admin mode.
  2. The hosts file’s horizontal entries are separated by a space or tab. To ensure there are no errors due to syntax issues, select the entire block of text including 127.0.0.1 and localhost but not the hash (#) symbol.
Windows Hosts File Copy 1st Line
  1. Copy-paste the IP address and localhost in a new line. You can add it next to the default previous entry. To keep the hosts file clean, it’s best to add some paragraph spacing.
Windows Hosts File Copy Pasted 1st Line
  1. To start adding entries in the hosts file, simply paste a new domain name instead of localhost. You can also modify the IP address.
  2. Press Enter to add a new line.
Windows Hosts File Admin Mode Entries Added
  1. Once you’re done editing the hosts file in Notepad admin mode, be sure to save the entries.
Windows Hosts File Windows11 Save Edits

Note: if you forget to edit the hosts file as an admin user in Windows, you will encounter a “You don’t have permission to save in this location. Contact the administrator to obtain permission” message.

Windows Hosts File Saving Error Non Admin Mode

Also read: Can (and Should) You Continue to Use Windows 8 or 8.1?

Applications of the Windows Hosts File

As the hosts file can override the DNS server, it can achieve advanced objectives, such as blocking or redirecting any website or URL of your choice.

1. Edit Windows Hosts File to Block Websites

You can use the Windows hosts file to remove time-wasting URLs, entertainment sites, and other websites you don’t want to visit. Don’t want to spend time on Netflix? Simply block it entirely at a system level.

  1. Open the hosts file in Notepad as an admin user.
  2. Populate the list of websites you want to block in the format 127.0.0.1 domain. Follow it by saving and exiting the Notepad file.
Windows Hosts File Browser Sites Being Blocked

There may be a time you need to block multiple domains of the same service as individual entries in the hosts file. For example, it treats “twitter.com” and “m.twitter.com” as two separate entries.

After you save the list of blocked websites in Notepad, you won’t be able to access it on a browser again unless you edit the hosts file to regain access. This blocking method is supported on Google Chrome, Microsoft Edge, Firefox, Opera and many other browsers. It is very important to delete the browser cache after blocking the website in the hosts file, otherwise, it will keep loading again.

Windows Hosts File Website Blocked Thru Hosts File

Apart from the major browsers, you can also block websites on less-used alternative browsers such as SlimBrowser.

Also read: How to Block Websites on Chrome, Firefox, Edge, Safari, Android, and iOS

2. Edit Windows Hosts File to Redirect Websites

You can use the Windows hosts file to redirect any website to another URL. This may not always be supported due to security certificate conflicts.

  1. Open the Windows hosts file in Notepad in Admin mode.
  2. Insert the name of the website you want to redirect elsewhere. Our example will replace “nytimes.com” with “bbc.com.”
Windows Hosts File Browser Redirection Redirect Browser Name
  1. Open Windows Command Prompt and enter the following text in the window.
ping website name
  1. The above command will generate a series of replies from the end website while revealing its IP address. Sometimes you may see an IPv6 address instead of IPv4 as shown in the below example.
Windows Hosts File Browser Redirected Website Ip Address
  1. Once the IP address is found, go back to the hosts file and change the 127.0.0.1 entry for the website you want redirected, replacing it with the IP address generated in the Command Prompt.
  2. Save the hosts file and go back to a browser for testing.
Windows Hosts File Browser Redirected Ip In Notepad.pg
  1. After setting the redirection property for any website, you will see a redirection message. Sometimes it’s an error, as the security certificates of the two sites do not match. This is why its main purpose is to redirect an entertainment or non-productive site to a company homepage or your own server address.
Windows Hosts File Redirected Website Using Hosts

Also read: 15 Safe Websites for Downloading Windows Software

3. Spam and Malware Protection with Hosts File

The Windows hosts file can also be used to manage multiple spam user links, third party sites, cookies, ad trackers, and surveillance bots. It can be used to keep malware sites at bay. Browsers such as Firefox give you a complete list of trackers and bots on any webpage. If you want to frequent a website while avoiding its spammy links, simply add them as blocked entries in a host file.

Windows Hosts File Malware Spam Links

Also read: What is Antimalware Service Executable? Why is CPU and Memory Usage High?

Frequently Asked Questions

1. Why is my hosts file not working?

Is Windows not able to read the hosts file on your computer? You may encounter this error when you don’t set the line endings properly in Notepad as an admin user. It really boils down to syntax errors in saving the entries. The good thing is these problems are very easy to fix.

To avoid possible errors in entries saved using the Windows hosts file, make sure you copy-paste the entire first sentence containing “127.0.0.1” and “localhost” in the default entry. Use it to modify each and every line below.

2. How can I clear my hosts file in Windows?

Clearing the hosts file is very easy. This allows you to reuse the hosts file document just as it was in a previous state.

  1. Open the hosts file in Notepad in Admin mode.
  2. Select all the non-default entries, which are the remainder text below the localhost lines.
  3. Delete the entries and save the hosts file. It is now cleared for reuse.
Windows Hosts File Clear Hosts File

3. How can I reset the Windows hosts file back to the default?

Sometimes you may face errors in the Windows hosts file configuration, especially after it was changed several times. To undo these errors, it’s a good idea to reset the Windows hosts file back to its default state.

  1. Open any Notepad document in Admin mode and copy-paste the following text below.
# Copyright (c) 1993-2006 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one # space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol. #
# For example:
#
# 102.54.94.97 rhino.acme.com
# source server
# 38.25.63.10 x.acme.com
# x client host
# localhost name resolution is handle within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
  1. Save the above entry on the Desktop or any other PC location apart from the “etc” folder in System32. Name the document as “hosts.”
Windows Hosts File Save Desktop
  1. Go back to the hosts file in the “etc” folder and rename it as “hosts.old.”
  2. Transfer the newly created hosts file into the “etc” folder to make it the default hosts file again.
Windows Hosts File Rename

4. Can I delete the Windows hosts file?

While the hosts file has a very powerful function in blocking websites, interestingly, it’s just an optional system resource in Windows 11/10. You can easily delete the hosts file from your PC without it affecting the PC operations in any way. You can always recreate a new hosts file by resetting it as a default in Notepad using the method shown above.

5. Can malware affect the hosts file?

As the hosts file has a powerful role to play in blocking and redirecting websites in Windows, it is a favorite attacking point for malware authors. Sometimes even Windows Defender may incorrectly determine that the hosts file was changed by malware or spyware. In this case, you can exclude the hosts file from being scanned by Windows Defender while you correct the malware links inserted inside the file. Yet, in other cases, malware authors may name their malware after hosts and try to place it in the etc folder.

Image credit: Pixabay

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sayak Boral
Sayak Boral - Staff Writer

Sayak Boral is a technology writer with over eleven years of experience working in different industries including semiconductors, IoT, enterprise IT, telecommunications OSS/BSS, and network security. He has been writing for MakeTechEasier on a wide range of technical topics including Windows, Android, Internet, Hardware Guides, Browsers, Software Tools, and Product Reviews.