The hosts file allows you to override DNS resolution for specific domains, directing them to IP addresses of your choice. This is useful for testing websites before DNS changes propagate or for development purposes.
Linux and macOS hosts file location
/etc/hosts
Windows machines hosts file location
C:\Windows\System32\drivers\etc\hosts
You need Administrative/root privileges.
Open the hosts file with any text editor such as vim or nano and add the following lines at the end of the file as examples:
127.0.0.1 mytestsite.local
192.168.1.100 dev.example.com
10.0.0.5 staging.mysite.com
Replace the IP and domain name with real ones.
Save and Close the File
Clear DNS cache
Linux (Ubuntu/Debian):
sudo systemd-resolve --flush-caches
# OR
sudo /etc/init.d/nscd restart
Linux (CentOS/RHEL/Fedora):
sudo systemctl restart NetworkManager
# OR
sudo systemctl restart nscd
macOS:
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
Windows:
ipconfig /flushdns
Now if you ping the domain from your localhost it should return your new server IP address which indicates that it is pointed to it.
Clear browser cache before testing or open the website in a Private/Incognito window.