The local hosts file maps IP addresses to hostnames (FQDNs and short aliases) on the local machine before querying external DNS servers. This is especially useful when testing a new web application, lab server, or migration target before public or internal DNS records are updated.
Location of the hosts File in Windows
On Windows (Windows 8, 10, 11, and Windows Server), the hosts file is located at:
C:\Windows\System32\drivers\etc\hosts
Default contents of C:\Windows\System32\drivers\etc\hosts:
# 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
172.2.2.24 chefmgrserver.ahmed.com
How to Edit the hosts File on Windows
Because C:\Windows\System32\drivers\etc\hosts is a protected system file, editing it requires Administrator privileges.
Method 1: Using Notepad as Administrator
- Press the Windows key and type
Notepad. - Right-click Notepad and select Run as administrator, then click Yes at the User Account Control (UAC) prompt.
- In Notepad, click File → Open (
Ctrl+O). - Navigate to
C:\Windows\System32\drivers\etc\. - Change the file filter drop-down in the bottom-right corner from Text Documents (
*.txt) to All Files (*.*) and openhosts. - Add your mapping on a new line in the format
<IP_Address> <FQDN> [optional_short_alias]:172.2.2.24 chefmgrserver.ahmed.com chefmgrserver - Save the file (
Ctrl+S). Make sure Notepad does not append a.txtextension (hosts.txt).
Method 2: Using an Elevated PowerShell Prompt
Open PowerShell as Administrator and either open the file in Notepad:
notepad C:\Windows\System32\drivers\etc\hosts
or append an entry directly from PowerShell:
Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value "`n172.2.2.24`tchefmgrserver.ahmed.com" -Encoding ASCII
Flush the DNS Cache and Verify Resolution
After saving changes to the hosts file, flush the Windows DNS client resolver cache and verify that the hostname resolves to the expected IP address:
ipconfig /flushdns
ping -n 1 chefmgrserver.ahmed.com
Note: Use
pingorResolve-DnsNameto testhostsfile entries rather thannslookup.nslookupqueries the configured DNS server directly and bypasses the localhostsfile entirely.
Equivalent on Linux and macOS
On Linux and macOS, the hosts file is located at /etc/hosts and can be edited with sudo:
sudo vim /etc/hosts