Brief

It’s crucial to understand why you might need to do this. Essentially, the hosts file is like an address book for your computer. When you type in a domain name, your system looks it up in this file to find the corresponding IP address. By modifying the hosts file, you can redirect traffic from a live website to a development environment. This is particularly useful for developers and site administrators who want to preview their website before going live or make changes without affecting the real site.

Three main operating systems will be covered in this example – this should cover 99% of users out there, and the rest are probably using mobile devices, which don’t apply to this situation.


Updating Hosts File on MacOS
Step 1: Open Terminal
– Press Cmd + Space to open Spotlight, type “Terminal,” and hit Enter.

Step 2: Edit Hosts File
– Type sudo nano /etc/hosts and press Enter. Enter your password when prompted.

Step 3: Add Your Entries
– Navigate using the arrow keys to where you want to insert the IP address and domain. The format should be:

102.54.94.97 example.com

or

ip-address hostname

Step 4: Save and Exit
– Press Ctrl + O to write the file, then Enter to confirm. Exit by pressing Ctrl + X.

Step 5: Flush DNS Cache (Optional)
– To ensure your system recognizes changes, flush the DNS cache by typing sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder.


Updating Hosts File on Windows
Step 1: Open Notepad as an Administrator
– Search for Notepad in the Start menu, right-click, and select “Run as administrator.”

Step 2: Open Hosts File
– In Notepad, go to File > Open and navigate to C:WindowsSystem32driversetchosts. Make sure to select “All Files” in the file type dropdown so you can see the hosts file.

Step 3: Add Your Entries
– Add lines for the IP addresses and domain names you wish to redirect, like so:

102.54.94.97 example.com

or

ip-address hostname

Step 4: Save and Exit
– Save the file and close Notepad.

Step 5: Flush DNS Cache (Optional)
– Open Command Prompt as an Administrator and type ipconfig /flushdns.


Updating Hosts File on Ubuntu
Step 1: Open Terminal
– Press Ctrl + Alt + T or search for “Terminal” in your applications.

Step 2: Edit Hosts File
– Type sudo nano /etc/hosts and press Enter. Enter your password when prompted.

Step 3: Add Your Entries
– Use the arrow keys to navigate and add your IP address and domain in the following format:

102.54.94.97 example.com

or

ip-address hostname

Step 4: Save and Exit
– Press Ctrl + O to save and Ctrl + X to exit.

Step 5: Flush DNS Cache (Optional)
– If you want to ensure the system recognizes your changes immediately, flush the DNS cache by typing sudo systemctl restart NetworkManager.


Conclusion

Updating your hosts file is a powerful tool for managing your development environment and testing websites before they go live. Remember that any changes you make only affect your local machine. Always back up your original hosts file before making modifications and follow these steps carefully for each respective operating system.