The Windows hosts file is a plain text file that maps domain names to IP addresses before DNS lookup happens. Editing it lets you block domains, redirect traffic locally, or create custom hostname entries for development work. Here’s how to edit it correctly.
What the hosts file does
When you type a URL in a browser, Windows first checks the hosts file before querying DNS. If the hosts file has an entry for that domain, it uses that IP address directly. This makes it useful for:
- Blocking websites by pointing them to 127.0.0.1 (localhost)
- Redirecting a domain to a local development server
- Overriding DNS for testing purposes
File location
The hosts file is at: C:\Windows\System32\drivers\etc\hosts
It has no file extension. It’s a plain text file.
How to edit it
You can’t just open and save the hosts file – it requires administrator permissions. Two methods:
Method 1: Open Notepad as administrator
- Search for Notepad in the Start menu
- Right-click Notepad > Run as administrator
- File > Open > navigate to
C:\Windows\System32\drivers\etc\ - Change the file type filter from “Text Documents” to “All Files”
- Select “hosts” and open it
- Make your edits and save
Method 2: Copy, edit, replace
- Copy the hosts file to your Desktop
- Edit it in any text editor
- Copy it back to
C:\Windows\System32\drivers\etc\and approve the administrator prompt
Hosts file format
Each line follows the format: IP address [tab or space] hostname
Examples:
127.0.0.1 example.com– blocks example.com by pointing it to localhost192.168.1.100 mydevserver.local– maps a custom hostname to a local IP- Lines starting with
#are comments and are ignored
Flushing DNS after editing
After saving changes to the hosts file, flush the DNS cache so the changes take effect immediately: open Command Prompt as administrator and run ipconfig /flushdns.
Reverting changes
To undo, open the hosts file again and remove the lines you added. The default hosts file only contains commented lines (starting with #) – deleting any uncommented lines you added restores the original behavior.