windows
Editing Windows Host File
📁 Editing Windows Host File
The Windows hosts file is a plain text file used to map hostnames to IP addresses. It is a critical, low-level component that can be used to override DNS settings locally.
Note: This procedure works ONLY IF the target network device(s) has a STATIC IP. Ensure you SET A DEVICE STATIC IP IF/AS NEEDED!
Common Reasons to Modify the Hosts File
- Redirecting Domains: To point a domain to a different IP address for development or testing purposes.
- Blocking Websites: To prevent access to specific websites by redirecting them to a non-existent or loopback IP (e.g.,
0.0.0.0). - Testing Server Changes: To test changes on a staging server by pointing a domain to the staging server’s IP instead of the production server.
- Improving Local Network Access & Performance:
- Used to create a static “direct link” to resolve local network hostnames when experiencing general network device access issues (e.g., DNS or network bugs).
- Resolves local network hostnames faster without querying a DNS server.
- Always triple check a system’s file and print sharing setup/settings before modifying the hosts file.
- Bypassing DNS: To bypass the DNS settings for specific domains, useful for troubleshooting DNS issues (e.g., the static link bypass).
Procedure to Modify the Windows Hosts File
Step 1: Locate the Hosts File
The hosts file is located in the following directory: C:\Windows\System32\drivers\etc\hosts
Step 2: Open Notepad as Administrator
- Search for Notepad: Press $\text{Windows} + \text{S}$, type
Notepad. - Run as Administrator: Right-click on Notepad and select Run as administrator.
Step 3: Open the Hosts File in Notepad
- In Notepad, click File $\rightarrow$ Open.
- Navigate to
C:\Windows\System32\drivers\etc. - In the lower right corner, change the file type from Text Documents (*.txt) to All Files (*.*).
- Select the hosts file and click Open.
Step 4: Modify and Replace the Hosts File (Permissions Bypass)
- Permissions Note: You typically cannot save the edited file directly back to the
\etcfolder unless using advanced tools. We use a workaround:
- Add Entries: To add a new entry, type the IP address followed by a space or tab, then the hostname.
127.0.0.1 example.com 192.168.1.100 localtestserver - Save to Desktop: Click File $\rightarrow$ Save As and save the newly edited file to the system’s Desktop.
- Show File Extensions: Through the Windows File Explorer, navigate to the Desktop. Under the “View” menu, mouse over “Show”, and then click “File Name Extensions”.
- Rename File: The modified host file saved to the Desktop will show as
hosts.txt. Rename it by removing the.txtextension, so the file simply readshosts.- Windows will warn you; accept the change.
- Backup Original: Navigate back to the system’s host file directory:
C:\Windows\System32\drivers\etc\ - Rename the system’s current
hostsfile tohosts.old(accept the Windows warning/permission request). - Copy/Paste New File: Copy/paste or drag your modified
hostsfile from the Desktop to the system’s\etcdirectory.- Windows will ask for Administrative Permission; accept it.
- Clean Up: Go back to File Explorer and change the View settings back to NOT show file extensions.
Step 5: Flush DNS Cache (As Needed)
After modifying the hosts file, it is often necessary to flush the DNS cache for changes to take effect immediately.
- Open Command Prompt as Administrator ($\text{Windows} + \text{S}$, type
cmd, right-click $\rightarrow$ Run as administrator). - Run the command:
ipconfig /flushdns
📝 Example Modifications
| Purpose | Example Entry |
|---|---|
| Redirecting to Localhost | 127.0.0.1 mytestsite.com |
| Blocking a Website | 0.0.0.0 badwebsite.com |
| Testing a New Server | 192.168.1.50 mysite.com |
Important Considerations
- Backup: By renaming the original hosts file to
hosts.old, you create an easy backup for reversion if needed. - Syntax: Ensure there are no typos. Use the format:
[IP Address] [Hostname].- Comments can be added using the
#character at the start of the line.
- Comments can be added using the
- Permissions: Modifying the hosts file requires administrative privileges.