Need to test a website on a different IP address before DNS changes go live? Editing your hosts file is a great way to override DNS locally. This guide will walk you through how to do it on Windows, macOS, and Linux.
โ ๏ธ Important: Editing the hosts file requires administrator/root access. Be cautious and double-check your changes!
๐ ๏ธ What is the hosts File?
The hosts file maps domain names to IP addresses manually. When your system resolves a URL, it checks this file before querying DNS servers.
Example entry:
203.0.113.42 example.com
This means requests to example.com will go to 203.0.113.42 instead of its real DNS address.
๐ป Windows
1. Open Notepad as Administrator
- Press
Start, search for Notepad. - Right-click it and select Run as administrator.
- In Notepad, go to
File>Open. Navigate to:
C:\Windows\System32\drivers\etc\
- Set file type to All Files (*.*) and open
hosts.
2. Add Your Entry
At the bottom, add your mapping, like:
203.0.113.42 test.example.com
3. Save and Test
- Save the file.
Open
cmd.exeand run:ipconfig /flushdns
Test the domain in your browser or using
ping:ping test.example.com
๐ macOS
1. Open Terminal
Open the Terminal app (found in /Applications/Utilities/).
2. Edit the Hosts File
Use sudo to open the file in a text editor:
sudo nano /etc/hosts
3. Add Your Entry
At the end of the file, add:
203.0.113.42 test.example.com
4. Save and Flush DNS
- Save with
Ctrl + O, then pressEnter. - Exit with
Ctrl + X.
Flush DNS cache:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Test with:
ping test.example.com
๐ง Linux
Most distributions use /etc/hosts.
1. Open Terminal and Edit Hosts File
sudo nano /etc/hosts
2. Add Your Entry
Example:
203.0.113.42 test.example.com
3. Save and Exit
- Save with
Ctrl + O, pressEnter. - Exit with
Ctrl + X.
4. (Optional) Flush DNS Cache
Some Linux systems (like those using systemd-resolved) may cache DNS:
sudo systemd-resolve --flush-caches
Or:
sudo service nscd restart
Test:
ping test.example.com
โ Wrap-Up
By editing your hosts file, you can:
- Test new servers before DNS propagation
- Bypass external DNS temporarily
- Develop sites locally using production domains
To revert changes, simply remove the entries from your hosts file and flush the DNS cache again.
Happy testing! ๐งช๐ง
Comments
Please sign in to leave a comment.