Windows Hosts File Reference

windows networking dns


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

  1. Press Win + S, type Notepad
  2. Right-click → Run as administrator
  3. File → Open → Navigate to C:\Windows\System32\drivers\etc\
  4. Change filter from “Text Documents” to All Files
  5. Select hosts and open
  6. 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" -Force

Method 3: Command Prompt (as Admin)

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

Note: 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 /flushdns

PowerShell

Clear-DnsClientCache

Verify Cache Cleared

ipconfig /displaydns

Common 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

  1. Flush DNS cache (see above)
  2. Check file encoding - Must be ANSI or UTF-8 without BOM
  3. Check permissions - File may be read-only or protected
  4. Browser cache - Clear browser cache or use incognito
  5. Proxy settings - Proxy may bypass hosts file

Verify Resolution

ping hostname
nslookup hostname

ping 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