Windows Hosts File Reference
What is the Hosts File?
The hosts file provides static hostname-to-IP mapping that takes precedence over DNS. When your computer needs to resolve a hostname, it checks the hosts file first before querying DNS servers.
Use cases:
- Testing website changes before DNS propagation
- Blocking unwanted domains (point to 127.0.0.1)
- Overriding DNS for specific hosts
- Development/staging environment testing
Location
C:\Windows\System32\drivers\etc\hosts
File Format
# Comments start with #
# Format: IP_ADDRESS HOSTNAME [ALIAS]
127.0.0.1 localhost
::1 localhost
# Custom entries
192.168.1.100 myserver.local myserver
10.0.0.50 internal-app.company.com
Each entry:
- IP address (IPv4 or IPv6)
- One or more hostnames separated by spaces/tabs
- Comments with
#
Editing the Hosts File
Method 1: Notepad as Administrator
- Press
Win + S, type Notepad - Right-click → Run as administrator
- File → Open → Navigate to
C:\Windows\System32\drivers\etc\ - Change filter from “Text Documents” to All Files
- Select
hostsand open - Make changes and save
Method 2: PowerShell
# Open in notepad (as admin)
Start-Process notepad -ArgumentList "C:\Windows\System32\drivers\etc\hosts" -Verb RunAs
# Or add entry directly
Add-Content -Path "C:\Windows\System32\drivers\etc\hosts" -Value "`n192.168.1.100`tserver.local" -ForceMethod 3: Command Prompt (as Admin)
notepad C:\Windows\System32\drivers\etc\hostsNote: You must run as Administrator to save changes.
Flush DNS Cache
After editing the hosts file, flush the DNS cache for changes to take effect immediately:
Command Prompt
ipconfig /flushdnsPowerShell
Clear-DnsClientCacheVerify Cache Cleared
ipconfig /displaydnsCommon Scenarios
Override Portal/Server Location
# Force connection to specific data center
198.217.73.93 portal.company.com # East DC
# 198.217.96.93 portal.company.com # West DC
Block Unwanted Domains
# Block tracking/ads
127.0.0.1 ads.example.com
127.0.0.1 tracking.example.com
0.0.0.0 telemetry.example.com
Local Development
# Point domain to local dev server
127.0.0.1 myapp.local
127.0.0.1 api.myapp.local
Troubleshooting
Changes Not Taking Effect
- Flush DNS cache (see above)
- Check file encoding - Must be ANSI or UTF-8 without BOM
- Check permissions - File may be read-only or protected
- Browser cache - Clear browser cache or use incognito
- Proxy settings - Proxy may bypass hosts file
Verify Resolution
ping hostname
nslookup hostnameping uses the hosts file; nslookup queries DNS directly. If ping resolves but nslookup doesn’t, your hosts entry is working.
Reset to Default
Default Windows hosts file content:
# 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