Get 30% off your WordPress Hosting with code WPSAVE10. Launch Faster. Save More!
  • 00 Days
  • 00 Hours
  • 00 Minutes
  • 00 Seconds

How to edit the Hosts File to preview websites

What is the Hosts File?

The hosts file is a plain text file used by your operating system to map hostnames to IP addresses. It’s like an address book for your computer that tells it where to find websites on the internet.

Why edit the Hosts File?

  • Preview changes to a website before it goes live.
  • Test a website on a new server without changing the DNS.
  • Block access to certain websites for security reasons.

Edit the Hosts File

1. Open the Hosts File

The location of the hosts file varies depending on your operating system:

  • Windows: C:\Windows\System32\drivers\etc\hosts
  • Mac and Linux: /etc/hosts

2. Backup the Hosts File

Before making any changes, it’s a good idea to create a backup of the hosts file.

  • Windows: Copy the file to another location.
  • Mac and Linux: Use the terminal command:
sudo cp /etc/hosts /etc/hosts.backup

3. Edit the Hosts File

Windows
  1. Open Notepad as an administrator:
  • Search for Notepad in the Start menu.
  • Right-click and select Run as administrator.
  1. In Notepad, go to File > Open and navigate to C:\Windows\System32\drivers\etc\hosts.
  • Select All Files in the file type dropdown to see the hosts file.
  1. Add your new entries at the end of the file:
127.0.0.1    example.com
192.168.1.1  test.example.com
  • 127.0.0.1 is the IP address for your local machine.
  • Replace example.com and test.example.com with the domains you are testing.
Mac and Linux
  1. Open the Terminal.
  2. Use the nano text editor to open the hosts file with administrative privileges:
sudo nano /etc/hosts
  1. Enter your password if prompted.
  2. Add your new entries at the end of the file:
127.0.0.1    example.com
192.168.1.1  test.example.com
  1. Press CTRL + X, then Y, and Enter to save and exit.

4. Flush the DNS Cache

After editing the hosts file, you may need to flush your DNS cache to apply the changes.

Windows
  1. Open Command Prompt as an administrator:
  • Search for cmd in the Start menu.
  • Right-click and select Run as administrator.
  1. Type the following command and press Enter:
ipconfig /flushdns
Mac
  1. Open Terminal.
  2. Type the following command and press Enter:
sudo killall -HUP mDNSResponder
Linux
  1. Open Terminal.
  2. Depending on your distribution, type one of the following commands:
sudo systemctl restart network-manager

or

sudo /etc/init.d/nscd restart

Tips and Troubleshooting

  • Permissions: Ensure you have administrative or root permissions to edit the hosts file.
  • Syntax: Double-check your entries for correct IP addresses and domain names.
  • Revert Changes: If something goes wrong, you can restore your backup hosts file.

Editing the hosts file is a simple yet powerful tool for web developers and IT professionals. With this guide, you should be able to preview websites or make local changes easily and safely.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to disable PHP error reporting – Enhance Hosting

In this guide, we will learn how to hide or suppress PHP errors and notices such as the common...

How to manage DNS – Enhance Hosting

How to add or edit DNS To add or edit DNS entries: Open Websites in the left sidebar Select...

Add a Staging Site & Push Live – Enhance Hosting

To add a staging website Open Websites in the left sidebar Click Add new Select either Start...

Importing a cPanel backup – Enhance Hosting

The cPanel importer allows you to migrate a cPanel account into your website. To import a...

How to manage FTP accounts – Enhance Hosting

To add an FTP accounts on a single website: On the website dashboard select the advanced...