Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
29.10.2024

The Hosts File in Windows 10: What It Is and How to Edit It

The hosts file is a plain text file used by the operating system to map domain names to IP addresses before the system checks external DNS servers. By editing the hosts file, you can manually define which IP addresses are associated with specific domain names. This is useful for testing websites, blocking certain domains, or overriding DNS settings.

In this article, we’ll explain what the hosts file is, how it works in Windows 10, and provide step-by-step instructions on how to find, edit, and use the hosts file effectively.


What is the Hosts File?

The hosts file is a system file that contains static mappings of domain names to IP addresses. When you type a domain name into your browser (e.g., www.example.com), your computer checks the hosts file before reaching out to a DNS server to resolve the domain to an IP address. If there’s an entry for the domain in the hosts file, the system uses the IP address specified there.

Common Uses of the Hosts File:

  • Blocking websites: Redirect a domain (e.g., www.ads.com) to 127.0.0.1, effectively blocking access.
  • Testing websites: Point a domain to a local development server.
  • Bypassing DNS issues: Manually assign an IP address to a domain if the DNS is not resolving correctly.

Location of the Hosts File in Windows 10

In Windows 10, the hosts file is located in the following directory:

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

You will need administrative privileges to edit this file because it’s a protected system file.


How to Edit the Hosts File in Windows 10

To edit the hosts file, follow these steps:

Step 1: Open Notepad as Administrator

  • Click the Start button, type Notepad in the search bar.
  • Right-click on Notepad and select Run as administrator.

Step 2: Open the Hosts File

  • In Notepad, click on File > Open.
  • Navigate to the hosts file location:
    C:\Windows\System32\drivers\etc\
  • In the bottom-right corner of the open dialog, change the file type filter from Text Documents (*.txt) to All Files.
  • Select the hosts file and click Open.

Step 3: Add Entries to the Hosts File

Once the hosts file is open, you can add, modify, or remove domain-to-IP mappings. Each entry should be on a new line with the following format:

IP_address domain_name

For example, to block www.ads.com, you can add this line:

127.0.0.1 www.ads.com

This will redirect any request for www.ads.com to your local machine (127.0.0.1), effectively blocking access to that website.

If you want to point a domain to a specific IP address (such as for testing purposes), you can use the following format:

192.168.1.100 www.example.com

This will force the system to resolve www.example.com to the local IP address 192.168.1.100.

Step 4: Save the Hosts File

After making the changes, click File > Save to save the hosts file. You may be asked to confirm the administrative permissions to make changes.


Practical Examples of Using the Hosts File

1. Blocking Unwanted Websites

You can block access to specific websites by mapping them to a non-existent IP address or to 127.0.0.1, which is the localhost address. This effectively prevents the browser from loading the website.

For example, to block Facebook, add the following entry to the hosts file:

127.0.0.1 www.facebook.com 127.0.0.1 facebook.com

When you try to visit www.facebook.com, it will fail to load because the system is trying to resolve it to 127.0.0.1, which doesn’t host the website.

2. Redirecting Domains for Testing

If you are developing a website and want to test it on your local machine without affecting the live site, you can edit the hosts file to redirect the domain to your local development server.

For example, if your local server is running at 192.168.1.50 and you want to test www.mytestsite.com locally, you can add:

192.168.1.50 www.mytestsite.com

Now, when you visit www.mytestsite.com, it will resolve to your local server for testing.

3. Bypassing DNS Issues

If a DNS server is failing to resolve a domain correctly, or if you need a temporary fix while DNS changes propagate, you can manually set the IP address in the hosts file.

For example, if www.example.com should resolve to 93.184.216.34, you can add:

93.184.216.34 www.example.com

This forces the domain to resolve to the specified IP address without relying on external DNS servers.


Restoring the Default Hosts File

If you accidentally modify the hosts file incorrectly or need to restore it to its default state, you can simply remove all custom entries and leave the default structure intact. The default hosts file looks like this:

# Copyright (c) 1993-2009 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 handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost

Troubleshooting Hosts File Issues

    • Ensure that you are editing the hosts file with administrative privileges.
    • Check that there are no typos in the entries.
    • Clear your DNS cache to ensure the changes take effect. You can do this by running the following command in the Command Prompt:Changes Not Taking Effect:
    ipconfig /flushdns
  1. Hosts File Permissions: If you’re unable to save changes, it may be due to incorrect file permissions. Right-click on the hosts file, go to Properties, and ensure the file is not set to Read-only.
  2. Antivirus Interference: Some antivirus programs monitor the hosts file to prevent malware from modifying it. If you cannot edit the hosts file, temporarily disable your antivirus program or check its settings.

Conclusion

The hosts file in Windows 10 is a simple but powerful tool that allows you to override DNS settings, block websites, and redirect domains for testing or troubleshooting. By understanding how to edit and manage the hosts file, you can take control of how your system resolves domain names, making it easier to perform tasks like blocking unwanted content, testing local sites, or managing network configurations.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills