Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Windows Hosts File: What It Is & How to Use It

· 5 min read · Home Tech

In this article
  1. What Exactly Is the Hosts File?
  2. Where Is the Hosts File Located?
  3. How to Edit the Hosts File Safely
  4. Common Uses and Real Examples
  5. Common Problems and Mistakes
  6. Security and Safety Considerations
  7. Alternatives to the Hosts File
  8. Frequently Asked Questions

The Windows hosts file is a plain text file that maps hostnames to IP addresses, acting as a local override before your PC queries a DNS server. It has been part of Windows since the early days of networking, and while its role has shrunk, it remains useful for blocking sites, speeding up development, and testing networks. This article explains what it is, where it lives, how to edit it safely, and the common problems you might run into.

What Exactly Is the Hosts File?

The hosts file is a local list of domain names and their corresponding IP addresses. When you type a web address like example.com, Windows checks this file first. If it finds an entry, it connects to that IP directly and never asks a DNS server. If there’s no entry, normal DNS resolution takes over.

This file predates DNS by decades. It started on ARPANET in the 1970s as a single HOSTS.TXT file maintained by the Stanford Research Institute. As the network grew, DNS replaced it as the global system, but every operating system kept a local hosts file for emergencies and local control. In Windows, the file is simply named hosts (no extension) and is stored in the system folder.

Where Is the Hosts File Located?

On modern Windows (10, 11, and Server editions), the file is at:

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

There is no file extension. To open it, you need administrator privileges. A quick way to navigate is to paste that path into File Explorer’s address bar, but you still need an elevated editor to save changes.

How to Edit the Hosts File Safely

Editing the hosts file is straightforward, but a mistake can block internet access or redirect a site you rely on. Follow these steps:

  1. Open Notepad as administrator: right-click Notepad and choose “Run as administrator”.
  2. In Notepad, go to File > Open, and navigate to C:\Windows\System32\drivers\etc.
  3. Change the file type filter from “Text Documents (*.txt)” to “All Files (*.*)” so the hosts file appears.
  4. Select the file named hosts and open it.
  5. Make your edits, then save (Ctrl+S).

Each line follows a simple format: an IP address, a space or tab, then the hostname. For example:

127.0.0.1   localhost
127.0.0.1   blockingads.example.com

Lines starting with # are comments and are ignored. The default file contains several comment lines explaining usage.

Common Uses and Real Examples

People use the hosts file for three main purposes:

  • Blocking websites: Point a domain to 127.0.0.1 (your own PC) to prevent it from loading. For example, 127.0.0.1 www.facebook.com would block Facebook on that machine.
  • Development and testing: Redirect a domain to a local server IP like 192.168.1.10 to test a website before DNS is updated.
  • Network troubleshooting: Bypass DNS issues by forcing a known-good IP for a service.

One common misconception is that the hosts file can block all ads or trackers. While it can block specific domains, modern websites use dozens of subdomains and dynamic IPs, so a hosts file is rarely a complete ad-block solution. Specialized software and browser extensions are more effective.

Common Problems and Mistakes

Several issues can arise when working with the hosts file:

  • File not saving: If you get “Access denied”, you didn’t run the editor as administrator. Close and reopen Notepad with elevation.
  • Wrong IP format: Always use the correct IP format (IPv4 like 192.168.1.1 or IPv6 like ::1). A typo will cause connection failures.
  • Forgotten mapping: If you block a site and later forget, the entry remains until you remove it. Keep comments to remind yourself.
  • DNS cache interference: Windows caches DNS lookups. After editing the hosts file, you may need to flush the cache with ipconfig /flushdns in Command Prompt.

Another frequent mistake is adding entries with extra spaces or tabs that break the format. Stick to a single space or tab between the IP and hostname.

Security and Safety Considerations

The hosts file is a prime target for malware. Malicious programs can add entries that redirect banking sites to phishing pages or block security updates. To protect yourself:

  • Keep the file read-only after making changes (right-click > Properties > Read-only).
  • Periodically review the file for unknown entries.
  • Use antivirus software that monitors hosts file modifications.

If you suspect your hosts file has been tampered with, you can reset it to the default. Microsoft provides a KB article with the default content, but a safe approach is to delete all lines except the localhost entry and then flush DNS.

Alternatives to the Hosts File

For many tasks, there are better tools than manually editing the hosts file:

  • DNS-based ad blockers: Services like AdGuard DNS or NextDNS filter at the DNS level, covering all devices on your network.
  • Browser extensions: uBlock Origin and similar extensions block ads and trackers without system-wide changes.
  • Local DNS servers: Pi-hole runs on a Raspberry Pi and acts as a network-wide ad blocker using a hosts-like approach but with more control.
  • Virtual hosts in development: Tools like XAMPP or Docker can map domains to local servers without touching the hosts file.

Each alternative has trade-offs. The hosts file is simple, free, and works offline, but it’s static and requires manual updates. DNS services are easier to manage but may have privacy implications.

Frequently Asked Questions

Why can’t I save changes to the hosts file?

You need administrator rights. Right-click your text editor and choose “Run as administrator”, then open the file from within that elevated session. If that fails, check if the file is marked read-only in its properties.

How do I flush the DNS cache after editing?

Open Command Prompt as administrator and type ipconfig /flushdns. This clears the DNS resolver cache, so your new hosts entries take effect immediately.

Can the hosts file block all ads?

No. It can block specific domains, but modern ads come from many dynamic domains. A hosts file is a blunt tool; dedicated ad-blockers are more effective and easier to maintain.

Is editing the hosts file dangerous?

Not if you’re careful. The main risks are typos that break connectivity and malware that modifies the file. Always back up the original and review changes regularly.

Does the hosts file work on Windows 11?

Yes, the location and behavior are the same as Windows 10. The file is at C:\Windows\System32\drivers\etc\hosts and requires administrator privileges to edit.