Overview
A hosts file allows a computer to override public DNS resolution and direct a specific domain name to a manually specified IP address. This is commonly used during website migrations, server replacements, DNS changes, and troubleshooting.
By modifying the hosts file, you can view a website on its new server before changing public DNS records, allowing testing and validation without affecting visitors to the live site.
Common Use Cases
Website Migration Testing
The most common use case is when a website is being moved to a new hosting provider or server.
Before updating DNS:
The website is copied to the new server.
The website is tested using a hosts file entry.
Any issues are corrected.
DNS is updated only after testing is complete.
This reduces the risk of downtime and unexpected problems after launch.
Website Redesign or Rebuild
If a new version of a website is being developed on a separate server, a hosts file can be used to preview the new site while the public continues to see the existing website.
Verifying DNS Changes
Administrators can verify that a website responds correctly on a new IP address before making DNS changes visible to the public.
SSL Certificate Testing
Hosts file overrides can be used to validate website functionality and SSL certificate installation prior to DNS cutover.
Troubleshooting
Support staff and developers may temporarily use hosts file entries to:
Bypass DNS issues
Test connectivity to a specific server
Verify load balancer or CDN configurations
Diagnose website problems
How the Hosts File Works
Normally, when a user visits:
the operating system queries DNS servers to determine the IP address.
When a hosts file entry exists:
203.0.113.100 www.example.com
the operating system uses the specified IP address instead of performing a DNS lookup.
This override only affects the device where the hosts file is modified.
Other users continue to use normal DNS resolution.
Important Considerations
The Website Must Exist on the Destination Server
A hosts file only changes where the computer connects.
The website, database, SSL certificate, and associated services must already be configured and functioning on the destination server.
The Change Is Local
Hosts file modifications affect only the computer where the changes are made.
Visitors and other devices will continue using public DNS records.
Remember to Remove Entries
After DNS changes have propagated and testing is complete, remove the hosts file entry to return to normal DNS resolution.
Failure to do so can result in viewing an outdated or incorrect version of a website.
Windows Instructions
Step 1: Open Notepad as Administrator
Click Start.
Search for Notepad.
Right-click Notepad.
Select Run as administrator.
Step 2: Open the Hosts File
Open:
C:\Windows\System32\drivers\etc\hosts
If the file is not visible, change the file type dropdown from:
Text Documents (*.txt) to All Files (.)
Select and open the file names hosts
Step 3: Add the Entry
Add a line at the bottom of the file:
203.0.113.100 example.com
203.0.113.100 www.example.com
Replace:
203.0.113.100with the destination server IPexample.comwith the actual domain
Step 4: Save the File
Save the file.
Step 5: Clear DNS Cache
Open Command Prompt as Administrator and run:
ipconfig /flushdns
You can now browse the website using the specified server.
macOS Instructions
Step 1: Open Terminal
Open:
Applications → Utilities → Terminal
Step 2: Edit the Hosts File
Run:
sudo nano /etc/hosts
Enter your macOS password when prompted.
Step 3: Add the Entry
Add:
203.0.113.100 example.com
203.0.113.100 www.example.com
Replace:
203.0.113.100with the destination server IPexample.comwith the actual domain
Step 4: Save the File
Press Control + O then Enter to save.
Exit Nano:
Control + X
Step 5: Clear DNS Cache
For modern versions of macOS:
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
The hosts file override is now active.
Linux Instructions
Most Linux distributions use the same hosts file location.
Step 1: Open Terminal
Open your preferred terminal application.
Step 2: Edit the Hosts File
Using Nano:
sudo nano /etc/hosts
Or using Vim:
sudo vim /etc/hosts
Step 3: Add the Entry
Add:
203.0.113.100 example.com
203.0.113.100 www.example.com
Replace:
203.0.113.100with the destination server IPexample.comwith the actual domain
Step 4: Save the File
Save and exit the editor.
Step 5: Flush DNS Cache (if applicable)
Depending on the Linux distribution, one of the following commands may be required:
systemd-resolved
sudo resolvectl flush-caches
nscd
sudo service nscd restart
dnsmasq
sudo service dnsmasq restart
Many Linux systems will begin using the updated hosts file immediately without additional steps.
Verifying That the Hosts File Is Working
Method 1: Ping
Windows:
ping example.com
Linux/macOS:
ping example.com
The returned IP address should match the IP entered in the hosts file.
Method 2: NSLookup
nslookup example.com
Note that nslookup queries DNS servers directly and may not reflect hosts file entries. Ping and web browser access are typically better validation methods.
Method 3: Test in a Browser
Visit:
If the hosts file entry is working, your computer will connect to the specified server regardless of public DNS records.
Removing the Hosts File Entry
After DNS changes have propagated:
Edit the hosts file again.
Remove the lines that were added.
Save the file.
Flush the DNS cache.
Your computer will resume using normal public DNS resolution.
Only your computer will view the website on the new server, allowing you to test functionality before making the DNS change visible to the public.
This approach is widely considered the safest method for validating website migrations before changing DNS records or putting a new website into production.