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?
- The domains you want to test (eg,
example.com
andwww.example.com
). - The IP address of your new server (eg,
203.0.113.100
). - 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 Mac, Windows 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.