How To Edit Host File To Preview Your Site Easily (2024)

How To Edit Host File To Preview Your Site

When you migrate to a new host or change your domain’s nameserver settings, it takes time for these settings to take effect. One of the reasons is the time required for DNS propagation. You cannot access your site using your domain name until DNS propagation is complete. To overcome this, we usually edit the host file.

There was no automated, centralized method for resolving hostnames before DNS was introduced. A list of known hostnames and their corresponding IP addresses is contained in the host file, which was once the foundation of every computer or local network.

Computers no longer rely on this filefile because DNS has automatically enabled them to retrieve hostname addresses from the DNS network. Nonetheless, the host file is in even the most recent operating systems (OS) because it is still functional.

A selected domain can be mapped to any IP address of your choosing. This local change only affects your computer and has no effect on other users. You can preview your website before the DNS propagation is complete by editing the host’s file on your local computer. This guide will show you how to edit host file to preview your site.

How To Edit Host File On Windows

You must have Administrator access to edit the host file on Windows. That should be fine if you’re using your computer. However, you might not have access to it if you’re using a laptop provided by the employer.

This same general strategy should function whether you’re running Windows 10, Windows 8, Windows Vista, or even an earlier version. 

Step 1: Open Text Editor As Administrator

You must launch your preferred text editor with administrator rights to begin.

Right-click on the icon of your text editor and select “Run as administrator” to accomplish that.

Notepad Run As Administrator - How To Edit Host File To Preview Your Site

I’ll use Notepad for this example, but you can use any text editor like Notepad++.

Step 2: Open the host File

To open your text editor, select file > Open. Next, navigate to the following folder location (you can go directly there by pasting it into the navigation bar):

drivers, etc., in C:\Windows\System32\etc\

A list of files with hosts should appear. Select Open by clicking on the host file.

Step 3: Add New Entries With Correct Format

You can now add as many new entries as you require.

Make use of this format:

1.2.3.4 www.yoursitename.com

Go to the last line. Add the server IP address, your domain name, and its www version.

Put the IP address for each on a separate line. You can add more than one hostname on the same line for each IP address.

Here’s an illustration of how it will appear:

How To Edit Host File To Preview Your Site

Now, Save the filefile and Flush the DNS Cache. To do that:

A) Open “Run” (Windows+R). Type “cmd” and click ok. This will open the command prompt.

How To Edit Host File To Preview Your Site

B) In the command prompt, run the following command:

ipconfig /flushdns

How To Edit Host File To Preview Your Site
How To Edit Host File To Preview Your Site

C) Hit Enter. You will get a success message once the DNS cache is clear.

Launch your browser, enter the website’s address, and explore your site. Let’s see how you can do the same thing on Mac.

How To Edit Host File On Mac

To edit the host’s file on Mac, you must be logged in as a user with valuable administrator privileges. You will also need the password for your administrator account.

Step 1: Launch the Terminal

To get started, launch the terminal. You can find it by clicking on the search icon and then heading to Applications and then Utilities:

Step 2: Enter Command In Terminal

In the terminal, enter the following command and then hit enter:

sudo nano /private/etc/hosts

If prompted, you might also be required to fill in your administrator password after running the command.

How To Edit Host File To Preview Your Site

Step 3: Include New Entries Observing Proper Format

Now that you’re using the text editor, add as many entries as necessary. Use the arrow keys to move between lines in the text editor, and press enter to start a new line.

Use the following format, just as you would with any other operating system:

1.2.3.4.5 www.yoursitedomainname.com

Put the IP address for each on a separate line. You can add more than one hostname on the same line for each IP address.

Here’s an illustration of how it could appear:

How To Edit Host File To Preview Your Site

Lastly, to save your changes, type Control + X.

How To Edit Host File On Linux

You can use your desired text editor in Linux to do the same job. Here, I will use Gedit in Ubuntu. 

Firstly, launch the terminal by pressing Ctrl+Alt+T.

Type the command below, then fill in your admin password:

sudo su

To open the host’s file using Gedit, type the following command and press Enter:

sudo gedit /etc/hosts

Now add the targeted IP address and ensure a space follows them, then the domain name at the bottom of the host file.

Save your changes.

Conclusion

When you want to create a unique hostname mapping for your computer only, unaffected by other users, the host file can be beneficial. Generally speaking, its most popular uses are:

Preventing a computer from connecting to a particular hostname

  1. Connecting your website to a non-existing domain
  2. Forcing a domain to open a different website from the one to which your computer’s DNS records presently point it
  3. At first, editing the host file might seem complicated, but it’s not that difficult if you follow the instructions. We hope that this post helped to clarify things.

I hope you found this guide on Editing your host files to preview your site helpful. If yes, let me know in the comments below. Also, feel free to ask any questions, and I will answer them happily. Happy-host-file-editing!

FAQs

Do I need to restart my computer or any services after editing the host’s filefile?

On most systems, changes to the host file take effect immediately. However, you might need to flush the DNS cache on some older Windows systems or restart the DNS Client service. It’s a good practice to clear your browser cache as well.

What should I do if I encounter permission issues when saving the host’s filefile?

Ensure that you are editing the host file with administrative or sudo privileges. On some systems, you may need to open the text editor as an administrator or use the `sudo` command to save changes.

Can I use the host’s filefile to preview a site on a mobile device?

Yes, you can. To preview a site on a mobile device, you need to edit the host file on your computer and connect the mobile device to the same local network. Ensure your mobile device uses the computer’s IP address as the DNS server.

Is there a risk of causing issues by editing the host’s filefile?

Editing the host file carries minimal risk if done correctly. However, it’s crucial to be cautious and follow proper syntax. Incorrect entries may lead to DNS issues, so double-check your changes and revert them if problems arise.

Want faster WordPress?

WordPress Speed Optimization

Try our AWS powered WordPress hosting for free and see the difference for yourself.

No Credit Card Required.

Whitelabel Web Hosting Portal Demo

Launching WordPress on AWS takes just one minute with Nestify.

Launching WooCommerce on AWS takes just one minute with Nestify.