1. Home
  2. Other
  3. Testing a New Site by Editing your Hosts File

Testing a New Site by Editing your Hosts File

You’ve got a new version of a website up on a new server. How do you test the new site without changing DNS records to point to the new server?

One of the easiest ways to do this is by tricking your local computer or laptop into thinking your domain is pointed at your new server. With this method, the rest of the internet still thinks your domain is pointed at your old server, so service is unaffected while you’re testing.

To achieve this, you can edit the hosts file to add local DNS overrides. The instructions are slightly different depending on whether you’re running Mac, Windows or Linux on your local computer or laptop.

What information do I need?

  1. The domains you want to test (eg, example.com and www.example.com).
  2. The IP address of your new server (eg, 203.0.113.100).
  3. The operating system of your local computer or laptop (eg, Windows).

What is the format of the hosts file?

Each line starts with an IP address, followed by a space and then a list of one or more domains. (This is similar for Mac, Windows and Linux.)

For example, you might add a line at the bottom that looks like this:

203.0.113.100 example.com www.example.com

Once you’ve added that line, your local web browser will behave as if your domains (example.com and www.example.com) are pointed at your new server (203.0.113.100).

So if you go to http://example.com in your browser, the real DNS records are ignored and your browser takes you to the site as it looks on your new server instead.

How do I edit the hosts file?

Follow the instructions below for MacWindows or Linux.

Add as many lines as you need using the format described above.

Mac OS X

nano is a text editor that you can use to edit the hosts file. Open the Terminal application and then run the following command. It will prompt for your user password.

sudo nano /private/etc/hosts

Use the cursor keys to navigate, and add as many lines as you need using the format above. Save the file by pressing Ctrl+x. It will prompt for confirmation at the bottom; press y.

To fully apply the changes, flush the local DNS cache. In the Terminal, run this command:

dscacheutil -flushcache

Windows

In the Windows menu, right-click Notepad and click Run as administrator. Then open c:\Windows\System32\Drivers\etc\hosts.

Add as many lines as you need using the format above. Save the file by clicking File and then Save.

Linux

nano is a text editor that you can use to edit the hosts file. Open the a terminal and then run the following command. It will prompt for your user password.

sudo nano /etc/hosts

Use the cursor keys to navigate, and add as many lines as you need using the format above. Save the file by pressing Ctrl+x. It will prompt for confirmation at the bottom; press y.

Don’t forget to undo your changes!

Once you’ve finished testing, do make sure you remove the lines you’ve added to the hosts file.

Updated on October 22, 2018

Was this article helpful?

Related Articles

Have you tried Kubernetes?
Kubernetes (K8s) is helping enterprises to ship faster & scale their business. Sounds good? Let us build a K8s solution for your needs.
Register your interest